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", result.Response(), "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 = "2020-03-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	future.Future, err = azure.NewFutureFromResponse(resp)
125	return
126}
127
128// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
129// closes the http.Response Body.
130func (client ExpressRouteCrossConnectionPeeringsClient) CreateOrUpdateResponder(resp *http.Response) (result ExpressRouteCrossConnectionPeering, err error) {
131	err = autorest.Respond(
132		resp,
133		client.ByInspecting(),
134		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
135		autorest.ByUnmarshallingJSON(&result),
136		autorest.ByClosing())
137	result.Response = autorest.Response{Response: resp}
138	return
139}
140
141// Delete deletes the specified peering from the ExpressRouteCrossConnection.
142// Parameters:
143// resourceGroupName - the name of the resource group.
144// crossConnectionName - the name of the ExpressRouteCrossConnection.
145// peeringName - the name of the peering.
146func (client ExpressRouteCrossConnectionPeeringsClient) Delete(ctx context.Context, resourceGroupName string, crossConnectionName string, peeringName string) (result ExpressRouteCrossConnectionPeeringsDeleteFuture, err error) {
147	if tracing.IsEnabled() {
148		ctx = tracing.StartSpan(ctx, fqdn+"/ExpressRouteCrossConnectionPeeringsClient.Delete")
149		defer func() {
150			sc := -1
151			if result.Response() != nil {
152				sc = result.Response().StatusCode
153			}
154			tracing.EndSpan(ctx, sc, err)
155		}()
156	}
157	req, err := client.DeletePreparer(ctx, resourceGroupName, crossConnectionName, peeringName)
158	if err != nil {
159		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "Delete", nil, "Failure preparing request")
160		return
161	}
162
163	result, err = client.DeleteSender(req)
164	if err != nil {
165		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "Delete", result.Response(), "Failure sending request")
166		return
167	}
168
169	return
170}
171
172// DeletePreparer prepares the Delete request.
173func (client ExpressRouteCrossConnectionPeeringsClient) DeletePreparer(ctx context.Context, resourceGroupName string, crossConnectionName string, peeringName string) (*http.Request, error) {
174	pathParameters := map[string]interface{}{
175		"crossConnectionName": autorest.Encode("path", crossConnectionName),
176		"peeringName":         autorest.Encode("path", peeringName),
177		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
178		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
179	}
180
181	const APIVersion = "2020-03-01"
182	queryParameters := map[string]interface{}{
183		"api-version": APIVersion,
184	}
185
186	preparer := autorest.CreatePreparer(
187		autorest.AsDelete(),
188		autorest.WithBaseURL(client.BaseURI),
189		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/expressRouteCrossConnections/{crossConnectionName}/peerings/{peeringName}", pathParameters),
190		autorest.WithQueryParameters(queryParameters))
191	return preparer.Prepare((&http.Request{}).WithContext(ctx))
192}
193
194// DeleteSender sends the Delete request. The method will close the
195// http.Response Body if it receives an error.
196func (client ExpressRouteCrossConnectionPeeringsClient) DeleteSender(req *http.Request) (future ExpressRouteCrossConnectionPeeringsDeleteFuture, err error) {
197	var resp *http.Response
198	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
199	if err != nil {
200		return
201	}
202	future.Future, err = azure.NewFutureFromResponse(resp)
203	return
204}
205
206// DeleteResponder handles the response to the Delete request. The method always
207// closes the http.Response Body.
208func (client ExpressRouteCrossConnectionPeeringsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
209	err = autorest.Respond(
210		resp,
211		client.ByInspecting(),
212		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
213		autorest.ByClosing())
214	result.Response = resp
215	return
216}
217
218// Get gets the specified peering for the ExpressRouteCrossConnection.
219// Parameters:
220// resourceGroupName - the name of the resource group.
221// crossConnectionName - the name of the ExpressRouteCrossConnection.
222// peeringName - the name of the peering.
223func (client ExpressRouteCrossConnectionPeeringsClient) Get(ctx context.Context, resourceGroupName string, crossConnectionName string, peeringName string) (result ExpressRouteCrossConnectionPeering, err error) {
224	if tracing.IsEnabled() {
225		ctx = tracing.StartSpan(ctx, fqdn+"/ExpressRouteCrossConnectionPeeringsClient.Get")
226		defer func() {
227			sc := -1
228			if result.Response.Response != nil {
229				sc = result.Response.Response.StatusCode
230			}
231			tracing.EndSpan(ctx, sc, err)
232		}()
233	}
234	req, err := client.GetPreparer(ctx, resourceGroupName, crossConnectionName, peeringName)
235	if err != nil {
236		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "Get", nil, "Failure preparing request")
237		return
238	}
239
240	resp, err := client.GetSender(req)
241	if err != nil {
242		result.Response = autorest.Response{Response: resp}
243		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "Get", resp, "Failure sending request")
244		return
245	}
246
247	result, err = client.GetResponder(resp)
248	if err != nil {
249		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "Get", resp, "Failure responding to request")
250	}
251
252	return
253}
254
255// GetPreparer prepares the Get request.
256func (client ExpressRouteCrossConnectionPeeringsClient) GetPreparer(ctx context.Context, resourceGroupName string, crossConnectionName string, peeringName string) (*http.Request, error) {
257	pathParameters := map[string]interface{}{
258		"crossConnectionName": autorest.Encode("path", crossConnectionName),
259		"peeringName":         autorest.Encode("path", peeringName),
260		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
261		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
262	}
263
264	const APIVersion = "2020-03-01"
265	queryParameters := map[string]interface{}{
266		"api-version": APIVersion,
267	}
268
269	preparer := autorest.CreatePreparer(
270		autorest.AsGet(),
271		autorest.WithBaseURL(client.BaseURI),
272		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/expressRouteCrossConnections/{crossConnectionName}/peerings/{peeringName}", pathParameters),
273		autorest.WithQueryParameters(queryParameters))
274	return preparer.Prepare((&http.Request{}).WithContext(ctx))
275}
276
277// GetSender sends the Get request. The method will close the
278// http.Response Body if it receives an error.
279func (client ExpressRouteCrossConnectionPeeringsClient) GetSender(req *http.Request) (*http.Response, error) {
280	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
281}
282
283// GetResponder handles the response to the Get request. The method always
284// closes the http.Response Body.
285func (client ExpressRouteCrossConnectionPeeringsClient) GetResponder(resp *http.Response) (result ExpressRouteCrossConnectionPeering, err error) {
286	err = autorest.Respond(
287		resp,
288		client.ByInspecting(),
289		azure.WithErrorUnlessStatusCode(http.StatusOK),
290		autorest.ByUnmarshallingJSON(&result),
291		autorest.ByClosing())
292	result.Response = autorest.Response{Response: resp}
293	return
294}
295
296// List gets all peerings in a specified ExpressRouteCrossConnection.
297// Parameters:
298// resourceGroupName - the name of the resource group.
299// crossConnectionName - the name of the ExpressRouteCrossConnection.
300func (client ExpressRouteCrossConnectionPeeringsClient) List(ctx context.Context, resourceGroupName string, crossConnectionName string) (result ExpressRouteCrossConnectionPeeringListPage, err error) {
301	if tracing.IsEnabled() {
302		ctx = tracing.StartSpan(ctx, fqdn+"/ExpressRouteCrossConnectionPeeringsClient.List")
303		defer func() {
304			sc := -1
305			if result.erccpl.Response.Response != nil {
306				sc = result.erccpl.Response.Response.StatusCode
307			}
308			tracing.EndSpan(ctx, sc, err)
309		}()
310	}
311	result.fn = client.listNextResults
312	req, err := client.ListPreparer(ctx, resourceGroupName, crossConnectionName)
313	if err != nil {
314		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "List", nil, "Failure preparing request")
315		return
316	}
317
318	resp, err := client.ListSender(req)
319	if err != nil {
320		result.erccpl.Response = autorest.Response{Response: resp}
321		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "List", resp, "Failure sending request")
322		return
323	}
324
325	result.erccpl, err = client.ListResponder(resp)
326	if err != nil {
327		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "List", resp, "Failure responding to request")
328	}
329
330	return
331}
332
333// ListPreparer prepares the List request.
334func (client ExpressRouteCrossConnectionPeeringsClient) ListPreparer(ctx context.Context, resourceGroupName string, crossConnectionName string) (*http.Request, error) {
335	pathParameters := map[string]interface{}{
336		"crossConnectionName": autorest.Encode("path", crossConnectionName),
337		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
338		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
339	}
340
341	const APIVersion = "2020-03-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/expressRouteCrossConnections/{crossConnectionName}/peerings", 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 ExpressRouteCrossConnectionPeeringsClient) 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 ExpressRouteCrossConnectionPeeringsClient) ListResponder(resp *http.Response) (result ExpressRouteCrossConnectionPeeringList, err error) {
363	err = autorest.Respond(
364		resp,
365		client.ByInspecting(),
366		azure.WithErrorUnlessStatusCode(http.StatusOK),
367		autorest.ByUnmarshallingJSON(&result),
368		autorest.ByClosing())
369	result.Response = autorest.Response{Response: resp}
370	return
371}
372
373// listNextResults retrieves the next set of results, if any.
374func (client ExpressRouteCrossConnectionPeeringsClient) listNextResults(ctx context.Context, lastResults ExpressRouteCrossConnectionPeeringList) (result ExpressRouteCrossConnectionPeeringList, err error) {
375	req, err := lastResults.expressRouteCrossConnectionPeeringListPreparer(ctx)
376	if err != nil {
377		return result, autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "listNextResults", nil, "Failure preparing next results request")
378	}
379	if req == nil {
380		return
381	}
382	resp, err := client.ListSender(req)
383	if err != nil {
384		result.Response = autorest.Response{Response: resp}
385		return result, autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "listNextResults", resp, "Failure sending next results request")
386	}
387	result, err = client.ListResponder(resp)
388	if err != nil {
389		err = autorest.NewErrorWithError(err, "network.ExpressRouteCrossConnectionPeeringsClient", "listNextResults", resp, "Failure responding to next results request")
390	}
391	return
392}
393
394// ListComplete enumerates all values, automatically crossing page boundaries as required.
395func (client ExpressRouteCrossConnectionPeeringsClient) ListComplete(ctx context.Context, resourceGroupName string, crossConnectionName string) (result ExpressRouteCrossConnectionPeeringListIterator, err error) {
396	if tracing.IsEnabled() {
397		ctx = tracing.StartSpan(ctx, fqdn+"/ExpressRouteCrossConnectionPeeringsClient.List")
398		defer func() {
399			sc := -1
400			if result.Response().Response.Response != nil {
401				sc = result.page.Response().Response.Response.StatusCode
402			}
403			tracing.EndSpan(ctx, sc, err)
404		}()
405	}
406	result.page, err = client.List(ctx, resourceGroupName, crossConnectionName)
407	return
408}
409