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