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