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// VirtualNetworksClient is the network Client
18type VirtualNetworksClient struct {
19	BaseClient
20}
21
22// NewVirtualNetworksClient creates an instance of the VirtualNetworksClient client.
23func NewVirtualNetworksClient(subscriptionID string) VirtualNetworksClient {
24	return NewVirtualNetworksClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewVirtualNetworksClientWithBaseURI creates an instance of the VirtualNetworksClient 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 NewVirtualNetworksClientWithBaseURI(baseURI string, subscriptionID string) VirtualNetworksClient {
30	return VirtualNetworksClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// CreateOrUpdate creates or updates a virtual network in the specified resource group.
34// Parameters:
35// resourceGroupName - the name of the resource group.
36// virtualNetworkName - the name of the virtual network.
37// parameters - parameters supplied to the create or update virtual network operation
38func (client VirtualNetworksClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, virtualNetworkName string, parameters VirtualNetwork) (result VirtualNetworksCreateOrUpdateFuture, err error) {
39	if tracing.IsEnabled() {
40		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworksClient.CreateOrUpdate")
41		defer func() {
42			sc := -1
43			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
44				sc = result.FutureAPI.Response().StatusCode
45			}
46			tracing.EndSpan(ctx, sc, err)
47		}()
48	}
49	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, virtualNetworkName, parameters)
50	if err != nil {
51		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "CreateOrUpdate", nil, "Failure preparing request")
52		return
53	}
54
55	result, err = client.CreateOrUpdateSender(req)
56	if err != nil {
57		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "CreateOrUpdate", nil, "Failure sending request")
58		return
59	}
60
61	return
62}
63
64// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
65func (client VirtualNetworksClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, parameters VirtualNetwork) (*http.Request, error) {
66	pathParameters := map[string]interface{}{
67		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
68		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
69		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
70	}
71
72	const APIVersion = "2015-06-15"
73	queryParameters := map[string]interface{}{
74		"api-version": APIVersion,
75	}
76
77	preparer := autorest.CreatePreparer(
78		autorest.AsContentType("application/json; charset=utf-8"),
79		autorest.AsPut(),
80		autorest.WithBaseURL(client.BaseURI),
81		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}", pathParameters),
82		autorest.WithJSON(parameters),
83		autorest.WithQueryParameters(queryParameters))
84	return preparer.Prepare((&http.Request{}).WithContext(ctx))
85}
86
87// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
88// http.Response Body if it receives an error.
89func (client VirtualNetworksClient) CreateOrUpdateSender(req *http.Request) (future VirtualNetworksCreateOrUpdateFuture, err error) {
90	var resp *http.Response
91	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
92	if err != nil {
93		return
94	}
95	var azf azure.Future
96	azf, err = azure.NewFutureFromResponse(resp)
97	future.FutureAPI = &azf
98	future.Result = future.result
99	return
100}
101
102// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
103// closes the http.Response Body.
104func (client VirtualNetworksClient) CreateOrUpdateResponder(resp *http.Response) (result VirtualNetwork, err error) {
105	err = autorest.Respond(
106		resp,
107		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
108		autorest.ByUnmarshallingJSON(&result),
109		autorest.ByClosing())
110	result.Response = autorest.Response{Response: resp}
111	return
112}
113
114// Delete deletes the specified virtual network.
115// Parameters:
116// resourceGroupName - the name of the resource group.
117// virtualNetworkName - the name of the virtual network.
118func (client VirtualNetworksClient) Delete(ctx context.Context, resourceGroupName string, virtualNetworkName string) (result VirtualNetworksDeleteFuture, err error) {
119	if tracing.IsEnabled() {
120		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworksClient.Delete")
121		defer func() {
122			sc := -1
123			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
124				sc = result.FutureAPI.Response().StatusCode
125			}
126			tracing.EndSpan(ctx, sc, err)
127		}()
128	}
129	req, err := client.DeletePreparer(ctx, resourceGroupName, virtualNetworkName)
130	if err != nil {
131		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "Delete", nil, "Failure preparing request")
132		return
133	}
134
135	result, err = client.DeleteSender(req)
136	if err != nil {
137		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "Delete", nil, "Failure sending request")
138		return
139	}
140
141	return
142}
143
144// DeletePreparer prepares the Delete request.
145func (client VirtualNetworksClient) DeletePreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string) (*http.Request, error) {
146	pathParameters := map[string]interface{}{
147		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
148		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
149		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
150	}
151
152	const APIVersion = "2015-06-15"
153	queryParameters := map[string]interface{}{
154		"api-version": APIVersion,
155	}
156
157	preparer := autorest.CreatePreparer(
158		autorest.AsDelete(),
159		autorest.WithBaseURL(client.BaseURI),
160		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}", pathParameters),
161		autorest.WithQueryParameters(queryParameters))
162	return preparer.Prepare((&http.Request{}).WithContext(ctx))
163}
164
165// DeleteSender sends the Delete request. The method will close the
166// http.Response Body if it receives an error.
167func (client VirtualNetworksClient) DeleteSender(req *http.Request) (future VirtualNetworksDeleteFuture, err error) {
168	var resp *http.Response
169	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
170	if err != nil {
171		return
172	}
173	var azf azure.Future
174	azf, err = azure.NewFutureFromResponse(resp)
175	future.FutureAPI = &azf
176	future.Result = future.result
177	return
178}
179
180// DeleteResponder handles the response to the Delete request. The method always
181// closes the http.Response Body.
182func (client VirtualNetworksClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
183	err = autorest.Respond(
184		resp,
185		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
186		autorest.ByClosing())
187	result.Response = resp
188	return
189}
190
191// Get gets the specified virtual network by resource group.
192// Parameters:
193// resourceGroupName - the name of the resource group.
194// virtualNetworkName - the name of the virtual network.
195// expand - expands referenced resources.
196func (client VirtualNetworksClient) Get(ctx context.Context, resourceGroupName string, virtualNetworkName string, expand string) (result VirtualNetwork, err error) {
197	if tracing.IsEnabled() {
198		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworksClient.Get")
199		defer func() {
200			sc := -1
201			if result.Response.Response != nil {
202				sc = result.Response.Response.StatusCode
203			}
204			tracing.EndSpan(ctx, sc, err)
205		}()
206	}
207	req, err := client.GetPreparer(ctx, resourceGroupName, virtualNetworkName, expand)
208	if err != nil {
209		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "Get", nil, "Failure preparing request")
210		return
211	}
212
213	resp, err := client.GetSender(req)
214	if err != nil {
215		result.Response = autorest.Response{Response: resp}
216		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "Get", resp, "Failure sending request")
217		return
218	}
219
220	result, err = client.GetResponder(resp)
221	if err != nil {
222		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "Get", resp, "Failure responding to request")
223		return
224	}
225
226	return
227}
228
229// GetPreparer prepares the Get request.
230func (client VirtualNetworksClient) GetPreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, expand string) (*http.Request, error) {
231	pathParameters := map[string]interface{}{
232		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
233		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
234		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
235	}
236
237	const APIVersion = "2015-06-15"
238	queryParameters := map[string]interface{}{
239		"api-version": APIVersion,
240	}
241	if len(expand) > 0 {
242		queryParameters["$expand"] = autorest.Encode("query", expand)
243	}
244
245	preparer := autorest.CreatePreparer(
246		autorest.AsGet(),
247		autorest.WithBaseURL(client.BaseURI),
248		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}", pathParameters),
249		autorest.WithQueryParameters(queryParameters))
250	return preparer.Prepare((&http.Request{}).WithContext(ctx))
251}
252
253// GetSender sends the Get request. The method will close the
254// http.Response Body if it receives an error.
255func (client VirtualNetworksClient) GetSender(req *http.Request) (*http.Response, error) {
256	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
257}
258
259// GetResponder handles the response to the Get request. The method always
260// closes the http.Response Body.
261func (client VirtualNetworksClient) GetResponder(resp *http.Response) (result VirtualNetwork, err error) {
262	err = autorest.Respond(
263		resp,
264		azure.WithErrorUnlessStatusCode(http.StatusOK),
265		autorest.ByUnmarshallingJSON(&result),
266		autorest.ByClosing())
267	result.Response = autorest.Response{Response: resp}
268	return
269}
270
271// List gets all virtual networks in a resource group.
272// Parameters:
273// resourceGroupName - the name of the resource group.
274func (client VirtualNetworksClient) List(ctx context.Context, resourceGroupName string) (result VirtualNetworkListResultPage, err error) {
275	if tracing.IsEnabled() {
276		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworksClient.List")
277		defer func() {
278			sc := -1
279			if result.vnlr.Response.Response != nil {
280				sc = result.vnlr.Response.Response.StatusCode
281			}
282			tracing.EndSpan(ctx, sc, err)
283		}()
284	}
285	result.fn = client.listNextResults
286	req, err := client.ListPreparer(ctx, resourceGroupName)
287	if err != nil {
288		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "List", nil, "Failure preparing request")
289		return
290	}
291
292	resp, err := client.ListSender(req)
293	if err != nil {
294		result.vnlr.Response = autorest.Response{Response: resp}
295		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "List", resp, "Failure sending request")
296		return
297	}
298
299	result.vnlr, err = client.ListResponder(resp)
300	if err != nil {
301		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "List", resp, "Failure responding to request")
302		return
303	}
304	if result.vnlr.hasNextLink() && result.vnlr.IsEmpty() {
305		err = result.NextWithContext(ctx)
306		return
307	}
308
309	return
310}
311
312// ListPreparer prepares the List request.
313func (client VirtualNetworksClient) ListPreparer(ctx context.Context, resourceGroupName string) (*http.Request, error) {
314	pathParameters := map[string]interface{}{
315		"resourceGroupName": autorest.Encode("path", resourceGroupName),
316		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
317	}
318
319	const APIVersion = "2015-06-15"
320	queryParameters := map[string]interface{}{
321		"api-version": APIVersion,
322	}
323
324	preparer := autorest.CreatePreparer(
325		autorest.AsGet(),
326		autorest.WithBaseURL(client.BaseURI),
327		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualnetworks", pathParameters),
328		autorest.WithQueryParameters(queryParameters))
329	return preparer.Prepare((&http.Request{}).WithContext(ctx))
330}
331
332// ListSender sends the List request. The method will close the
333// http.Response Body if it receives an error.
334func (client VirtualNetworksClient) ListSender(req *http.Request) (*http.Response, error) {
335	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
336}
337
338// ListResponder handles the response to the List request. The method always
339// closes the http.Response Body.
340func (client VirtualNetworksClient) ListResponder(resp *http.Response) (result VirtualNetworkListResult, err error) {
341	err = autorest.Respond(
342		resp,
343		azure.WithErrorUnlessStatusCode(http.StatusOK),
344		autorest.ByUnmarshallingJSON(&result),
345		autorest.ByClosing())
346	result.Response = autorest.Response{Response: resp}
347	return
348}
349
350// listNextResults retrieves the next set of results, if any.
351func (client VirtualNetworksClient) listNextResults(ctx context.Context, lastResults VirtualNetworkListResult) (result VirtualNetworkListResult, err error) {
352	req, err := lastResults.virtualNetworkListResultPreparer(ctx)
353	if err != nil {
354		return result, autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "listNextResults", nil, "Failure preparing next results request")
355	}
356	if req == nil {
357		return
358	}
359	resp, err := client.ListSender(req)
360	if err != nil {
361		result.Response = autorest.Response{Response: resp}
362		return result, autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "listNextResults", resp, "Failure sending next results request")
363	}
364	result, err = client.ListResponder(resp)
365	if err != nil {
366		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "listNextResults", resp, "Failure responding to next results request")
367	}
368	return
369}
370
371// ListComplete enumerates all values, automatically crossing page boundaries as required.
372func (client VirtualNetworksClient) ListComplete(ctx context.Context, resourceGroupName string) (result VirtualNetworkListResultIterator, err error) {
373	if tracing.IsEnabled() {
374		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworksClient.List")
375		defer func() {
376			sc := -1
377			if result.Response().Response.Response != nil {
378				sc = result.page.Response().Response.Response.StatusCode
379			}
380			tracing.EndSpan(ctx, sc, err)
381		}()
382	}
383	result.page, err = client.List(ctx, resourceGroupName)
384	return
385}
386
387// ListAll gets all virtual networks in a subscription.
388func (client VirtualNetworksClient) ListAll(ctx context.Context) (result VirtualNetworkListResultPage, err error) {
389	if tracing.IsEnabled() {
390		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworksClient.ListAll")
391		defer func() {
392			sc := -1
393			if result.vnlr.Response.Response != nil {
394				sc = result.vnlr.Response.Response.StatusCode
395			}
396			tracing.EndSpan(ctx, sc, err)
397		}()
398	}
399	result.fn = client.listAllNextResults
400	req, err := client.ListAllPreparer(ctx)
401	if err != nil {
402		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "ListAll", nil, "Failure preparing request")
403		return
404	}
405
406	resp, err := client.ListAllSender(req)
407	if err != nil {
408		result.vnlr.Response = autorest.Response{Response: resp}
409		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "ListAll", resp, "Failure sending request")
410		return
411	}
412
413	result.vnlr, err = client.ListAllResponder(resp)
414	if err != nil {
415		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "ListAll", resp, "Failure responding to request")
416		return
417	}
418	if result.vnlr.hasNextLink() && result.vnlr.IsEmpty() {
419		err = result.NextWithContext(ctx)
420		return
421	}
422
423	return
424}
425
426// ListAllPreparer prepares the ListAll request.
427func (client VirtualNetworksClient) ListAllPreparer(ctx context.Context) (*http.Request, error) {
428	pathParameters := map[string]interface{}{
429		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
430	}
431
432	const APIVersion = "2015-06-15"
433	queryParameters := map[string]interface{}{
434		"api-version": APIVersion,
435	}
436
437	preparer := autorest.CreatePreparer(
438		autorest.AsGet(),
439		autorest.WithBaseURL(client.BaseURI),
440		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Network/virtualnetworks", pathParameters),
441		autorest.WithQueryParameters(queryParameters))
442	return preparer.Prepare((&http.Request{}).WithContext(ctx))
443}
444
445// ListAllSender sends the ListAll request. The method will close the
446// http.Response Body if it receives an error.
447func (client VirtualNetworksClient) ListAllSender(req *http.Request) (*http.Response, error) {
448	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
449}
450
451// ListAllResponder handles the response to the ListAll request. The method always
452// closes the http.Response Body.
453func (client VirtualNetworksClient) ListAllResponder(resp *http.Response) (result VirtualNetworkListResult, err error) {
454	err = autorest.Respond(
455		resp,
456		azure.WithErrorUnlessStatusCode(http.StatusOK),
457		autorest.ByUnmarshallingJSON(&result),
458		autorest.ByClosing())
459	result.Response = autorest.Response{Response: resp}
460	return
461}
462
463// listAllNextResults retrieves the next set of results, if any.
464func (client VirtualNetworksClient) listAllNextResults(ctx context.Context, lastResults VirtualNetworkListResult) (result VirtualNetworkListResult, err error) {
465	req, err := lastResults.virtualNetworkListResultPreparer(ctx)
466	if err != nil {
467		return result, autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "listAllNextResults", nil, "Failure preparing next results request")
468	}
469	if req == nil {
470		return
471	}
472	resp, err := client.ListAllSender(req)
473	if err != nil {
474		result.Response = autorest.Response{Response: resp}
475		return result, autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "listAllNextResults", resp, "Failure sending next results request")
476	}
477	result, err = client.ListAllResponder(resp)
478	if err != nil {
479		err = autorest.NewErrorWithError(err, "network.VirtualNetworksClient", "listAllNextResults", resp, "Failure responding to next results request")
480	}
481	return
482}
483
484// ListAllComplete enumerates all values, automatically crossing page boundaries as required.
485func (client VirtualNetworksClient) ListAllComplete(ctx context.Context) (result VirtualNetworkListResultIterator, err error) {
486	if tracing.IsEnabled() {
487		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworksClient.ListAll")
488		defer func() {
489			sc := -1
490			if result.Response().Response.Response != nil {
491				sc = result.page.Response().Response.Response.StatusCode
492			}
493			tracing.EndSpan(ctx, sc, err)
494		}()
495	}
496	result.page, err = client.ListAll(ctx)
497	return
498}
499