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// BastionHostsClient is the network Client
29type BastionHostsClient struct {
30	BaseClient
31}
32
33// NewBastionHostsClient creates an instance of the BastionHostsClient client.
34func NewBastionHostsClient(subscriptionID string) BastionHostsClient {
35	return NewBastionHostsClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewBastionHostsClientWithBaseURI creates an instance of the BastionHostsClient client using a custom endpoint.  Use
39// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
40func NewBastionHostsClientWithBaseURI(baseURI string, subscriptionID string) BastionHostsClient {
41	return BastionHostsClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// CreateOrUpdate creates or updates the specified Bastion Host.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// bastionHostName - the name of the Bastion Host.
48// parameters - parameters supplied to the create or update Bastion Host operation.
49func (client BastionHostsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, bastionHostName string, parameters BastionHost) (result BastionHostsCreateOrUpdateFuture, err error) {
50	if tracing.IsEnabled() {
51		ctx = tracing.StartSpan(ctx, fqdn+"/BastionHostsClient.CreateOrUpdate")
52		defer func() {
53			sc := -1
54			if result.Response() != nil {
55				sc = result.Response().StatusCode
56			}
57			tracing.EndSpan(ctx, sc, err)
58		}()
59	}
60	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, bastionHostName, parameters)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "CreateOrUpdate", nil, "Failure preparing request")
63		return
64	}
65
66	result, err = client.CreateOrUpdateSender(req)
67	if err != nil {
68		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "CreateOrUpdate", nil, "Failure sending request")
69		return
70	}
71
72	return
73}
74
75// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
76func (client BastionHostsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, bastionHostName string, parameters BastionHost) (*http.Request, error) {
77	pathParameters := map[string]interface{}{
78		"bastionHostName":   autorest.Encode("path", bastionHostName),
79		"resourceGroupName": autorest.Encode("path", resourceGroupName),
80		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
81	}
82
83	const APIVersion = "2019-12-01"
84	queryParameters := map[string]interface{}{
85		"api-version": APIVersion,
86	}
87
88	parameters.Etag = nil
89	preparer := autorest.CreatePreparer(
90		autorest.AsContentType("application/json; charset=utf-8"),
91		autorest.AsPut(),
92		autorest.WithBaseURL(client.BaseURI),
93		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/bastionHosts/{bastionHostName}", pathParameters),
94		autorest.WithJSON(parameters),
95		autorest.WithQueryParameters(queryParameters))
96	return preparer.Prepare((&http.Request{}).WithContext(ctx))
97}
98
99// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
100// http.Response Body if it receives an error.
101func (client BastionHostsClient) CreateOrUpdateSender(req *http.Request) (future BastionHostsCreateOrUpdateFuture, err error) {
102	var resp *http.Response
103	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
104	if err != nil {
105		return
106	}
107	var azf azure.Future
108	azf, err = azure.NewFutureFromResponse(resp)
109	future.FutureAPI = &azf
110	future.Result = func(client BastionHostsClient) (bh BastionHost, err error) {
111		var done bool
112		done, err = future.DoneWithContext(context.Background(), client)
113		if err != nil {
114			err = autorest.NewErrorWithError(err, "network.BastionHostsCreateOrUpdateFuture", "Result", future.Response(), "Polling failure")
115			return
116		}
117		if !done {
118			err = azure.NewAsyncOpIncompleteError("network.BastionHostsCreateOrUpdateFuture")
119			return
120		}
121		sender := autorest.DecorateSender(client, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
122		if bh.Response.Response, err = future.GetResult(sender); err == nil && bh.Response.Response.StatusCode != http.StatusNoContent {
123			bh, err = client.CreateOrUpdateResponder(bh.Response.Response)
124			if err != nil {
125				err = autorest.NewErrorWithError(err, "network.BastionHostsCreateOrUpdateFuture", "Result", bh.Response.Response, "Failure responding to request")
126			}
127		}
128		return
129	}
130	return
131}
132
133// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
134// closes the http.Response Body.
135func (client BastionHostsClient) CreateOrUpdateResponder(resp *http.Response) (result BastionHost, err error) {
136	err = autorest.Respond(
137		resp,
138		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
139		autorest.ByUnmarshallingJSON(&result),
140		autorest.ByClosing())
141	result.Response = autorest.Response{Response: resp}
142	return
143}
144
145// Delete deletes the specified Bastion Host.
146// Parameters:
147// resourceGroupName - the name of the resource group.
148// bastionHostName - the name of the Bastion Host.
149func (client BastionHostsClient) Delete(ctx context.Context, resourceGroupName string, bastionHostName string) (result BastionHostsDeleteFuture, err error) {
150	if tracing.IsEnabled() {
151		ctx = tracing.StartSpan(ctx, fqdn+"/BastionHostsClient.Delete")
152		defer func() {
153			sc := -1
154			if result.Response() != nil {
155				sc = result.Response().StatusCode
156			}
157			tracing.EndSpan(ctx, sc, err)
158		}()
159	}
160	req, err := client.DeletePreparer(ctx, resourceGroupName, bastionHostName)
161	if err != nil {
162		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "Delete", nil, "Failure preparing request")
163		return
164	}
165
166	result, err = client.DeleteSender(req)
167	if err != nil {
168		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "Delete", nil, "Failure sending request")
169		return
170	}
171
172	return
173}
174
175// DeletePreparer prepares the Delete request.
176func (client BastionHostsClient) DeletePreparer(ctx context.Context, resourceGroupName string, bastionHostName string) (*http.Request, error) {
177	pathParameters := map[string]interface{}{
178		"bastionHostName":   autorest.Encode("path", bastionHostName),
179		"resourceGroupName": autorest.Encode("path", resourceGroupName),
180		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
181	}
182
183	const APIVersion = "2019-12-01"
184	queryParameters := map[string]interface{}{
185		"api-version": APIVersion,
186	}
187
188	preparer := autorest.CreatePreparer(
189		autorest.AsDelete(),
190		autorest.WithBaseURL(client.BaseURI),
191		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/bastionHosts/{bastionHostName}", pathParameters),
192		autorest.WithQueryParameters(queryParameters))
193	return preparer.Prepare((&http.Request{}).WithContext(ctx))
194}
195
196// DeleteSender sends the Delete request. The method will close the
197// http.Response Body if it receives an error.
198func (client BastionHostsClient) DeleteSender(req *http.Request) (future BastionHostsDeleteFuture, err error) {
199	var resp *http.Response
200	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
201	if err != nil {
202		return
203	}
204	var azf azure.Future
205	azf, err = azure.NewFutureFromResponse(resp)
206	future.FutureAPI = &azf
207	future.Result = func(client BastionHostsClient) (ar autorest.Response, err error) {
208		var done bool
209		done, err = future.DoneWithContext(context.Background(), client)
210		if err != nil {
211			err = autorest.NewErrorWithError(err, "network.BastionHostsDeleteFuture", "Result", future.Response(), "Polling failure")
212			return
213		}
214		if !done {
215			err = azure.NewAsyncOpIncompleteError("network.BastionHostsDeleteFuture")
216			return
217		}
218		ar.Response = future.Response()
219		return
220	}
221	return
222}
223
224// DeleteResponder handles the response to the Delete request. The method always
225// closes the http.Response Body.
226func (client BastionHostsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
227	err = autorest.Respond(
228		resp,
229		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
230		autorest.ByClosing())
231	result.Response = resp
232	return
233}
234
235// Get gets the specified Bastion Host.
236// Parameters:
237// resourceGroupName - the name of the resource group.
238// bastionHostName - the name of the Bastion Host.
239func (client BastionHostsClient) Get(ctx context.Context, resourceGroupName string, bastionHostName string) (result BastionHost, err error) {
240	if tracing.IsEnabled() {
241		ctx = tracing.StartSpan(ctx, fqdn+"/BastionHostsClient.Get")
242		defer func() {
243			sc := -1
244			if result.Response.Response != nil {
245				sc = result.Response.Response.StatusCode
246			}
247			tracing.EndSpan(ctx, sc, err)
248		}()
249	}
250	req, err := client.GetPreparer(ctx, resourceGroupName, bastionHostName)
251	if err != nil {
252		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "Get", nil, "Failure preparing request")
253		return
254	}
255
256	resp, err := client.GetSender(req)
257	if err != nil {
258		result.Response = autorest.Response{Response: resp}
259		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "Get", resp, "Failure sending request")
260		return
261	}
262
263	result, err = client.GetResponder(resp)
264	if err != nil {
265		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "Get", resp, "Failure responding to request")
266		return
267	}
268
269	return
270}
271
272// GetPreparer prepares the Get request.
273func (client BastionHostsClient) GetPreparer(ctx context.Context, resourceGroupName string, bastionHostName string) (*http.Request, error) {
274	pathParameters := map[string]interface{}{
275		"bastionHostName":   autorest.Encode("path", bastionHostName),
276		"resourceGroupName": autorest.Encode("path", resourceGroupName),
277		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
278	}
279
280	const APIVersion = "2019-12-01"
281	queryParameters := map[string]interface{}{
282		"api-version": APIVersion,
283	}
284
285	preparer := autorest.CreatePreparer(
286		autorest.AsGet(),
287		autorest.WithBaseURL(client.BaseURI),
288		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/bastionHosts/{bastionHostName}", pathParameters),
289		autorest.WithQueryParameters(queryParameters))
290	return preparer.Prepare((&http.Request{}).WithContext(ctx))
291}
292
293// GetSender sends the Get request. The method will close the
294// http.Response Body if it receives an error.
295func (client BastionHostsClient) GetSender(req *http.Request) (*http.Response, error) {
296	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
297}
298
299// GetResponder handles the response to the Get request. The method always
300// closes the http.Response Body.
301func (client BastionHostsClient) GetResponder(resp *http.Response) (result BastionHost, err error) {
302	err = autorest.Respond(
303		resp,
304		azure.WithErrorUnlessStatusCode(http.StatusOK),
305		autorest.ByUnmarshallingJSON(&result),
306		autorest.ByClosing())
307	result.Response = autorest.Response{Response: resp}
308	return
309}
310
311// List lists all Bastion Hosts in a subscription.
312func (client BastionHostsClient) List(ctx context.Context) (result BastionHostListResultPage, err error) {
313	if tracing.IsEnabled() {
314		ctx = tracing.StartSpan(ctx, fqdn+"/BastionHostsClient.List")
315		defer func() {
316			sc := -1
317			if result.bhlr.Response.Response != nil {
318				sc = result.bhlr.Response.Response.StatusCode
319			}
320			tracing.EndSpan(ctx, sc, err)
321		}()
322	}
323	result.fn = client.listNextResults
324	req, err := client.ListPreparer(ctx)
325	if err != nil {
326		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "List", nil, "Failure preparing request")
327		return
328	}
329
330	resp, err := client.ListSender(req)
331	if err != nil {
332		result.bhlr.Response = autorest.Response{Response: resp}
333		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "List", resp, "Failure sending request")
334		return
335	}
336
337	result.bhlr, err = client.ListResponder(resp)
338	if err != nil {
339		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "List", resp, "Failure responding to request")
340		return
341	}
342	if result.bhlr.hasNextLink() && result.bhlr.IsEmpty() {
343		err = result.NextWithContext(ctx)
344		return
345	}
346
347	return
348}
349
350// ListPreparer prepares the List request.
351func (client BastionHostsClient) ListPreparer(ctx context.Context) (*http.Request, error) {
352	pathParameters := map[string]interface{}{
353		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
354	}
355
356	const APIVersion = "2019-12-01"
357	queryParameters := map[string]interface{}{
358		"api-version": APIVersion,
359	}
360
361	preparer := autorest.CreatePreparer(
362		autorest.AsGet(),
363		autorest.WithBaseURL(client.BaseURI),
364		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Network/bastionHosts", pathParameters),
365		autorest.WithQueryParameters(queryParameters))
366	return preparer.Prepare((&http.Request{}).WithContext(ctx))
367}
368
369// ListSender sends the List request. The method will close the
370// http.Response Body if it receives an error.
371func (client BastionHostsClient) ListSender(req *http.Request) (*http.Response, error) {
372	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
373}
374
375// ListResponder handles the response to the List request. The method always
376// closes the http.Response Body.
377func (client BastionHostsClient) ListResponder(resp *http.Response) (result BastionHostListResult, err error) {
378	err = autorest.Respond(
379		resp,
380		azure.WithErrorUnlessStatusCode(http.StatusOK),
381		autorest.ByUnmarshallingJSON(&result),
382		autorest.ByClosing())
383	result.Response = autorest.Response{Response: resp}
384	return
385}
386
387// listNextResults retrieves the next set of results, if any.
388func (client BastionHostsClient) listNextResults(ctx context.Context, lastResults BastionHostListResult) (result BastionHostListResult, err error) {
389	req, err := lastResults.bastionHostListResultPreparer(ctx)
390	if err != nil {
391		return result, autorest.NewErrorWithError(err, "network.BastionHostsClient", "listNextResults", nil, "Failure preparing next results request")
392	}
393	if req == nil {
394		return
395	}
396	resp, err := client.ListSender(req)
397	if err != nil {
398		result.Response = autorest.Response{Response: resp}
399		return result, autorest.NewErrorWithError(err, "network.BastionHostsClient", "listNextResults", resp, "Failure sending next results request")
400	}
401	result, err = client.ListResponder(resp)
402	if err != nil {
403		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "listNextResults", resp, "Failure responding to next results request")
404	}
405	return
406}
407
408// ListComplete enumerates all values, automatically crossing page boundaries as required.
409func (client BastionHostsClient) ListComplete(ctx context.Context) (result BastionHostListResultIterator, err error) {
410	if tracing.IsEnabled() {
411		ctx = tracing.StartSpan(ctx, fqdn+"/BastionHostsClient.List")
412		defer func() {
413			sc := -1
414			if result.Response().Response.Response != nil {
415				sc = result.page.Response().Response.Response.StatusCode
416			}
417			tracing.EndSpan(ctx, sc, err)
418		}()
419	}
420	result.page, err = client.List(ctx)
421	return
422}
423
424// ListByResourceGroup lists all Bastion Hosts in a resource group.
425// Parameters:
426// resourceGroupName - the name of the resource group.
427func (client BastionHostsClient) ListByResourceGroup(ctx context.Context, resourceGroupName string) (result BastionHostListResultPage, err error) {
428	if tracing.IsEnabled() {
429		ctx = tracing.StartSpan(ctx, fqdn+"/BastionHostsClient.ListByResourceGroup")
430		defer func() {
431			sc := -1
432			if result.bhlr.Response.Response != nil {
433				sc = result.bhlr.Response.Response.StatusCode
434			}
435			tracing.EndSpan(ctx, sc, err)
436		}()
437	}
438	result.fn = client.listByResourceGroupNextResults
439	req, err := client.ListByResourceGroupPreparer(ctx, resourceGroupName)
440	if err != nil {
441		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "ListByResourceGroup", nil, "Failure preparing request")
442		return
443	}
444
445	resp, err := client.ListByResourceGroupSender(req)
446	if err != nil {
447		result.bhlr.Response = autorest.Response{Response: resp}
448		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "ListByResourceGroup", resp, "Failure sending request")
449		return
450	}
451
452	result.bhlr, err = client.ListByResourceGroupResponder(resp)
453	if err != nil {
454		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "ListByResourceGroup", resp, "Failure responding to request")
455		return
456	}
457	if result.bhlr.hasNextLink() && result.bhlr.IsEmpty() {
458		err = result.NextWithContext(ctx)
459		return
460	}
461
462	return
463}
464
465// ListByResourceGroupPreparer prepares the ListByResourceGroup request.
466func (client BastionHostsClient) ListByResourceGroupPreparer(ctx context.Context, resourceGroupName string) (*http.Request, error) {
467	pathParameters := map[string]interface{}{
468		"resourceGroupName": autorest.Encode("path", resourceGroupName),
469		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
470	}
471
472	const APIVersion = "2019-12-01"
473	queryParameters := map[string]interface{}{
474		"api-version": APIVersion,
475	}
476
477	preparer := autorest.CreatePreparer(
478		autorest.AsGet(),
479		autorest.WithBaseURL(client.BaseURI),
480		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/bastionHosts", pathParameters),
481		autorest.WithQueryParameters(queryParameters))
482	return preparer.Prepare((&http.Request{}).WithContext(ctx))
483}
484
485// ListByResourceGroupSender sends the ListByResourceGroup request. The method will close the
486// http.Response Body if it receives an error.
487func (client BastionHostsClient) ListByResourceGroupSender(req *http.Request) (*http.Response, error) {
488	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
489}
490
491// ListByResourceGroupResponder handles the response to the ListByResourceGroup request. The method always
492// closes the http.Response Body.
493func (client BastionHostsClient) ListByResourceGroupResponder(resp *http.Response) (result BastionHostListResult, err error) {
494	err = autorest.Respond(
495		resp,
496		azure.WithErrorUnlessStatusCode(http.StatusOK),
497		autorest.ByUnmarshallingJSON(&result),
498		autorest.ByClosing())
499	result.Response = autorest.Response{Response: resp}
500	return
501}
502
503// listByResourceGroupNextResults retrieves the next set of results, if any.
504func (client BastionHostsClient) listByResourceGroupNextResults(ctx context.Context, lastResults BastionHostListResult) (result BastionHostListResult, err error) {
505	req, err := lastResults.bastionHostListResultPreparer(ctx)
506	if err != nil {
507		return result, autorest.NewErrorWithError(err, "network.BastionHostsClient", "listByResourceGroupNextResults", nil, "Failure preparing next results request")
508	}
509	if req == nil {
510		return
511	}
512	resp, err := client.ListByResourceGroupSender(req)
513	if err != nil {
514		result.Response = autorest.Response{Response: resp}
515		return result, autorest.NewErrorWithError(err, "network.BastionHostsClient", "listByResourceGroupNextResults", resp, "Failure sending next results request")
516	}
517	result, err = client.ListByResourceGroupResponder(resp)
518	if err != nil {
519		err = autorest.NewErrorWithError(err, "network.BastionHostsClient", "listByResourceGroupNextResults", resp, "Failure responding to next results request")
520	}
521	return
522}
523
524// ListByResourceGroupComplete enumerates all values, automatically crossing page boundaries as required.
525func (client BastionHostsClient) ListByResourceGroupComplete(ctx context.Context, resourceGroupName string) (result BastionHostListResultIterator, err error) {
526	if tracing.IsEnabled() {
527		ctx = tracing.StartSpan(ctx, fqdn+"/BastionHostsClient.ListByResourceGroup")
528		defer func() {
529			sc := -1
530			if result.Response().Response.Response != nil {
531				sc = result.page.Response().Response.Response.StatusCode
532			}
533			tracing.EndSpan(ctx, sc, err)
534		}()
535	}
536	result.page, err = client.ListByResourceGroup(ctx, resourceGroupName)
537	return
538}
539