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// VirtualNetworkPeeringsClient is the network Client
19type VirtualNetworkPeeringsClient struct {
20	BaseClient
21}
22
23// NewVirtualNetworkPeeringsClient creates an instance of the VirtualNetworkPeeringsClient client.
24func NewVirtualNetworkPeeringsClient(subscriptionID string) VirtualNetworkPeeringsClient {
25	return NewVirtualNetworkPeeringsClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewVirtualNetworkPeeringsClientWithBaseURI creates an instance of the VirtualNetworkPeeringsClient client using a
29// custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds,
30// Azure stack).
31func NewVirtualNetworkPeeringsClientWithBaseURI(baseURI string, subscriptionID string) VirtualNetworkPeeringsClient {
32	return VirtualNetworkPeeringsClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// CreateOrUpdate creates or updates a peering in the specified virtual network.
36// Parameters:
37// resourceGroupName - the name of the resource group.
38// virtualNetworkName - the name of the virtual network.
39// virtualNetworkPeeringName - the name of the peering.
40// virtualNetworkPeeringParameters - parameters supplied to the create or update virtual network peering
41// operation.
42// syncRemoteAddressSpace - parameter indicates the intention to sync the peering with the current address
43// space on the remote vNet after it's updated.
44func (client VirtualNetworkPeeringsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, virtualNetworkName string, virtualNetworkPeeringName string, virtualNetworkPeeringParameters VirtualNetworkPeering, syncRemoteAddressSpace SyncRemoteAddressSpace) (result VirtualNetworkPeeringsCreateOrUpdateFuture, err error) {
45	if tracing.IsEnabled() {
46		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworkPeeringsClient.CreateOrUpdate")
47		defer func() {
48			sc := -1
49			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
50				sc = result.FutureAPI.Response().StatusCode
51			}
52			tracing.EndSpan(ctx, sc, err)
53		}()
54	}
55	if err := validation.Validate([]validation.Validation{
56		{TargetValue: virtualNetworkPeeringParameters,
57			Constraints: []validation.Constraint{{Target: "virtualNetworkPeeringParameters.VirtualNetworkPeeringPropertiesFormat", Name: validation.Null, Rule: false,
58				Chain: []validation.Constraint{{Target: "virtualNetworkPeeringParameters.VirtualNetworkPeeringPropertiesFormat.RemoteBgpCommunities", Name: validation.Null, Rule: false,
59					Chain: []validation.Constraint{{Target: "virtualNetworkPeeringParameters.VirtualNetworkPeeringPropertiesFormat.RemoteBgpCommunities.VirtualNetworkCommunity", Name: validation.Null, Rule: true, Chain: nil}}},
60				}}}}}); err != nil {
61		return result, validation.NewError("network.VirtualNetworkPeeringsClient", "CreateOrUpdate", err.Error())
62	}
63
64	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName, virtualNetworkPeeringParameters, syncRemoteAddressSpace)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "CreateOrUpdate", nil, "Failure preparing request")
67		return
68	}
69
70	result, err = client.CreateOrUpdateSender(req)
71	if err != nil {
72		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "CreateOrUpdate", nil, "Failure sending request")
73		return
74	}
75
76	return
77}
78
79// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
80func (client VirtualNetworkPeeringsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, virtualNetworkPeeringName string, virtualNetworkPeeringParameters VirtualNetworkPeering, syncRemoteAddressSpace SyncRemoteAddressSpace) (*http.Request, error) {
81	pathParameters := map[string]interface{}{
82		"resourceGroupName":         autorest.Encode("path", resourceGroupName),
83		"subscriptionId":            autorest.Encode("path", client.SubscriptionID),
84		"virtualNetworkName":        autorest.Encode("path", virtualNetworkName),
85		"virtualNetworkPeeringName": autorest.Encode("path", virtualNetworkPeeringName),
86	}
87
88	const APIVersion = "2021-02-01"
89	queryParameters := map[string]interface{}{
90		"api-version": APIVersion,
91	}
92	if len(string(syncRemoteAddressSpace)) > 0 {
93		queryParameters["syncRemoteAddressSpace"] = autorest.Encode("query", syncRemoteAddressSpace)
94	}
95
96	virtualNetworkPeeringParameters.Etag = nil
97	preparer := autorest.CreatePreparer(
98		autorest.AsContentType("application/json; charset=utf-8"),
99		autorest.AsPut(),
100		autorest.WithBaseURL(client.BaseURI),
101		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings/{virtualNetworkPeeringName}", pathParameters),
102		autorest.WithJSON(virtualNetworkPeeringParameters),
103		autorest.WithQueryParameters(queryParameters))
104	return preparer.Prepare((&http.Request{}).WithContext(ctx))
105}
106
107// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
108// http.Response Body if it receives an error.
109func (client VirtualNetworkPeeringsClient) CreateOrUpdateSender(req *http.Request) (future VirtualNetworkPeeringsCreateOrUpdateFuture, err error) {
110	var resp *http.Response
111	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
112	if err != nil {
113		return
114	}
115	var azf azure.Future
116	azf, err = azure.NewFutureFromResponse(resp)
117	future.FutureAPI = &azf
118	future.Result = future.result
119	return
120}
121
122// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
123// closes the http.Response Body.
124func (client VirtualNetworkPeeringsClient) CreateOrUpdateResponder(resp *http.Response) (result VirtualNetworkPeering, err error) {
125	err = autorest.Respond(
126		resp,
127		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
128		autorest.ByUnmarshallingJSON(&result),
129		autorest.ByClosing())
130	result.Response = autorest.Response{Response: resp}
131	return
132}
133
134// Delete deletes the specified virtual network peering.
135// Parameters:
136// resourceGroupName - the name of the resource group.
137// virtualNetworkName - the name of the virtual network.
138// virtualNetworkPeeringName - the name of the virtual network peering.
139func (client VirtualNetworkPeeringsClient) Delete(ctx context.Context, resourceGroupName string, virtualNetworkName string, virtualNetworkPeeringName string) (result VirtualNetworkPeeringsDeleteFuture, err error) {
140	if tracing.IsEnabled() {
141		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworkPeeringsClient.Delete")
142		defer func() {
143			sc := -1
144			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
145				sc = result.FutureAPI.Response().StatusCode
146			}
147			tracing.EndSpan(ctx, sc, err)
148		}()
149	}
150	req, err := client.DeletePreparer(ctx, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName)
151	if err != nil {
152		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "Delete", nil, "Failure preparing request")
153		return
154	}
155
156	result, err = client.DeleteSender(req)
157	if err != nil {
158		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "Delete", nil, "Failure sending request")
159		return
160	}
161
162	return
163}
164
165// DeletePreparer prepares the Delete request.
166func (client VirtualNetworkPeeringsClient) DeletePreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, virtualNetworkPeeringName string) (*http.Request, error) {
167	pathParameters := map[string]interface{}{
168		"resourceGroupName":         autorest.Encode("path", resourceGroupName),
169		"subscriptionId":            autorest.Encode("path", client.SubscriptionID),
170		"virtualNetworkName":        autorest.Encode("path", virtualNetworkName),
171		"virtualNetworkPeeringName": autorest.Encode("path", virtualNetworkPeeringName),
172	}
173
174	const APIVersion = "2021-02-01"
175	queryParameters := map[string]interface{}{
176		"api-version": APIVersion,
177	}
178
179	preparer := autorest.CreatePreparer(
180		autorest.AsDelete(),
181		autorest.WithBaseURL(client.BaseURI),
182		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings/{virtualNetworkPeeringName}", pathParameters),
183		autorest.WithQueryParameters(queryParameters))
184	return preparer.Prepare((&http.Request{}).WithContext(ctx))
185}
186
187// DeleteSender sends the Delete request. The method will close the
188// http.Response Body if it receives an error.
189func (client VirtualNetworkPeeringsClient) DeleteSender(req *http.Request) (future VirtualNetworkPeeringsDeleteFuture, err error) {
190	var resp *http.Response
191	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
192	if err != nil {
193		return
194	}
195	var azf azure.Future
196	azf, err = azure.NewFutureFromResponse(resp)
197	future.FutureAPI = &azf
198	future.Result = future.result
199	return
200}
201
202// DeleteResponder handles the response to the Delete request. The method always
203// closes the http.Response Body.
204func (client VirtualNetworkPeeringsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
205	err = autorest.Respond(
206		resp,
207		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
208		autorest.ByClosing())
209	result.Response = resp
210	return
211}
212
213// Get gets the specified virtual network peering.
214// Parameters:
215// resourceGroupName - the name of the resource group.
216// virtualNetworkName - the name of the virtual network.
217// virtualNetworkPeeringName - the name of the virtual network peering.
218func (client VirtualNetworkPeeringsClient) Get(ctx context.Context, resourceGroupName string, virtualNetworkName string, virtualNetworkPeeringName string) (result VirtualNetworkPeering, err error) {
219	if tracing.IsEnabled() {
220		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworkPeeringsClient.Get")
221		defer func() {
222			sc := -1
223			if result.Response.Response != nil {
224				sc = result.Response.Response.StatusCode
225			}
226			tracing.EndSpan(ctx, sc, err)
227		}()
228	}
229	req, err := client.GetPreparer(ctx, resourceGroupName, virtualNetworkName, virtualNetworkPeeringName)
230	if err != nil {
231		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "Get", nil, "Failure preparing request")
232		return
233	}
234
235	resp, err := client.GetSender(req)
236	if err != nil {
237		result.Response = autorest.Response{Response: resp}
238		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "Get", resp, "Failure sending request")
239		return
240	}
241
242	result, err = client.GetResponder(resp)
243	if err != nil {
244		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "Get", resp, "Failure responding to request")
245		return
246	}
247
248	return
249}
250
251// GetPreparer prepares the Get request.
252func (client VirtualNetworkPeeringsClient) GetPreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string, virtualNetworkPeeringName string) (*http.Request, error) {
253	pathParameters := map[string]interface{}{
254		"resourceGroupName":         autorest.Encode("path", resourceGroupName),
255		"subscriptionId":            autorest.Encode("path", client.SubscriptionID),
256		"virtualNetworkName":        autorest.Encode("path", virtualNetworkName),
257		"virtualNetworkPeeringName": autorest.Encode("path", virtualNetworkPeeringName),
258	}
259
260	const APIVersion = "2021-02-01"
261	queryParameters := map[string]interface{}{
262		"api-version": APIVersion,
263	}
264
265	preparer := autorest.CreatePreparer(
266		autorest.AsGet(),
267		autorest.WithBaseURL(client.BaseURI),
268		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings/{virtualNetworkPeeringName}", pathParameters),
269		autorest.WithQueryParameters(queryParameters))
270	return preparer.Prepare((&http.Request{}).WithContext(ctx))
271}
272
273// GetSender sends the Get request. The method will close the
274// http.Response Body if it receives an error.
275func (client VirtualNetworkPeeringsClient) GetSender(req *http.Request) (*http.Response, error) {
276	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
277}
278
279// GetResponder handles the response to the Get request. The method always
280// closes the http.Response Body.
281func (client VirtualNetworkPeeringsClient) GetResponder(resp *http.Response) (result VirtualNetworkPeering, err error) {
282	err = autorest.Respond(
283		resp,
284		azure.WithErrorUnlessStatusCode(http.StatusOK),
285		autorest.ByUnmarshallingJSON(&result),
286		autorest.ByClosing())
287	result.Response = autorest.Response{Response: resp}
288	return
289}
290
291// List gets all virtual network peerings in a virtual network.
292// Parameters:
293// resourceGroupName - the name of the resource group.
294// virtualNetworkName - the name of the virtual network.
295func (client VirtualNetworkPeeringsClient) List(ctx context.Context, resourceGroupName string, virtualNetworkName string) (result VirtualNetworkPeeringListResultPage, err error) {
296	if tracing.IsEnabled() {
297		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworkPeeringsClient.List")
298		defer func() {
299			sc := -1
300			if result.vnplr.Response.Response != nil {
301				sc = result.vnplr.Response.Response.StatusCode
302			}
303			tracing.EndSpan(ctx, sc, err)
304		}()
305	}
306	result.fn = client.listNextResults
307	req, err := client.ListPreparer(ctx, resourceGroupName, virtualNetworkName)
308	if err != nil {
309		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "List", nil, "Failure preparing request")
310		return
311	}
312
313	resp, err := client.ListSender(req)
314	if err != nil {
315		result.vnplr.Response = autorest.Response{Response: resp}
316		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "List", resp, "Failure sending request")
317		return
318	}
319
320	result.vnplr, err = client.ListResponder(resp)
321	if err != nil {
322		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "List", resp, "Failure responding to request")
323		return
324	}
325	if result.vnplr.hasNextLink() && result.vnplr.IsEmpty() {
326		err = result.NextWithContext(ctx)
327		return
328	}
329
330	return
331}
332
333// ListPreparer prepares the List request.
334func (client VirtualNetworkPeeringsClient) ListPreparer(ctx context.Context, resourceGroupName string, virtualNetworkName string) (*http.Request, error) {
335	pathParameters := map[string]interface{}{
336		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
337		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
338		"virtualNetworkName": autorest.Encode("path", virtualNetworkName),
339	}
340
341	const APIVersion = "2021-02-01"
342	queryParameters := map[string]interface{}{
343		"api-version": APIVersion,
344	}
345
346	preparer := autorest.CreatePreparer(
347		autorest.AsGet(),
348		autorest.WithBaseURL(client.BaseURI),
349		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/virtualNetworkPeerings", pathParameters),
350		autorest.WithQueryParameters(queryParameters))
351	return preparer.Prepare((&http.Request{}).WithContext(ctx))
352}
353
354// ListSender sends the List request. The method will close the
355// http.Response Body if it receives an error.
356func (client VirtualNetworkPeeringsClient) ListSender(req *http.Request) (*http.Response, error) {
357	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
358}
359
360// ListResponder handles the response to the List request. The method always
361// closes the http.Response Body.
362func (client VirtualNetworkPeeringsClient) ListResponder(resp *http.Response) (result VirtualNetworkPeeringListResult, err error) {
363	err = autorest.Respond(
364		resp,
365		azure.WithErrorUnlessStatusCode(http.StatusOK),
366		autorest.ByUnmarshallingJSON(&result),
367		autorest.ByClosing())
368	result.Response = autorest.Response{Response: resp}
369	return
370}
371
372// listNextResults retrieves the next set of results, if any.
373func (client VirtualNetworkPeeringsClient) listNextResults(ctx context.Context, lastResults VirtualNetworkPeeringListResult) (result VirtualNetworkPeeringListResult, err error) {
374	req, err := lastResults.virtualNetworkPeeringListResultPreparer(ctx)
375	if err != nil {
376		return result, autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "listNextResults", nil, "Failure preparing next results request")
377	}
378	if req == nil {
379		return
380	}
381	resp, err := client.ListSender(req)
382	if err != nil {
383		result.Response = autorest.Response{Response: resp}
384		return result, autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "listNextResults", resp, "Failure sending next results request")
385	}
386	result, err = client.ListResponder(resp)
387	if err != nil {
388		err = autorest.NewErrorWithError(err, "network.VirtualNetworkPeeringsClient", "listNextResults", resp, "Failure responding to next results request")
389	}
390	return
391}
392
393// ListComplete enumerates all values, automatically crossing page boundaries as required.
394func (client VirtualNetworkPeeringsClient) ListComplete(ctx context.Context, resourceGroupName string, virtualNetworkName string) (result VirtualNetworkPeeringListResultIterator, err error) {
395	if tracing.IsEnabled() {
396		ctx = tracing.StartSpan(ctx, fqdn+"/VirtualNetworkPeeringsClient.List")
397		defer func() {
398			sc := -1
399			if result.Response().Response.Response != nil {
400				sc = result.page.Response().Response.Response.StatusCode
401			}
402			tracing.EndSpan(ctx, sc, err)
403		}()
404	}
405	result.page, err = client.List(ctx, resourceGroupName, virtualNetworkName)
406	return
407}
408