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// DefaultSecurityRulesClient is the network Client
29type DefaultSecurityRulesClient struct {
30	BaseClient
31}
32
33// NewDefaultSecurityRulesClient creates an instance of the DefaultSecurityRulesClient client.
34func NewDefaultSecurityRulesClient(subscriptionID string) DefaultSecurityRulesClient {
35	return NewDefaultSecurityRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewDefaultSecurityRulesClientWithBaseURI creates an instance of the DefaultSecurityRulesClient client using a custom
39// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
40// stack).
41func NewDefaultSecurityRulesClientWithBaseURI(baseURI string, subscriptionID string) DefaultSecurityRulesClient {
42	return DefaultSecurityRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// Get get the specified default network security rule.
46// Parameters:
47// resourceGroupName - the name of the resource group.
48// networkSecurityGroupName - the name of the network security group.
49// defaultSecurityRuleName - the name of the default security rule.
50func (client DefaultSecurityRulesClient) Get(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, defaultSecurityRuleName string) (result SecurityRule, err error) {
51	if tracing.IsEnabled() {
52		ctx = tracing.StartSpan(ctx, fqdn+"/DefaultSecurityRulesClient.Get")
53		defer func() {
54			sc := -1
55			if result.Response.Response != nil {
56				sc = result.Response.Response.StatusCode
57			}
58			tracing.EndSpan(ctx, sc, err)
59		}()
60	}
61	req, err := client.GetPreparer(ctx, resourceGroupName, networkSecurityGroupName, defaultSecurityRuleName)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "Get", nil, "Failure preparing request")
64		return
65	}
66
67	resp, err := client.GetSender(req)
68	if err != nil {
69		result.Response = autorest.Response{Response: resp}
70		err = autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "Get", resp, "Failure sending request")
71		return
72	}
73
74	result, err = client.GetResponder(resp)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "Get", resp, "Failure responding to request")
77	}
78
79	return
80}
81
82// GetPreparer prepares the Get request.
83func (client DefaultSecurityRulesClient) GetPreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, defaultSecurityRuleName string) (*http.Request, error) {
84	pathParameters := map[string]interface{}{
85		"defaultSecurityRuleName":  autorest.Encode("path", defaultSecurityRuleName),
86		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
87		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
88		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
89	}
90
91	const APIVersion = "2018-07-01"
92	queryParameters := map[string]interface{}{
93		"api-version": APIVersion,
94	}
95
96	preparer := autorest.CreatePreparer(
97		autorest.AsGet(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/defaultSecurityRules/{defaultSecurityRuleName}", pathParameters),
100		autorest.WithQueryParameters(queryParameters))
101	return preparer.Prepare((&http.Request{}).WithContext(ctx))
102}
103
104// GetSender sends the Get request. The method will close the
105// http.Response Body if it receives an error.
106func (client DefaultSecurityRulesClient) GetSender(req *http.Request) (*http.Response, error) {
107	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
108}
109
110// GetResponder handles the response to the Get request. The method always
111// closes the http.Response Body.
112func (client DefaultSecurityRulesClient) GetResponder(resp *http.Response) (result SecurityRule, err error) {
113	err = autorest.Respond(
114		resp,
115		client.ByInspecting(),
116		azure.WithErrorUnlessStatusCode(http.StatusOK),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// List gets all default security rules in a network security group.
124// Parameters:
125// resourceGroupName - the name of the resource group.
126// networkSecurityGroupName - the name of the network security group.
127func (client DefaultSecurityRulesClient) List(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (result SecurityRuleListResultPage, err error) {
128	if tracing.IsEnabled() {
129		ctx = tracing.StartSpan(ctx, fqdn+"/DefaultSecurityRulesClient.List")
130		defer func() {
131			sc := -1
132			if result.srlr.Response.Response != nil {
133				sc = result.srlr.Response.Response.StatusCode
134			}
135			tracing.EndSpan(ctx, sc, err)
136		}()
137	}
138	result.fn = client.listNextResults
139	req, err := client.ListPreparer(ctx, resourceGroupName, networkSecurityGroupName)
140	if err != nil {
141		err = autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "List", nil, "Failure preparing request")
142		return
143	}
144
145	resp, err := client.ListSender(req)
146	if err != nil {
147		result.srlr.Response = autorest.Response{Response: resp}
148		err = autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "List", resp, "Failure sending request")
149		return
150	}
151
152	result.srlr, err = client.ListResponder(resp)
153	if err != nil {
154		err = autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "List", resp, "Failure responding to request")
155	}
156
157	return
158}
159
160// ListPreparer prepares the List request.
161func (client DefaultSecurityRulesClient) ListPreparer(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (*http.Request, error) {
162	pathParameters := map[string]interface{}{
163		"networkSecurityGroupName": autorest.Encode("path", networkSecurityGroupName),
164		"resourceGroupName":        autorest.Encode("path", resourceGroupName),
165		"subscriptionId":           autorest.Encode("path", client.SubscriptionID),
166	}
167
168	const APIVersion = "2018-07-01"
169	queryParameters := map[string]interface{}{
170		"api-version": APIVersion,
171	}
172
173	preparer := autorest.CreatePreparer(
174		autorest.AsGet(),
175		autorest.WithBaseURL(client.BaseURI),
176		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/defaultSecurityRules", pathParameters),
177		autorest.WithQueryParameters(queryParameters))
178	return preparer.Prepare((&http.Request{}).WithContext(ctx))
179}
180
181// ListSender sends the List request. The method will close the
182// http.Response Body if it receives an error.
183func (client DefaultSecurityRulesClient) ListSender(req *http.Request) (*http.Response, error) {
184	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
185}
186
187// ListResponder handles the response to the List request. The method always
188// closes the http.Response Body.
189func (client DefaultSecurityRulesClient) ListResponder(resp *http.Response) (result SecurityRuleListResult, err error) {
190	err = autorest.Respond(
191		resp,
192		client.ByInspecting(),
193		azure.WithErrorUnlessStatusCode(http.StatusOK),
194		autorest.ByUnmarshallingJSON(&result),
195		autorest.ByClosing())
196	result.Response = autorest.Response{Response: resp}
197	return
198}
199
200// listNextResults retrieves the next set of results, if any.
201func (client DefaultSecurityRulesClient) listNextResults(ctx context.Context, lastResults SecurityRuleListResult) (result SecurityRuleListResult, err error) {
202	req, err := lastResults.securityRuleListResultPreparer(ctx)
203	if err != nil {
204		return result, autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "listNextResults", nil, "Failure preparing next results request")
205	}
206	if req == nil {
207		return
208	}
209	resp, err := client.ListSender(req)
210	if err != nil {
211		result.Response = autorest.Response{Response: resp}
212		return result, autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "listNextResults", resp, "Failure sending next results request")
213	}
214	result, err = client.ListResponder(resp)
215	if err != nil {
216		err = autorest.NewErrorWithError(err, "network.DefaultSecurityRulesClient", "listNextResults", resp, "Failure responding to next results request")
217	}
218	return
219}
220
221// ListComplete enumerates all values, automatically crossing page boundaries as required.
222func (client DefaultSecurityRulesClient) ListComplete(ctx context.Context, resourceGroupName string, networkSecurityGroupName string) (result SecurityRuleListResultIterator, err error) {
223	if tracing.IsEnabled() {
224		ctx = tracing.StartSpan(ctx, fqdn+"/DefaultSecurityRulesClient.List")
225		defer func() {
226			sc := -1
227			if result.Response().Response.Response != nil {
228				sc = result.page.Response().Response.Response.StatusCode
229			}
230			tracing.EndSpan(ctx, sc, err)
231		}()
232	}
233	result.page, err = client.List(ctx, resourceGroupName, networkSecurityGroupName)
234	return
235}
236