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// SubnetsClient is the network Client
29type SubnetsClient struct {
30	BaseClient
31}
32
33// NewSubnetsClient creates an instance of the SubnetsClient client.
34func NewSubnetsClient(subscriptionID string) SubnetsClient {
35	return NewSubnetsClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewSubnetsClientWithBaseURI creates an instance of the SubnetsClient client.
39func NewSubnetsClientWithBaseURI(baseURI string, subscriptionID string) SubnetsClient {
40	return SubnetsClient{NewWithBaseURI(baseURI, subscriptionID)}
41}
42
43// CreateOrUpdate creates or updates a subnet in the specified virtual network.
44// Parameters:
45// resourceGroupName - the name of the resource group.
46// virtualNetworkName - the name of the virtual network.
47// subnetName - the name of the subnet.
48// subnetParameters - parameters supplied to the create or update subnet operation.
49func (client SubnetsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, virtualNetworkName string, subnetName string, subnetParameters Subnet) (result SubnetsCreateOrUpdateFuture, err error) {
50	if tracing.IsEnabled() {
51		ctx = tracing.StartSpan(ctx, fqdn+"/SubnetsClient.CreateOrUpdate")
52		defer func() {
53			sc := -1
54			if result.Response() != nil {
55				sc = result.Response().StatusCode
56			}
57			tracing.EndSpan(ctx, sc, err)
58		}()
59	}
60	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, virtualNetworkName, subnetName, subnetParameters)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "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.SubnetsClient", "CreateOrUpdate", result.Response(), "Failure sending request")
69		return
70	}
71
72	return
73}
74
75// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
76func (client SubnetsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, subnetName string, subnetParameters Subnet) (*http.Request, error) {
77	pathParameters := map[string]interface{}{
78		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
79		"subnetName":         autorest.Encode("path", subnetName),
80		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
81		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
82	}
83
84	const APIVersion = "2018-01-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/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}", pathParameters),
94		autorest.WithJSON(subnetParameters),
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 SubnetsClient) CreateOrUpdateSender(req *http.Request) (future SubnetsCreateOrUpdateFuture, err error) {
102	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
103	var resp *http.Response
104	resp, err = autorest.SendWithSender(client, req, sd...)
105	if err != nil {
106		return
107	}
108	future.Future, err = azure.NewFutureFromResponse(resp)
109	return
110}
111
112// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
113// closes the http.Response Body.
114func (client SubnetsClient) CreateOrUpdateResponder(resp *http.Response) (result Subnet, err error) {
115	err = autorest.Respond(
116		resp,
117		client.ByInspecting(),
118		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
119		autorest.ByUnmarshallingJSON(&result),
120		autorest.ByClosing())
121	result.Response = autorest.Response{Response: resp}
122	return
123}
124
125// Delete deletes the specified subnet.
126// Parameters:
127// resourceGroupName - the name of the resource group.
128// virtualNetworkName - the name of the virtual network.
129// subnetName - the name of the subnet.
130func (client SubnetsClient) Delete(ctx context.Context, resourceGroupName string, virtualNetworkName string, subnetName string) (result SubnetsDeleteFuture, err error) {
131	if tracing.IsEnabled() {
132		ctx = tracing.StartSpan(ctx, fqdn+"/SubnetsClient.Delete")
133		defer func() {
134			sc := -1
135			if result.Response() != nil {
136				sc = result.Response().StatusCode
137			}
138			tracing.EndSpan(ctx, sc, err)
139		}()
140	}
141	req, err := client.DeletePreparer(ctx, resourceGroupName, virtualNetworkName, subnetName)
142	if err != nil {
143		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "Delete", nil, "Failure preparing request")
144		return
145	}
146
147	result, err = client.DeleteSender(req)
148	if err != nil {
149		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "Delete", result.Response(), "Failure sending request")
150		return
151	}
152
153	return
154}
155
156// DeletePreparer prepares the Delete request.
157func (client SubnetsClient) DeletePreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, subnetName string) (*http.Request, error) {
158	pathParameters := map[string]interface{}{
159		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
160		"subnetName":         autorest.Encode("path", subnetName),
161		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
162		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
163	}
164
165	const APIVersion = "2018-01-01"
166	queryParameters := map[string]interface{}{
167		"api-version": APIVersion,
168	}
169
170	preparer := autorest.CreatePreparer(
171		autorest.AsDelete(),
172		autorest.WithBaseURL(client.BaseURI),
173		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}", pathParameters),
174		autorest.WithQueryParameters(queryParameters))
175	return preparer.Prepare((&http.Request{}).WithContext(ctx))
176}
177
178// DeleteSender sends the Delete request. The method will close the
179// http.Response Body if it receives an error.
180func (client SubnetsClient) DeleteSender(req *http.Request) (future SubnetsDeleteFuture, err error) {
181	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
182	var resp *http.Response
183	resp, err = autorest.SendWithSender(client, req, sd...)
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 SubnetsClient) 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 gets the specified subnet by virtual network and resource group.
204// Parameters:
205// resourceGroupName - the name of the resource group.
206// virtualNetworkName - the name of the virtual network.
207// subnetName - the name of the subnet.
208// expand - expands referenced resources.
209func (client SubnetsClient) Get(ctx context.Context, resourceGroupName string, virtualNetworkName string, subnetName string, expand string) (result Subnet, err error) {
210	if tracing.IsEnabled() {
211		ctx = tracing.StartSpan(ctx, fqdn+"/SubnetsClient.Get")
212		defer func() {
213			sc := -1
214			if result.Response.Response != nil {
215				sc = result.Response.Response.StatusCode
216			}
217			tracing.EndSpan(ctx, sc, err)
218		}()
219	}
220	req, err := client.GetPreparer(ctx, resourceGroupName, virtualNetworkName, subnetName, expand)
221	if err != nil {
222		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "Get", nil, "Failure preparing request")
223		return
224	}
225
226	resp, err := client.GetSender(req)
227	if err != nil {
228		result.Response = autorest.Response{Response: resp}
229		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "Get", resp, "Failure sending request")
230		return
231	}
232
233	result, err = client.GetResponder(resp)
234	if err != nil {
235		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "Get", resp, "Failure responding to request")
236	}
237
238	return
239}
240
241// GetPreparer prepares the Get request.
242func (client SubnetsClient) GetPreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, subnetName string, expand string) (*http.Request, error) {
243	pathParameters := map[string]interface{}{
244		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
245		"subnetName":         autorest.Encode("path", subnetName),
246		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
247		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
248	}
249
250	const APIVersion = "2018-01-01"
251	queryParameters := map[string]interface{}{
252		"api-version": APIVersion,
253	}
254	if len(expand) > 0 {
255		queryParameters["$expand"] = autorest.Encode("query", expand)
256	}
257
258	preparer := autorest.CreatePreparer(
259		autorest.AsGet(),
260		autorest.WithBaseURL(client.BaseURI),
261		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}", pathParameters),
262		autorest.WithQueryParameters(queryParameters))
263	return preparer.Prepare((&http.Request{}).WithContext(ctx))
264}
265
266// GetSender sends the Get request. The method will close the
267// http.Response Body if it receives an error.
268func (client SubnetsClient) GetSender(req *http.Request) (*http.Response, error) {
269	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
270	return autorest.SendWithSender(client, req, sd...)
271}
272
273// GetResponder handles the response to the Get request. The method always
274// closes the http.Response Body.
275func (client SubnetsClient) GetResponder(resp *http.Response) (result Subnet, err error) {
276	err = autorest.Respond(
277		resp,
278		client.ByInspecting(),
279		azure.WithErrorUnlessStatusCode(http.StatusOK),
280		autorest.ByUnmarshallingJSON(&result),
281		autorest.ByClosing())
282	result.Response = autorest.Response{Response: resp}
283	return
284}
285
286// List gets all subnets in a virtual network.
287// Parameters:
288// resourceGroupName - the name of the resource group.
289// virtualNetworkName - the name of the virtual network.
290func (client SubnetsClient) List(ctx context.Context, resourceGroupName string, virtualNetworkName string) (result SubnetListResultPage, err error) {
291	if tracing.IsEnabled() {
292		ctx = tracing.StartSpan(ctx, fqdn+"/SubnetsClient.List")
293		defer func() {
294			sc := -1
295			if result.slr.Response.Response != nil {
296				sc = result.slr.Response.Response.StatusCode
297			}
298			tracing.EndSpan(ctx, sc, err)
299		}()
300	}
301	result.fn = client.listNextResults
302	req, err := client.ListPreparer(ctx, resourceGroupName, virtualNetworkName)
303	if err != nil {
304		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "List", nil, "Failure preparing request")
305		return
306	}
307
308	resp, err := client.ListSender(req)
309	if err != nil {
310		result.slr.Response = autorest.Response{Response: resp}
311		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "List", resp, "Failure sending request")
312		return
313	}
314
315	result.slr, err = client.ListResponder(resp)
316	if err != nil {
317		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "List", resp, "Failure responding to request")
318	}
319
320	return
321}
322
323// ListPreparer prepares the List request.
324func (client SubnetsClient) ListPreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string) (*http.Request, error) {
325	pathParameters := map[string]interface{}{
326		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
327		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
328		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
329	}
330
331	const APIVersion = "2018-01-01"
332	queryParameters := map[string]interface{}{
333		"api-version": APIVersion,
334	}
335
336	preparer := autorest.CreatePreparer(
337		autorest.AsGet(),
338		autorest.WithBaseURL(client.BaseURI),
339		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets", pathParameters),
340		autorest.WithQueryParameters(queryParameters))
341	return preparer.Prepare((&http.Request{}).WithContext(ctx))
342}
343
344// ListSender sends the List request. The method will close the
345// http.Response Body if it receives an error.
346func (client SubnetsClient) ListSender(req *http.Request) (*http.Response, error) {
347	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
348	return autorest.SendWithSender(client, req, sd...)
349}
350
351// ListResponder handles the response to the List request. The method always
352// closes the http.Response Body.
353func (client SubnetsClient) ListResponder(resp *http.Response) (result SubnetListResult, err error) {
354	err = autorest.Respond(
355		resp,
356		client.ByInspecting(),
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 SubnetsClient) listNextResults(ctx context.Context, lastResults SubnetListResult) (result SubnetListResult, err error) {
366	req, err := lastResults.subnetListResultPreparer(ctx)
367	if err != nil {
368		return result, autorest.NewErrorWithError(err, "network.SubnetsClient", "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.SubnetsClient", "listNextResults", resp, "Failure sending next results request")
377	}
378	result, err = client.ListResponder(resp)
379	if err != nil {
380		err = autorest.NewErrorWithError(err, "network.SubnetsClient", "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 SubnetsClient) ListComplete(ctx context.Context, resourceGroupName string, virtualNetworkName string) (result SubnetListResultIterator, err error) {
387	if tracing.IsEnabled() {
388		ctx = tracing.StartSpan(ctx, fqdn+"/SubnetsClient.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, virtualNetworkName)
398	return
399}
400