1// +build go1.13
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// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armnetwork
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"net/http"
17	"net/url"
18	"strings"
19	"time"
20)
21
22// ExpressRouteConnectionsClient contains the methods for the ExpressRouteConnections group.
23// Don't use this type directly, use NewExpressRouteConnectionsClient() instead.
24type ExpressRouteConnectionsClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewExpressRouteConnectionsClient creates a new instance of ExpressRouteConnectionsClient with the specified values.
30func NewExpressRouteConnectionsClient(con *armcore.Connection, subscriptionID string) *ExpressRouteConnectionsClient {
31	return &ExpressRouteConnectionsClient{con: con, subscriptionID: subscriptionID}
32}
33
34// BeginCreateOrUpdate - Creates a connection between an ExpressRoute gateway and an ExpressRoute circuit.
35// If the operation fails it returns the *CloudError error type.
36func (client *ExpressRouteConnectionsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, putExpressRouteConnectionParameters ExpressRouteConnection, options *ExpressRouteConnectionsBeginCreateOrUpdateOptions) (ExpressRouteConnectionPollerResponse, error) {
37	resp, err := client.createOrUpdate(ctx, resourceGroupName, expressRouteGatewayName, connectionName, putExpressRouteConnectionParameters, options)
38	if err != nil {
39		return ExpressRouteConnectionPollerResponse{}, err
40	}
41	result := ExpressRouteConnectionPollerResponse{
42		RawResponse: resp.Response,
43	}
44	pt, err := armcore.NewLROPoller("ExpressRouteConnectionsClient.CreateOrUpdate", "azure-async-operation", resp, client.con.Pipeline(), client.createOrUpdateHandleError)
45	if err != nil {
46		return ExpressRouteConnectionPollerResponse{}, err
47	}
48	poller := &expressRouteConnectionPoller{
49		pt: pt,
50	}
51	result.Poller = poller
52	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (ExpressRouteConnectionResponse, error) {
53		return poller.pollUntilDone(ctx, frequency)
54	}
55	return result, nil
56}
57
58// ResumeCreateOrUpdate creates a new ExpressRouteConnectionPoller from the specified resume token.
59// token - The value must come from a previous call to ExpressRouteConnectionPoller.ResumeToken().
60func (client *ExpressRouteConnectionsClient) ResumeCreateOrUpdate(ctx context.Context, token string) (ExpressRouteConnectionPollerResponse, error) {
61	pt, err := armcore.NewLROPollerFromResumeToken("ExpressRouteConnectionsClient.CreateOrUpdate", token, client.con.Pipeline(), client.createOrUpdateHandleError)
62	if err != nil {
63		return ExpressRouteConnectionPollerResponse{}, err
64	}
65	poller := &expressRouteConnectionPoller{
66		pt: pt,
67	}
68	resp, err := poller.Poll(ctx)
69	if err != nil {
70		return ExpressRouteConnectionPollerResponse{}, err
71	}
72	result := ExpressRouteConnectionPollerResponse{
73		RawResponse: resp,
74	}
75	result.Poller = poller
76	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (ExpressRouteConnectionResponse, error) {
77		return poller.pollUntilDone(ctx, frequency)
78	}
79	return result, nil
80}
81
82// CreateOrUpdate - Creates a connection between an ExpressRoute gateway and an ExpressRoute circuit.
83// If the operation fails it returns the *CloudError error type.
84func (client *ExpressRouteConnectionsClient) createOrUpdate(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, putExpressRouteConnectionParameters ExpressRouteConnection, options *ExpressRouteConnectionsBeginCreateOrUpdateOptions) (*azcore.Response, error) {
85	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, expressRouteGatewayName, connectionName, putExpressRouteConnectionParameters, options)
86	if err != nil {
87		return nil, err
88	}
89	resp, err := client.con.Pipeline().Do(req)
90	if err != nil {
91		return nil, err
92	}
93	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated) {
94		return nil, client.createOrUpdateHandleError(resp)
95	}
96	return resp, nil
97}
98
99// createOrUpdateCreateRequest creates the CreateOrUpdate request.
100func (client *ExpressRouteConnectionsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, putExpressRouteConnectionParameters ExpressRouteConnection, options *ExpressRouteConnectionsBeginCreateOrUpdateOptions) (*azcore.Request, error) {
101	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/expressRouteGateways/{expressRouteGatewayName}/expressRouteConnections/{connectionName}"
102	if resourceGroupName == "" {
103		return nil, errors.New("parameter resourceGroupName cannot be empty")
104	}
105	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
106	if expressRouteGatewayName == "" {
107		return nil, errors.New("parameter expressRouteGatewayName cannot be empty")
108	}
109	urlPath = strings.ReplaceAll(urlPath, "{expressRouteGatewayName}", url.PathEscape(expressRouteGatewayName))
110	if connectionName == "" {
111		return nil, errors.New("parameter connectionName cannot be empty")
112	}
113	urlPath = strings.ReplaceAll(urlPath, "{connectionName}", url.PathEscape(connectionName))
114	if client.subscriptionID == "" {
115		return nil, errors.New("parameter client.subscriptionID cannot be empty")
116	}
117	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
118	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
119	if err != nil {
120		return nil, err
121	}
122	req.Telemetry(telemetryInfo)
123	reqQP := req.URL.Query()
124	reqQP.Set("api-version", "2021-02-01")
125	req.URL.RawQuery = reqQP.Encode()
126	req.Header.Set("Accept", "application/json")
127	return req, req.MarshalAsJSON(putExpressRouteConnectionParameters)
128}
129
130// createOrUpdateHandleError handles the CreateOrUpdate error response.
131func (client *ExpressRouteConnectionsClient) createOrUpdateHandleError(resp *azcore.Response) error {
132	body, err := resp.Payload()
133	if err != nil {
134		return azcore.NewResponseError(err, resp.Response)
135	}
136	errType := CloudError{raw: string(body)}
137	if err := resp.UnmarshalAsJSON(&errType); err != nil {
138		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
139	}
140	return azcore.NewResponseError(&errType, resp.Response)
141}
142
143// BeginDelete - Deletes a connection to a ExpressRoute circuit.
144// If the operation fails it returns the *CloudError error type.
145func (client *ExpressRouteConnectionsClient) BeginDelete(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, options *ExpressRouteConnectionsBeginDeleteOptions) (HTTPPollerResponse, error) {
146	resp, err := client.deleteOperation(ctx, resourceGroupName, expressRouteGatewayName, connectionName, options)
147	if err != nil {
148		return HTTPPollerResponse{}, err
149	}
150	result := HTTPPollerResponse{
151		RawResponse: resp.Response,
152	}
153	pt, err := armcore.NewLROPoller("ExpressRouteConnectionsClient.Delete", "location", resp, client.con.Pipeline(), client.deleteHandleError)
154	if err != nil {
155		return HTTPPollerResponse{}, err
156	}
157	poller := &httpPoller{
158		pt: pt,
159	}
160	result.Poller = poller
161	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
162		return poller.pollUntilDone(ctx, frequency)
163	}
164	return result, nil
165}
166
167// ResumeDelete creates a new HTTPPoller from the specified resume token.
168// token - The value must come from a previous call to HTTPPoller.ResumeToken().
169func (client *ExpressRouteConnectionsClient) ResumeDelete(ctx context.Context, token string) (HTTPPollerResponse, error) {
170	pt, err := armcore.NewLROPollerFromResumeToken("ExpressRouteConnectionsClient.Delete", token, client.con.Pipeline(), client.deleteHandleError)
171	if err != nil {
172		return HTTPPollerResponse{}, err
173	}
174	poller := &httpPoller{
175		pt: pt,
176	}
177	resp, err := poller.Poll(ctx)
178	if err != nil {
179		return HTTPPollerResponse{}, err
180	}
181	result := HTTPPollerResponse{
182		RawResponse: resp,
183	}
184	result.Poller = poller
185	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
186		return poller.pollUntilDone(ctx, frequency)
187	}
188	return result, nil
189}
190
191// Delete - Deletes a connection to a ExpressRoute circuit.
192// If the operation fails it returns the *CloudError error type.
193func (client *ExpressRouteConnectionsClient) deleteOperation(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, options *ExpressRouteConnectionsBeginDeleteOptions) (*azcore.Response, error) {
194	req, err := client.deleteCreateRequest(ctx, resourceGroupName, expressRouteGatewayName, connectionName, options)
195	if err != nil {
196		return nil, err
197	}
198	resp, err := client.con.Pipeline().Do(req)
199	if err != nil {
200		return nil, err
201	}
202	if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
203		return nil, client.deleteHandleError(resp)
204	}
205	return resp, nil
206}
207
208// deleteCreateRequest creates the Delete request.
209func (client *ExpressRouteConnectionsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, options *ExpressRouteConnectionsBeginDeleteOptions) (*azcore.Request, error) {
210	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/expressRouteGateways/{expressRouteGatewayName}/expressRouteConnections/{connectionName}"
211	if resourceGroupName == "" {
212		return nil, errors.New("parameter resourceGroupName cannot be empty")
213	}
214	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
215	if expressRouteGatewayName == "" {
216		return nil, errors.New("parameter expressRouteGatewayName cannot be empty")
217	}
218	urlPath = strings.ReplaceAll(urlPath, "{expressRouteGatewayName}", url.PathEscape(expressRouteGatewayName))
219	if connectionName == "" {
220		return nil, errors.New("parameter connectionName cannot be empty")
221	}
222	urlPath = strings.ReplaceAll(urlPath, "{connectionName}", url.PathEscape(connectionName))
223	if client.subscriptionID == "" {
224		return nil, errors.New("parameter client.subscriptionID cannot be empty")
225	}
226	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
227	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
228	if err != nil {
229		return nil, err
230	}
231	req.Telemetry(telemetryInfo)
232	reqQP := req.URL.Query()
233	reqQP.Set("api-version", "2021-02-01")
234	req.URL.RawQuery = reqQP.Encode()
235	req.Header.Set("Accept", "application/json")
236	return req, nil
237}
238
239// deleteHandleError handles the Delete error response.
240func (client *ExpressRouteConnectionsClient) deleteHandleError(resp *azcore.Response) error {
241	body, err := resp.Payload()
242	if err != nil {
243		return azcore.NewResponseError(err, resp.Response)
244	}
245	errType := CloudError{raw: string(body)}
246	if err := resp.UnmarshalAsJSON(&errType); err != nil {
247		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
248	}
249	return azcore.NewResponseError(&errType, resp.Response)
250}
251
252// Get - Gets the specified ExpressRouteConnection.
253// If the operation fails it returns the *CloudError error type.
254func (client *ExpressRouteConnectionsClient) Get(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, options *ExpressRouteConnectionsGetOptions) (ExpressRouteConnectionResponse, error) {
255	req, err := client.getCreateRequest(ctx, resourceGroupName, expressRouteGatewayName, connectionName, options)
256	if err != nil {
257		return ExpressRouteConnectionResponse{}, err
258	}
259	resp, err := client.con.Pipeline().Do(req)
260	if err != nil {
261		return ExpressRouteConnectionResponse{}, err
262	}
263	if !resp.HasStatusCode(http.StatusOK) {
264		return ExpressRouteConnectionResponse{}, client.getHandleError(resp)
265	}
266	return client.getHandleResponse(resp)
267}
268
269// getCreateRequest creates the Get request.
270func (client *ExpressRouteConnectionsClient) getCreateRequest(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, connectionName string, options *ExpressRouteConnectionsGetOptions) (*azcore.Request, error) {
271	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/expressRouteGateways/{expressRouteGatewayName}/expressRouteConnections/{connectionName}"
272	if resourceGroupName == "" {
273		return nil, errors.New("parameter resourceGroupName cannot be empty")
274	}
275	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
276	if expressRouteGatewayName == "" {
277		return nil, errors.New("parameter expressRouteGatewayName cannot be empty")
278	}
279	urlPath = strings.ReplaceAll(urlPath, "{expressRouteGatewayName}", url.PathEscape(expressRouteGatewayName))
280	if connectionName == "" {
281		return nil, errors.New("parameter connectionName cannot be empty")
282	}
283	urlPath = strings.ReplaceAll(urlPath, "{connectionName}", url.PathEscape(connectionName))
284	if client.subscriptionID == "" {
285		return nil, errors.New("parameter client.subscriptionID cannot be empty")
286	}
287	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
288	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
289	if err != nil {
290		return nil, err
291	}
292	req.Telemetry(telemetryInfo)
293	reqQP := req.URL.Query()
294	reqQP.Set("api-version", "2021-02-01")
295	req.URL.RawQuery = reqQP.Encode()
296	req.Header.Set("Accept", "application/json")
297	return req, nil
298}
299
300// getHandleResponse handles the Get response.
301func (client *ExpressRouteConnectionsClient) getHandleResponse(resp *azcore.Response) (ExpressRouteConnectionResponse, error) {
302	var val *ExpressRouteConnection
303	if err := resp.UnmarshalAsJSON(&val); err != nil {
304		return ExpressRouteConnectionResponse{}, err
305	}
306	return ExpressRouteConnectionResponse{RawResponse: resp.Response, ExpressRouteConnection: val}, nil
307}
308
309// getHandleError handles the Get error response.
310func (client *ExpressRouteConnectionsClient) getHandleError(resp *azcore.Response) error {
311	body, err := resp.Payload()
312	if err != nil {
313		return azcore.NewResponseError(err, resp.Response)
314	}
315	errType := CloudError{raw: string(body)}
316	if err := resp.UnmarshalAsJSON(&errType); err != nil {
317		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
318	}
319	return azcore.NewResponseError(&errType, resp.Response)
320}
321
322// List - Lists ExpressRouteConnections.
323// If the operation fails it returns the *CloudError error type.
324func (client *ExpressRouteConnectionsClient) List(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, options *ExpressRouteConnectionsListOptions) (ExpressRouteConnectionListResponse, error) {
325	req, err := client.listCreateRequest(ctx, resourceGroupName, expressRouteGatewayName, options)
326	if err != nil {
327		return ExpressRouteConnectionListResponse{}, err
328	}
329	resp, err := client.con.Pipeline().Do(req)
330	if err != nil {
331		return ExpressRouteConnectionListResponse{}, err
332	}
333	if !resp.HasStatusCode(http.StatusOK) {
334		return ExpressRouteConnectionListResponse{}, client.listHandleError(resp)
335	}
336	return client.listHandleResponse(resp)
337}
338
339// listCreateRequest creates the List request.
340func (client *ExpressRouteConnectionsClient) listCreateRequest(ctx context.Context, resourceGroupName string, expressRouteGatewayName string, options *ExpressRouteConnectionsListOptions) (*azcore.Request, error) {
341	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/expressRouteGateways/{expressRouteGatewayName}/expressRouteConnections"
342	if resourceGroupName == "" {
343		return nil, errors.New("parameter resourceGroupName cannot be empty")
344	}
345	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
346	if expressRouteGatewayName == "" {
347		return nil, errors.New("parameter expressRouteGatewayName cannot be empty")
348	}
349	urlPath = strings.ReplaceAll(urlPath, "{expressRouteGatewayName}", url.PathEscape(expressRouteGatewayName))
350	if client.subscriptionID == "" {
351		return nil, errors.New("parameter client.subscriptionID cannot be empty")
352	}
353	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
354	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
355	if err != nil {
356		return nil, err
357	}
358	req.Telemetry(telemetryInfo)
359	reqQP := req.URL.Query()
360	reqQP.Set("api-version", "2021-02-01")
361	req.URL.RawQuery = reqQP.Encode()
362	req.Header.Set("Accept", "application/json")
363	return req, nil
364}
365
366// listHandleResponse handles the List response.
367func (client *ExpressRouteConnectionsClient) listHandleResponse(resp *azcore.Response) (ExpressRouteConnectionListResponse, error) {
368	var val *ExpressRouteConnectionList
369	if err := resp.UnmarshalAsJSON(&val); err != nil {
370		return ExpressRouteConnectionListResponse{}, err
371	}
372	return ExpressRouteConnectionListResponse{RawResponse: resp.Response, ExpressRouteConnectionList: val}, nil
373}
374
375// listHandleError handles the List error response.
376func (client *ExpressRouteConnectionsClient) listHandleError(resp *azcore.Response) error {
377	body, err := resp.Payload()
378	if err != nil {
379		return azcore.NewResponseError(err, resp.Response)
380	}
381	errType := CloudError{raw: string(body)}
382	if err := resp.UnmarshalAsJSON(&errType); err != nil {
383		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
384	}
385	return azcore.NewResponseError(&errType, resp.Response)
386}
387