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