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