1package network
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/tracing"
25	"net/http"
26)
27
28// SecurityRulesClient is the network Client
29type SecurityRulesClient struct {
30	BaseClient
31}
32
33// NewSecurityRulesClient creates an instance of the SecurityRulesClient client.
34func NewSecurityRulesClient(subscriptionID string) SecurityRulesClient {
35	return NewSecurityRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewSecurityRulesClientWithBaseURI creates an instance of the SecurityRulesClient client using a custom endpoint.
39// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
40func NewSecurityRulesClientWithBaseURI(baseURI string, subscriptionID string) SecurityRulesClient {
41	return SecurityRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// CreateOrUpdate creates or updates a security rule in the specified network security group.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// networkSecurityGroupName - the name of the network security group.
48// securityRuleName - the name of the security rule.
49// securityRuleParameters - parameters supplied to the create or update network security rule operation.
50func (client SecurityRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string, securityRuleParameters SecurityRule) (result SecurityRulesCreateOrUpdateFuture, err error) {
51	if tracing.IsEnabled() {
52		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.CreateOrUpdate")
53		defer func() {
54			sc := -1
55			if result.Response() != nil {
56				sc = result.Response().StatusCode
57			}
58			tracing.EndSpan(ctx, sc, err)
59		}()
60	}
61	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, networkSecurityGroupName, securityRuleName, securityRuleParameters)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "CreateOrUpdate", nil, "Failure preparing request")
64		return
65	}
66
67	result, err = client.CreateOrUpdateSender(req)
68	if err != nil {
69		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "CreateOrUpdate", result.Response(), "Failure sending request")
70		return
71	}
72
73	return
74}
75
76// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
77func (client SecurityRulesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string, securityRuleParameters SecurityRule) (*http.Request, error) {
78	pathParameters := map[string]interface{}{
79		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
80		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
81		"securityRuleName":         autorest.Encode("path", securityRuleName),
82		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
83	}
84
85	const APIVersion = "2020-04-01"
86	queryParameters := map[string]interface{}{
87		"api-version": APIVersion,
88	}
89
90	securityRuleParameters.Etag = nil
91	preparer := autorest.CreatePreparer(
92		autorest.AsContentType("application/json; charset=utf-8"),
93		autorest.AsPut(),
94		autorest.WithBaseURL(client.BaseURI),
95		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}", pathParameters),
96		autorest.WithJSON(securityRuleParameters),
97		autorest.WithQueryParameters(queryParameters))
98	return preparer.Prepare((&http.Request{}).WithContext(ctx))
99}
100
101// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
102// http.Response Body if it receives an error.
103func (client SecurityRulesClient) CreateOrUpdateSender(req *http.Request) (future SecurityRulesCreateOrUpdateFuture, err error) {
104	var resp *http.Response
105	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
106	if err != nil {
107		return
108	}
109	future.Future, err = azure.NewFutureFromResponse(resp)
110	return
111}
112
113// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
114// closes the http.Response Body.
115func (client SecurityRulesClient) CreateOrUpdateResponder(resp *http.Response) (result SecurityRule, err error) {
116	err = autorest.Respond(
117		resp,
118		client.ByInspecting(),
119		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
120		autorest.ByUnmarshallingJSON(&result),
121		autorest.ByClosing())
122	result.Response = autorest.Response{Response: resp}
123	return
124}
125
126// Delete deletes the specified network security rule.
127// Parameters:
128// resourceGroupName - the name of the resource group.
129// networkSecurityGroupName - the name of the network security group.
130// securityRuleName - the name of the security rule.
131func (client SecurityRulesClient) Delete(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string) (result SecurityRulesDeleteFuture, err error) {
132	if tracing.IsEnabled() {
133		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.Delete")
134		defer func() {
135			sc := -1
136			if result.Response() != nil {
137				sc = result.Response().StatusCode
138			}
139			tracing.EndSpan(ctx, sc, err)
140		}()
141	}
142	req, err := client.DeletePreparer(ctx, resourceGroupName, networkSecurityGroupName, securityRuleName)
143	if err != nil {
144		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Delete", nil, "Failure preparing request")
145		return
146	}
147
148	result, err = client.DeleteSender(req)
149	if err != nil {
150		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Delete", result.Response(), "Failure sending request")
151		return
152	}
153
154	return
155}
156
157// DeletePreparer prepares the Delete request.
158func (client SecurityRulesClient) DeletePreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string) (*http.Request, error) {
159	pathParameters := map[string]interface{}{
160		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
161		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
162		"securityRuleName":         autorest.Encode("path", securityRuleName),
163		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
164	}
165
166	const APIVersion = "2020-04-01"
167	queryParameters := map[string]interface{}{
168		"api-version": APIVersion,
169	}
170
171	preparer := autorest.CreatePreparer(
172		autorest.AsDelete(),
173		autorest.WithBaseURL(client.BaseURI),
174		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}", pathParameters),
175		autorest.WithQueryParameters(queryParameters))
176	return preparer.Prepare((&http.Request{}).WithContext(ctx))
177}
178
179// DeleteSender sends the Delete request. The method will close the
180// http.Response Body if it receives an error.
181func (client SecurityRulesClient) DeleteSender(req *http.Request) (future SecurityRulesDeleteFuture, err error) {
182	var resp *http.Response
183	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
184	if err != nil {
185		return
186	}
187	future.Future, err = azure.NewFutureFromResponse(resp)
188	return
189}
190
191// DeleteResponder handles the response to the Delete request. The method always
192// closes the http.Response Body.
193func (client SecurityRulesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
194	err = autorest.Respond(
195		resp,
196		client.ByInspecting(),
197		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
198		autorest.ByClosing())
199	result.Response = resp
200	return
201}
202
203// Get get the specified network security rule.
204// Parameters:
205// resourceGroupName - the name of the resource group.
206// networkSecurityGroupName - the name of the network security group.
207// securityRuleName - the name of the security rule.
208func (client SecurityRulesClient) Get(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string) (result SecurityRule, err error) {
209	if tracing.IsEnabled() {
210		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.Get")
211		defer func() {
212			sc := -1
213			if result.Response.Response != nil {
214				sc = result.Response.Response.StatusCode
215			}
216			tracing.EndSpan(ctx, sc, err)
217		}()
218	}
219	req, err := client.GetPreparer(ctx, resourceGroupName, networkSecurityGroupName, securityRuleName)
220	if err != nil {
221		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Get", nil, "Failure preparing request")
222		return
223	}
224
225	resp, err := client.GetSender(req)
226	if err != nil {
227		result.Response = autorest.Response{Response: resp}
228		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Get", resp, "Failure sending request")
229		return
230	}
231
232	result, err = client.GetResponder(resp)
233	if err != nil {
234		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Get", resp, "Failure responding to request")
235	}
236
237	return
238}
239
240// GetPreparer prepares the Get request.
241func (client SecurityRulesClient) GetPreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string) (*http.Request, error) {
242	pathParameters := map[string]interface{}{
243		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
244		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
245		"securityRuleName":         autorest.Encode("path", securityRuleName),
246		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
247	}
248
249	const APIVersion = "2020-04-01"
250	queryParameters := map[string]interface{}{
251		"api-version": APIVersion,
252	}
253
254	preparer := autorest.CreatePreparer(
255		autorest.AsGet(),
256		autorest.WithBaseURL(client.BaseURI),
257		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}", pathParameters),
258		autorest.WithQueryParameters(queryParameters))
259	return preparer.Prepare((&http.Request{}).WithContext(ctx))
260}
261
262// GetSender sends the Get request. The method will close the
263// http.Response Body if it receives an error.
264func (client SecurityRulesClient) GetSender(req *http.Request) (*http.Response, error) {
265	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
266}
267
268// GetResponder handles the response to the Get request. The method always
269// closes the http.Response Body.
270func (client SecurityRulesClient) GetResponder(resp *http.Response) (result SecurityRule, err error) {
271	err = autorest.Respond(
272		resp,
273		client.ByInspecting(),
274		azure.WithErrorUnlessStatusCode(http.StatusOK),
275		autorest.ByUnmarshallingJSON(&result),
276		autorest.ByClosing())
277	result.Response = autorest.Response{Response: resp}
278	return
279}
280
281// List gets all security rules in a network security group.
282// Parameters:
283// resourceGroupName - the name of the resource group.
284// networkSecurityGroupName - the name of the network security group.
285func (client SecurityRulesClient) List(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (result SecurityRuleListResultPage, err error) {
286	if tracing.IsEnabled() {
287		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.List")
288		defer func() {
289			sc := -1
290			if result.srlr.Response.Response != nil {
291				sc = result.srlr.Response.Response.StatusCode
292			}
293			tracing.EndSpan(ctx, sc, err)
294		}()
295	}
296	result.fn = client.listNextResults
297	req, err := client.ListPreparer(ctx, resourceGroupName, networkSecurityGroupName)
298	if err != nil {
299		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "List", nil, "Failure preparing request")
300		return
301	}
302
303	resp, err := client.ListSender(req)
304	if err != nil {
305		result.srlr.Response = autorest.Response{Response: resp}
306		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "List", resp, "Failure sending request")
307		return
308	}
309
310	result.srlr, err = client.ListResponder(resp)
311	if err != nil {
312		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "List", resp, "Failure responding to request")
313	}
314
315	return
316}
317
318// ListPreparer prepares the List request.
319func (client SecurityRulesClient) ListPreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (*http.Request, error) {
320	pathParameters := map[string]interface{}{
321		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
322		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
323		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
324	}
325
326	const APIVersion = "2020-04-01"
327	queryParameters := map[string]interface{}{
328		"api-version": APIVersion,
329	}
330
331	preparer := autorest.CreatePreparer(
332		autorest.AsGet(),
333		autorest.WithBaseURL(client.BaseURI),
334		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules", pathParameters),
335		autorest.WithQueryParameters(queryParameters))
336	return preparer.Prepare((&http.Request{}).WithContext(ctx))
337}
338
339// ListSender sends the List request. The method will close the
340// http.Response Body if it receives an error.
341func (client SecurityRulesClient) ListSender(req *http.Request) (*http.Response, error) {
342	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
343}
344
345// ListResponder handles the response to the List request. The method always
346// closes the http.Response Body.
347func (client SecurityRulesClient) ListResponder(resp *http.Response) (result SecurityRuleListResult, err error) {
348	err = autorest.Respond(
349		resp,
350		client.ByInspecting(),
351		azure.WithErrorUnlessStatusCode(http.StatusOK),
352		autorest.ByUnmarshallingJSON(&result),
353		autorest.ByClosing())
354	result.Response = autorest.Response{Response: resp}
355	return
356}
357
358// listNextResults retrieves the next set of results, if any.
359func (client SecurityRulesClient) listNextResults(ctx context.Context, lastResults SecurityRuleListResult) (result SecurityRuleListResult, err error) {
360	req, err := lastResults.securityRuleListResultPreparer(ctx)
361	if err != nil {
362		return result, autorest.NewErrorWithError(err, "network.SecurityRulesClient", "listNextResults", nil, "Failure preparing next results request")
363	}
364	if req == nil {
365		return
366	}
367	resp, err := client.ListSender(req)
368	if err != nil {
369		result.Response = autorest.Response{Response: resp}
370		return result, autorest.NewErrorWithError(err, "network.SecurityRulesClient", "listNextResults", resp, "Failure sending next results request")
371	}
372	result, err = client.ListResponder(resp)
373	if err != nil {
374		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "listNextResults", resp, "Failure responding to next results request")
375	}
376	return
377}
378
379// ListComplete enumerates all values, automatically crossing page boundaries as required.
380func (client SecurityRulesClient) ListComplete(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (result SecurityRuleListResultIterator, err error) {
381	if tracing.IsEnabled() {
382		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.List")
383		defer func() {
384			sc := -1
385			if result.Response().Response.Response != nil {
386				sc = result.page.Response().Response.Response.StatusCode
387			}
388			tracing.EndSpan(ctx, sc, err)
389		}()
390	}
391	result.page, err = client.List(ctx, resourceGroupName, networkSecurityGroupName)
392	return
393}
394