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/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// SecurityRulesClient is the network Client
19type SecurityRulesClient struct {
20	BaseClient
21}
22
23// NewSecurityRulesClient creates an instance of the SecurityRulesClient client.
24func NewSecurityRulesClient(subscriptionID string) SecurityRulesClient {
25	return NewSecurityRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewSecurityRulesClientWithBaseURI creates an instance of the SecurityRulesClient client using a custom endpoint.
29// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewSecurityRulesClientWithBaseURI(baseURI string, subscriptionID string) SecurityRulesClient {
31	return SecurityRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// CreateOrUpdate creates or updates a security rule in the specified network security group.
35// Parameters:
36// resourceGroupName - the name of the resource group.
37// networkSecurityGroupName - the name of the network security group.
38// securityRuleName - the name of the security rule.
39// securityRuleParameters - parameters supplied to the create or update network security rule operation.
40func (client SecurityRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string, securityRuleParameters SecurityRule) (result SecurityRulesCreateOrUpdateFuture, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.CreateOrUpdate")
43		defer func() {
44			sc := -1
45			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
46				sc = result.FutureAPI.Response().StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	if err := validation.Validate([]validation.Validation{
52		{TargetValue: securityRuleParameters,
53			Constraints: []validation.Constraint{{Target: "securityRuleParameters.SecurityRulePropertiesFormat", Name: validation.Null, Rule: false,
54				Chain: []validation.Constraint{{Target: "securityRuleParameters.SecurityRulePropertiesFormat.SourceAddressPrefix", Name: validation.Null, Rule: true, Chain: nil},
55					{Target: "securityRuleParameters.SecurityRulePropertiesFormat.DestinationAddressPrefix", Name: validation.Null, Rule: true, Chain: nil},
56				}}}}}); err != nil {
57		return result, validation.NewError("network.SecurityRulesClient", "CreateOrUpdate", err.Error())
58	}
59
60	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, networkSecurityGroupName, securityRuleName, securityRuleParameters)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "CreateOrUpdate", nil, "Failure preparing request")
63		return
64	}
65
66	result, err = client.CreateOrUpdateSender(req)
67	if err != nil {
68		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "CreateOrUpdate", nil, "Failure sending request")
69		return
70	}
71
72	return
73}
74
75// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
76func (client SecurityRulesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string, securityRuleParameters SecurityRule) (*http.Request, error) {
77	pathParameters := map[string]interface{}{
78		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
79		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
80		"securityRuleName":         autorest.Encode("path", securityRuleName),
81		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
82	}
83
84	const APIVersion = "2017-03-01"
85	queryParameters := map[string]interface{}{
86		"api-version": APIVersion,
87	}
88
89	preparer := autorest.CreatePreparer(
90		autorest.AsContentType("application/json; charset=utf-8"),
91		autorest.AsPut(),
92		autorest.WithBaseURL(client.BaseURI),
93		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}", pathParameters),
94		autorest.WithJSON(securityRuleParameters),
95		autorest.WithQueryParameters(queryParameters))
96	return preparer.Prepare((&http.Request{}).WithContext(ctx))
97}
98
99// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
100// http.Response Body if it receives an error.
101func (client SecurityRulesClient) CreateOrUpdateSender(req *http.Request) (future SecurityRulesCreateOrUpdateFuture, err error) {
102	var resp *http.Response
103	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
104	if err != nil {
105		return
106	}
107	var azf azure.Future
108	azf, err = azure.NewFutureFromResponse(resp)
109	future.FutureAPI = &azf
110	future.Result = future.result
111	return
112}
113
114// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
115// closes the http.Response Body.
116func (client SecurityRulesClient) CreateOrUpdateResponder(resp *http.Response) (result SecurityRule, err error) {
117	err = autorest.Respond(
118		resp,
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.FutureAPI != nil && result.FutureAPI.Response() != nil {
137				sc = result.FutureAPI.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", nil, "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 = "2017-03-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	var azf azure.Future
188	azf, err = azure.NewFutureFromResponse(resp)
189	future.FutureAPI = &azf
190	future.Result = future.result
191	return
192}
193
194// DeleteResponder handles the response to the Delete request. The method always
195// closes the http.Response Body.
196func (client SecurityRulesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
197	err = autorest.Respond(
198		resp,
199		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
200		autorest.ByClosing())
201	result.Response = resp
202	return
203}
204
205// Get get the specified network security rule.
206// Parameters:
207// resourceGroupName - the name of the resource group.
208// networkSecurityGroupName - the name of the network security group.
209// securityRuleName - the name of the security rule.
210func (client SecurityRulesClient) Get(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string) (result SecurityRule, err error) {
211	if tracing.IsEnabled() {
212		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.Get")
213		defer func() {
214			sc := -1
215			if result.Response.Response != nil {
216				sc = result.Response.Response.StatusCode
217			}
218			tracing.EndSpan(ctx, sc, err)
219		}()
220	}
221	req, err := client.GetPreparer(ctx, resourceGroupName, networkSecurityGroupName, securityRuleName)
222	if err != nil {
223		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Get", nil, "Failure preparing request")
224		return
225	}
226
227	resp, err := client.GetSender(req)
228	if err != nil {
229		result.Response = autorest.Response{Response: resp}
230		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Get", resp, "Failure sending request")
231		return
232	}
233
234	result, err = client.GetResponder(resp)
235	if err != nil {
236		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "Get", resp, "Failure responding to request")
237		return
238	}
239
240	return
241}
242
243// GetPreparer prepares the Get request.
244func (client SecurityRulesClient) GetPreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, securityRuleName string) (*http.Request, error) {
245	pathParameters := map[string]interface{}{
246		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
247		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
248		"securityRuleName":         autorest.Encode("path", securityRuleName),
249		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
250	}
251
252	const APIVersion = "2017-03-01"
253	queryParameters := map[string]interface{}{
254		"api-version": APIVersion,
255	}
256
257	preparer := autorest.CreatePreparer(
258		autorest.AsGet(),
259		autorest.WithBaseURL(client.BaseURI),
260		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules/{securityRuleName}", pathParameters),
261		autorest.WithQueryParameters(queryParameters))
262	return preparer.Prepare((&http.Request{}).WithContext(ctx))
263}
264
265// GetSender sends the Get request. The method will close the
266// http.Response Body if it receives an error.
267func (client SecurityRulesClient) GetSender(req *http.Request) (*http.Response, error) {
268	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
269}
270
271// GetResponder handles the response to the Get request. The method always
272// closes the http.Response Body.
273func (client SecurityRulesClient) GetResponder(resp *http.Response) (result SecurityRule, err error) {
274	err = autorest.Respond(
275		resp,
276		azure.WithErrorUnlessStatusCode(http.StatusOK),
277		autorest.ByUnmarshallingJSON(&result),
278		autorest.ByClosing())
279	result.Response = autorest.Response{Response: resp}
280	return
281}
282
283// List gets all security rules in a network security group.
284// Parameters:
285// resourceGroupName - the name of the resource group.
286// networkSecurityGroupName - the name of the network security group.
287func (client SecurityRulesClient) List(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (result SecurityRuleListResultPage, err error) {
288	if tracing.IsEnabled() {
289		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.List")
290		defer func() {
291			sc := -1
292			if result.srlr.Response.Response != nil {
293				sc = result.srlr.Response.Response.StatusCode
294			}
295			tracing.EndSpan(ctx, sc, err)
296		}()
297	}
298	result.fn = client.listNextResults
299	req, err := client.ListPreparer(ctx, resourceGroupName, networkSecurityGroupName)
300	if err != nil {
301		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "List", nil, "Failure preparing request")
302		return
303	}
304
305	resp, err := client.ListSender(req)
306	if err != nil {
307		result.srlr.Response = autorest.Response{Response: resp}
308		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "List", resp, "Failure sending request")
309		return
310	}
311
312	result.srlr, err = client.ListResponder(resp)
313	if err != nil {
314		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "List", resp, "Failure responding to request")
315		return
316	}
317	if result.srlr.hasNextLink() && result.srlr.IsEmpty() {
318		err = result.NextWithContext(ctx)
319		return
320	}
321
322	return
323}
324
325// ListPreparer prepares the List request.
326func (client SecurityRulesClient) ListPreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (*http.Request, error) {
327	pathParameters := map[string]interface{}{
328		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
329		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
330		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
331	}
332
333	const APIVersion = "2017-03-01"
334	queryParameters := map[string]interface{}{
335		"api-version": APIVersion,
336	}
337
338	preparer := autorest.CreatePreparer(
339		autorest.AsGet(),
340		autorest.WithBaseURL(client.BaseURI),
341		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/securityRules", pathParameters),
342		autorest.WithQueryParameters(queryParameters))
343	return preparer.Prepare((&http.Request{}).WithContext(ctx))
344}
345
346// ListSender sends the List request. The method will close the
347// http.Response Body if it receives an error.
348func (client SecurityRulesClient) ListSender(req *http.Request) (*http.Response, error) {
349	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
350}
351
352// ListResponder handles the response to the List request. The method always
353// closes the http.Response Body.
354func (client SecurityRulesClient) ListResponder(resp *http.Response) (result SecurityRuleListResult, err error) {
355	err = autorest.Respond(
356		resp,
357		azure.WithErrorUnlessStatusCode(http.StatusOK),
358		autorest.ByUnmarshallingJSON(&result),
359		autorest.ByClosing())
360	result.Response = autorest.Response{Response: resp}
361	return
362}
363
364// listNextResults retrieves the next set of results, if any.
365func (client SecurityRulesClient) listNextResults(ctx context.Context, lastResults SecurityRuleListResult) (result SecurityRuleListResult, err error) {
366	req, err := lastResults.securityRuleListResultPreparer(ctx)
367	if err != nil {
368		return result, autorest.NewErrorWithError(err, "network.SecurityRulesClient", "listNextResults", nil, "Failure preparing next results request")
369	}
370	if req == nil {
371		return
372	}
373	resp, err := client.ListSender(req)
374	if err != nil {
375		result.Response = autorest.Response{Response: resp}
376		return result, autorest.NewErrorWithError(err, "network.SecurityRulesClient", "listNextResults", resp, "Failure sending next results request")
377	}
378	result, err = client.ListResponder(resp)
379	if err != nil {
380		err = autorest.NewErrorWithError(err, "network.SecurityRulesClient", "listNextResults", resp, "Failure responding to next results request")
381	}
382	return
383}
384
385// ListComplete enumerates all values, automatically crossing page boundaries as required.
386func (client SecurityRulesClient) ListComplete(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (result SecurityRuleListResultIterator, err error) {
387	if tracing.IsEnabled() {
388		ctx = tracing.StartSpan(ctx, fqdn+"/SecurityRulesClient.List")
389		defer func() {
390			sc := -1
391			if result.Response().Response.Response != nil {
392				sc = result.page.Response().Response.Response.StatusCode
393			}
394			tracing.EndSpan(ctx, sc, err)
395		}()
396	}
397	result.page, err = client.List(ctx, resourceGroupName, networkSecurityGroupName)
398	return
399}
400