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.Response() != nil {
57				sc = result.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", result.Response(), "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 = "2020-04-01"
106	queryParameters := map[string]interface{}{
107		"api-version": APIVersion,
108	}
109
110	inboundNatRuleParameters.Etag = nil
111	inboundNatRuleParameters.Type = nil
112	preparer := autorest.CreatePreparer(
113		autorest.AsContentType("application/json; charset=utf-8"),
114		autorest.AsPut(),
115		autorest.WithBaseURL(client.BaseURI),
116		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules/{inboundNatRuleName}", pathParameters),
117		autorest.WithJSON(inboundNatRuleParameters),
118		autorest.WithQueryParameters(queryParameters))
119	return preparer.Prepare((&http.Request{}).WithContext(ctx))
120}
121
122// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
123// http.Response Body if it receives an error.
124func (client InboundNatRulesClient) CreateOrUpdateSender(req *http.Request) (future InboundNatRulesCreateOrUpdateFuture, err error) {
125	var resp *http.Response
126	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
127	if err != nil {
128		return
129	}
130	future.Future, err = azure.NewFutureFromResponse(resp)
131	return
132}
133
134// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
135// closes the http.Response Body.
136func (client InboundNatRulesClient) CreateOrUpdateResponder(resp *http.Response) (result InboundNatRule, err error) {
137	err = autorest.Respond(
138		resp,
139		client.ByInspecting(),
140		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
141		autorest.ByUnmarshallingJSON(&result),
142		autorest.ByClosing())
143	result.Response = autorest.Response{Response: resp}
144	return
145}
146
147// Delete deletes the specified load balancer inbound nat rule.
148// Parameters:
149// resourceGroupName - the name of the resource group.
150// loadBalancerName - the name of the load balancer.
151// inboundNatRuleName - the name of the inbound nat rule.
152func (client InboundNatRulesClient) Delete(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string) (result InboundNatRulesDeleteFuture, err error) {
153	if tracing.IsEnabled() {
154		ctx = tracing.StartSpan(ctx, fqdn+"/InboundNatRulesClient.Delete")
155		defer func() {
156			sc := -1
157			if result.Response() != nil {
158				sc = result.Response().StatusCode
159			}
160			tracing.EndSpan(ctx, sc, err)
161		}()
162	}
163	req, err := client.DeletePreparer(ctx, resourceGroupName, loadBalancerName, inboundNatRuleName)
164	if err != nil {
165		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "Delete", nil, "Failure preparing request")
166		return
167	}
168
169	result, err = client.DeleteSender(req)
170	if err != nil {
171		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "Delete", result.Response(), "Failure sending request")
172		return
173	}
174
175	return
176}
177
178// DeletePreparer prepares the Delete request.
179func (client InboundNatRulesClient) DeletePreparer(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string) (*http.Request, error) {
180	pathParameters := map[string]interface{}{
181		"inboundNatRuleName": autorest.Encode("path", inboundNatRuleName),
182		"loadBalancerName":   autorest.Encode("path", loadBalancerName),
183		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
184		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
185	}
186
187	const APIVersion = "2020-04-01"
188	queryParameters := map[string]interface{}{
189		"api-version": APIVersion,
190	}
191
192	preparer := autorest.CreatePreparer(
193		autorest.AsDelete(),
194		autorest.WithBaseURL(client.BaseURI),
195		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules/{inboundNatRuleName}", pathParameters),
196		autorest.WithQueryParameters(queryParameters))
197	return preparer.Prepare((&http.Request{}).WithContext(ctx))
198}
199
200// DeleteSender sends the Delete request. The method will close the
201// http.Response Body if it receives an error.
202func (client InboundNatRulesClient) DeleteSender(req *http.Request) (future InboundNatRulesDeleteFuture, err error) {
203	var resp *http.Response
204	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
205	if err != nil {
206		return
207	}
208	future.Future, err = azure.NewFutureFromResponse(resp)
209	return
210}
211
212// DeleteResponder handles the response to the Delete request. The method always
213// closes the http.Response Body.
214func (client InboundNatRulesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
215	err = autorest.Respond(
216		resp,
217		client.ByInspecting(),
218		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
219		autorest.ByClosing())
220	result.Response = resp
221	return
222}
223
224// Get gets the specified load balancer inbound nat rule.
225// Parameters:
226// resourceGroupName - the name of the resource group.
227// loadBalancerName - the name of the load balancer.
228// inboundNatRuleName - the name of the inbound nat rule.
229// expand - expands referenced resources.
230func (client InboundNatRulesClient) Get(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, expand string) (result InboundNatRule, err error) {
231	if tracing.IsEnabled() {
232		ctx = tracing.StartSpan(ctx, fqdn+"/InboundNatRulesClient.Get")
233		defer func() {
234			sc := -1
235			if result.Response.Response != nil {
236				sc = result.Response.Response.StatusCode
237			}
238			tracing.EndSpan(ctx, sc, err)
239		}()
240	}
241	req, err := client.GetPreparer(ctx, resourceGroupName, loadBalancerName, inboundNatRuleName, expand)
242	if err != nil {
243		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "Get", nil, "Failure preparing request")
244		return
245	}
246
247	resp, err := client.GetSender(req)
248	if err != nil {
249		result.Response = autorest.Response{Response: resp}
250		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "Get", resp, "Failure sending request")
251		return
252	}
253
254	result, err = client.GetResponder(resp)
255	if err != nil {
256		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "Get", resp, "Failure responding to request")
257	}
258
259	return
260}
261
262// GetPreparer prepares the Get request.
263func (client InboundNatRulesClient) GetPreparer(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, expand string) (*http.Request, error) {
264	pathParameters := map[string]interface{}{
265		"inboundNatRuleName": autorest.Encode("path", inboundNatRuleName),
266		"loadBalancerName":   autorest.Encode("path", loadBalancerName),
267		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
268		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
269	}
270
271	const APIVersion = "2020-04-01"
272	queryParameters := map[string]interface{}{
273		"api-version": APIVersion,
274	}
275	if len(expand) > 0 {
276		queryParameters["$expand"] = autorest.Encode("query", expand)
277	}
278
279	preparer := autorest.CreatePreparer(
280		autorest.AsGet(),
281		autorest.WithBaseURL(client.BaseURI),
282		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules/{inboundNatRuleName}", pathParameters),
283		autorest.WithQueryParameters(queryParameters))
284	return preparer.Prepare((&http.Request{}).WithContext(ctx))
285}
286
287// GetSender sends the Get request. The method will close the
288// http.Response Body if it receives an error.
289func (client InboundNatRulesClient) GetSender(req *http.Request) (*http.Response, error) {
290	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
291}
292
293// GetResponder handles the response to the Get request. The method always
294// closes the http.Response Body.
295func (client InboundNatRulesClient) GetResponder(resp *http.Response) (result InboundNatRule, err error) {
296	err = autorest.Respond(
297		resp,
298		client.ByInspecting(),
299		azure.WithErrorUnlessStatusCode(http.StatusOK),
300		autorest.ByUnmarshallingJSON(&result),
301		autorest.ByClosing())
302	result.Response = autorest.Response{Response: resp}
303	return
304}
305
306// List gets all the inbound nat rules in a load balancer.
307// Parameters:
308// resourceGroupName - the name of the resource group.
309// loadBalancerName - the name of the load balancer.
310func (client InboundNatRulesClient) List(ctx context.Context, resourceGroupName string, loadBalancerName string) (result InboundNatRuleListResultPage, err error) {
311	if tracing.IsEnabled() {
312		ctx = tracing.StartSpan(ctx, fqdn+"/InboundNatRulesClient.List")
313		defer func() {
314			sc := -1
315			if result.inrlr.Response.Response != nil {
316				sc = result.inrlr.Response.Response.StatusCode
317			}
318			tracing.EndSpan(ctx, sc, err)
319		}()
320	}
321	result.fn = client.listNextResults
322	req, err := client.ListPreparer(ctx, resourceGroupName, loadBalancerName)
323	if err != nil {
324		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "List", nil, "Failure preparing request")
325		return
326	}
327
328	resp, err := client.ListSender(req)
329	if err != nil {
330		result.inrlr.Response = autorest.Response{Response: resp}
331		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "List", resp, "Failure sending request")
332		return
333	}
334
335	result.inrlr, err = client.ListResponder(resp)
336	if err != nil {
337		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "List", resp, "Failure responding to request")
338	}
339
340	return
341}
342
343// ListPreparer prepares the List request.
344func (client InboundNatRulesClient) ListPreparer(ctx context.Context, resourceGroupName string, loadBalancerName string) (*http.Request, error) {
345	pathParameters := map[string]interface{}{
346		"loadBalancerName":  autorest.Encode("path", loadBalancerName),
347		"resourceGroupName": autorest.Encode("path", resourceGroupName),
348		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
349	}
350
351	const APIVersion = "2020-04-01"
352	queryParameters := map[string]interface{}{
353		"api-version": APIVersion,
354	}
355
356	preparer := autorest.CreatePreparer(
357		autorest.AsGet(),
358		autorest.WithBaseURL(client.BaseURI),
359		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules", pathParameters),
360		autorest.WithQueryParameters(queryParameters))
361	return preparer.Prepare((&http.Request{}).WithContext(ctx))
362}
363
364// ListSender sends the List request. The method will close the
365// http.Response Body if it receives an error.
366func (client InboundNatRulesClient) ListSender(req *http.Request) (*http.Response, error) {
367	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
368}
369
370// ListResponder handles the response to the List request. The method always
371// closes the http.Response Body.
372func (client InboundNatRulesClient) ListResponder(resp *http.Response) (result InboundNatRuleListResult, err error) {
373	err = autorest.Respond(
374		resp,
375		client.ByInspecting(),
376		azure.WithErrorUnlessStatusCode(http.StatusOK),
377		autorest.ByUnmarshallingJSON(&result),
378		autorest.ByClosing())
379	result.Response = autorest.Response{Response: resp}
380	return
381}
382
383// listNextResults retrieves the next set of results, if any.
384func (client InboundNatRulesClient) listNextResults(ctx context.Context, lastResults InboundNatRuleListResult) (result InboundNatRuleListResult, err error) {
385	req, err := lastResults.inboundNatRuleListResultPreparer(ctx)
386	if err != nil {
387		return result, autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "listNextResults", nil, "Failure preparing next results request")
388	}
389	if req == nil {
390		return
391	}
392	resp, err := client.ListSender(req)
393	if err != nil {
394		result.Response = autorest.Response{Response: resp}
395		return result, autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "listNextResults", resp, "Failure sending next results request")
396	}
397	result, err = client.ListResponder(resp)
398	if err != nil {
399		err = autorest.NewErrorWithError(err, "network.InboundNatRulesClient", "listNextResults", resp, "Failure responding to next results request")
400	}
401	return
402}
403
404// ListComplete enumerates all values, automatically crossing page boundaries as required.
405func (client InboundNatRulesClient) ListComplete(ctx context.Context, resourceGroupName string, loadBalancerName string) (result InboundNatRuleListResultIterator, err error) {
406	if tracing.IsEnabled() {
407		ctx = tracing.StartSpan(ctx, fqdn+"/InboundNatRulesClient.List")
408		defer func() {
409			sc := -1
410			if result.Response().Response.Response != nil {
411				sc = result.page.Response().Response.Response.StatusCode
412			}
413			tracing.EndSpan(ctx, sc, err)
414		}()
415	}
416	result.page, err = client.List(ctx, resourceGroupName, loadBalancerName)
417	return
418}
419