github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/inboundnatrules_client.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  // Copyright (c) Microsoft Corporation. All rights reserved.
     5  // Licensed under the MIT License. See License.txt in the project root for license information.
     6  // Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
     7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
     8  
     9  package armnetwork
    10  
    11  import (
    12  	"context"
    13  	"errors"
    14  	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
    15  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
    16  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
    17  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
    18  	"net/http"
    19  	"net/url"
    20  	"strings"
    21  )
    22  
    23  // InboundNatRulesClient contains the methods for the InboundNatRules group.
    24  // Don't use this type directly, use NewInboundNatRulesClient() instead.
    25  type InboundNatRulesClient struct {
    26  	internal       *arm.Client
    27  	subscriptionID string
    28  }
    29  
    30  // NewInboundNatRulesClient creates a new instance of InboundNatRulesClient with the specified values.
    31  //   - subscriptionID - The subscription credentials which uniquely identify the Microsoft Azure subscription. The subscription
    32  //     ID forms part of the URI for every service call.
    33  //   - credential - used to authorize requests. Usually a credential from azidentity.
    34  //   - options - pass nil to accept the default values.
    35  func NewInboundNatRulesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*InboundNatRulesClient, error) {
    36  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	client := &InboundNatRulesClient{
    41  		subscriptionID: subscriptionID,
    42  		internal:       cl,
    43  	}
    44  	return client, nil
    45  }
    46  
    47  // BeginCreateOrUpdate - Creates or updates a load balancer inbound NAT rule.
    48  // If the operation fails it returns an *azcore.ResponseError type.
    49  //
    50  // Generated from API version 2024-05-01
    51  //   - resourceGroupName - The name of the resource group.
    52  //   - loadBalancerName - The name of the load balancer.
    53  //   - inboundNatRuleName - The name of the inbound NAT rule.
    54  //   - inboundNatRuleParameters - Parameters supplied to the create or update inbound NAT rule operation.
    55  //   - options - InboundNatRulesClientBeginCreateOrUpdateOptions contains the optional parameters for the InboundNatRulesClient.BeginCreateOrUpdate
    56  //     method.
    57  func (client *InboundNatRulesClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, inboundNatRuleParameters InboundNatRule, options *InboundNatRulesClientBeginCreateOrUpdateOptions) (*runtime.Poller[InboundNatRulesClientCreateOrUpdateResponse], error) {
    58  	if options == nil || options.ResumeToken == "" {
    59  		resp, err := client.createOrUpdate(ctx, resourceGroupName, loadBalancerName, inboundNatRuleName, inboundNatRuleParameters, options)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InboundNatRulesClientCreateOrUpdateResponse]{
    64  			FinalStateVia: runtime.FinalStateViaAzureAsyncOp,
    65  			Tracer:        client.internal.Tracer(),
    66  		})
    67  		return poller, err
    68  	} else {
    69  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InboundNatRulesClientCreateOrUpdateResponse]{
    70  			Tracer: client.internal.Tracer(),
    71  		})
    72  	}
    73  }
    74  
    75  // CreateOrUpdate - Creates or updates a load balancer inbound NAT rule.
    76  // If the operation fails it returns an *azcore.ResponseError type.
    77  //
    78  // Generated from API version 2024-05-01
    79  func (client *InboundNatRulesClient) createOrUpdate(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, inboundNatRuleParameters InboundNatRule, options *InboundNatRulesClientBeginCreateOrUpdateOptions) (*http.Response, error) {
    80  	var err error
    81  	const operationName = "InboundNatRulesClient.BeginCreateOrUpdate"
    82  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    83  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    84  	defer func() { endSpan(err) }()
    85  	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, loadBalancerName, inboundNatRuleName, inboundNatRuleParameters, options)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	httpResp, err := client.internal.Pipeline().Do(req)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
    94  		err = runtime.NewResponseError(httpResp)
    95  		return nil, err
    96  	}
    97  	return httpResp, nil
    98  }
    99  
   100  // createOrUpdateCreateRequest creates the CreateOrUpdate request.
   101  func (client *InboundNatRulesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, inboundNatRuleParameters InboundNatRule, options *InboundNatRulesClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
   102  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules/{inboundNatRuleName}"
   103  	if resourceGroupName == "" {
   104  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   105  	}
   106  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   107  	if loadBalancerName == "" {
   108  		return nil, errors.New("parameter loadBalancerName cannot be empty")
   109  	}
   110  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancerName}", url.PathEscape(loadBalancerName))
   111  	if inboundNatRuleName == "" {
   112  		return nil, errors.New("parameter inboundNatRuleName cannot be empty")
   113  	}
   114  	urlPath = strings.ReplaceAll(urlPath, "{inboundNatRuleName}", url.PathEscape(inboundNatRuleName))
   115  	if client.subscriptionID == "" {
   116  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   117  	}
   118  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   119  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	reqQP := req.Raw().URL.Query()
   124  	reqQP.Set("api-version", "2024-05-01")
   125  	req.Raw().URL.RawQuery = reqQP.Encode()
   126  	req.Raw().Header["Accept"] = []string{"application/json"}
   127  	if err := runtime.MarshalAsJSON(req, inboundNatRuleParameters); err != nil {
   128  		return nil, err
   129  	}
   130  	return req, nil
   131  }
   132  
   133  // BeginDelete - Deletes the specified load balancer inbound NAT rule.
   134  // If the operation fails it returns an *azcore.ResponseError type.
   135  //
   136  // Generated from API version 2024-05-01
   137  //   - resourceGroupName - The name of the resource group.
   138  //   - loadBalancerName - The name of the load balancer.
   139  //   - inboundNatRuleName - The name of the inbound NAT rule.
   140  //   - options - InboundNatRulesClientBeginDeleteOptions contains the optional parameters for the InboundNatRulesClient.BeginDelete
   141  //     method.
   142  func (client *InboundNatRulesClient) BeginDelete(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, options *InboundNatRulesClientBeginDeleteOptions) (*runtime.Poller[InboundNatRulesClientDeleteResponse], error) {
   143  	if options == nil || options.ResumeToken == "" {
   144  		resp, err := client.deleteOperation(ctx, resourceGroupName, loadBalancerName, inboundNatRuleName, options)
   145  		if err != nil {
   146  			return nil, err
   147  		}
   148  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InboundNatRulesClientDeleteResponse]{
   149  			FinalStateVia: runtime.FinalStateViaLocation,
   150  			Tracer:        client.internal.Tracer(),
   151  		})
   152  		return poller, err
   153  	} else {
   154  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InboundNatRulesClientDeleteResponse]{
   155  			Tracer: client.internal.Tracer(),
   156  		})
   157  	}
   158  }
   159  
   160  // Delete - Deletes the specified load balancer inbound NAT rule.
   161  // If the operation fails it returns an *azcore.ResponseError type.
   162  //
   163  // Generated from API version 2024-05-01
   164  func (client *InboundNatRulesClient) deleteOperation(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, options *InboundNatRulesClientBeginDeleteOptions) (*http.Response, error) {
   165  	var err error
   166  	const operationName = "InboundNatRulesClient.BeginDelete"
   167  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   168  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   169  	defer func() { endSpan(err) }()
   170  	req, err := client.deleteCreateRequest(ctx, resourceGroupName, loadBalancerName, inboundNatRuleName, options)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	httpResp, err := client.internal.Pipeline().Do(req)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
   179  		err = runtime.NewResponseError(httpResp)
   180  		return nil, err
   181  	}
   182  	return httpResp, nil
   183  }
   184  
   185  // deleteCreateRequest creates the Delete request.
   186  func (client *InboundNatRulesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, options *InboundNatRulesClientBeginDeleteOptions) (*policy.Request, error) {
   187  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules/{inboundNatRuleName}"
   188  	if resourceGroupName == "" {
   189  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   190  	}
   191  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   192  	if loadBalancerName == "" {
   193  		return nil, errors.New("parameter loadBalancerName cannot be empty")
   194  	}
   195  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancerName}", url.PathEscape(loadBalancerName))
   196  	if inboundNatRuleName == "" {
   197  		return nil, errors.New("parameter inboundNatRuleName cannot be empty")
   198  	}
   199  	urlPath = strings.ReplaceAll(urlPath, "{inboundNatRuleName}", url.PathEscape(inboundNatRuleName))
   200  	if client.subscriptionID == "" {
   201  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   202  	}
   203  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   204  	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	reqQP := req.Raw().URL.Query()
   209  	reqQP.Set("api-version", "2024-05-01")
   210  	req.Raw().URL.RawQuery = reqQP.Encode()
   211  	req.Raw().Header["Accept"] = []string{"application/json"}
   212  	return req, nil
   213  }
   214  
   215  // Get - Gets the specified load balancer inbound NAT rule.
   216  // If the operation fails it returns an *azcore.ResponseError type.
   217  //
   218  // Generated from API version 2024-05-01
   219  //   - resourceGroupName - The name of the resource group.
   220  //   - loadBalancerName - The name of the load balancer.
   221  //   - inboundNatRuleName - The name of the inbound NAT rule.
   222  //   - options - InboundNatRulesClientGetOptions contains the optional parameters for the InboundNatRulesClient.Get method.
   223  func (client *InboundNatRulesClient) Get(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, options *InboundNatRulesClientGetOptions) (InboundNatRulesClientGetResponse, error) {
   224  	var err error
   225  	const operationName = "InboundNatRulesClient.Get"
   226  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   227  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   228  	defer func() { endSpan(err) }()
   229  	req, err := client.getCreateRequest(ctx, resourceGroupName, loadBalancerName, inboundNatRuleName, options)
   230  	if err != nil {
   231  		return InboundNatRulesClientGetResponse{}, err
   232  	}
   233  	httpResp, err := client.internal.Pipeline().Do(req)
   234  	if err != nil {
   235  		return InboundNatRulesClientGetResponse{}, err
   236  	}
   237  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   238  		err = runtime.NewResponseError(httpResp)
   239  		return InboundNatRulesClientGetResponse{}, err
   240  	}
   241  	resp, err := client.getHandleResponse(httpResp)
   242  	return resp, err
   243  }
   244  
   245  // getCreateRequest creates the Get request.
   246  func (client *InboundNatRulesClient) getCreateRequest(ctx context.Context, resourceGroupName string, loadBalancerName string, inboundNatRuleName string, options *InboundNatRulesClientGetOptions) (*policy.Request, error) {
   247  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules/{inboundNatRuleName}"
   248  	if resourceGroupName == "" {
   249  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   250  	}
   251  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   252  	if loadBalancerName == "" {
   253  		return nil, errors.New("parameter loadBalancerName cannot be empty")
   254  	}
   255  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancerName}", url.PathEscape(loadBalancerName))
   256  	if inboundNatRuleName == "" {
   257  		return nil, errors.New("parameter inboundNatRuleName cannot be empty")
   258  	}
   259  	urlPath = strings.ReplaceAll(urlPath, "{inboundNatRuleName}", url.PathEscape(inboundNatRuleName))
   260  	if client.subscriptionID == "" {
   261  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   262  	}
   263  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   264  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   265  	if err != nil {
   266  		return nil, err
   267  	}
   268  	reqQP := req.Raw().URL.Query()
   269  	if options != nil && options.Expand != nil {
   270  		reqQP.Set("$expand", *options.Expand)
   271  	}
   272  	reqQP.Set("api-version", "2024-05-01")
   273  	req.Raw().URL.RawQuery = reqQP.Encode()
   274  	req.Raw().Header["Accept"] = []string{"application/json"}
   275  	return req, nil
   276  }
   277  
   278  // getHandleResponse handles the Get response.
   279  func (client *InboundNatRulesClient) getHandleResponse(resp *http.Response) (InboundNatRulesClientGetResponse, error) {
   280  	result := InboundNatRulesClientGetResponse{}
   281  	if err := runtime.UnmarshalAsJSON(resp, &result.InboundNatRule); err != nil {
   282  		return InboundNatRulesClientGetResponse{}, err
   283  	}
   284  	return result, nil
   285  }
   286  
   287  // NewListPager - Gets all the inbound NAT rules in a load balancer.
   288  //
   289  // Generated from API version 2024-05-01
   290  //   - resourceGroupName - The name of the resource group.
   291  //   - loadBalancerName - The name of the load balancer.
   292  //   - options - InboundNatRulesClientListOptions contains the optional parameters for the InboundNatRulesClient.NewListPager
   293  //     method.
   294  func (client *InboundNatRulesClient) NewListPager(resourceGroupName string, loadBalancerName string, options *InboundNatRulesClientListOptions) *runtime.Pager[InboundNatRulesClientListResponse] {
   295  	return runtime.NewPager(runtime.PagingHandler[InboundNatRulesClientListResponse]{
   296  		More: func(page InboundNatRulesClientListResponse) bool {
   297  			return page.NextLink != nil && len(*page.NextLink) > 0
   298  		},
   299  		Fetcher: func(ctx context.Context, page *InboundNatRulesClientListResponse) (InboundNatRulesClientListResponse, error) {
   300  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "InboundNatRulesClient.NewListPager")
   301  			nextLink := ""
   302  			if page != nil {
   303  				nextLink = *page.NextLink
   304  			}
   305  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   306  				return client.listCreateRequest(ctx, resourceGroupName, loadBalancerName, options)
   307  			}, nil)
   308  			if err != nil {
   309  				return InboundNatRulesClientListResponse{}, err
   310  			}
   311  			return client.listHandleResponse(resp)
   312  		},
   313  		Tracer: client.internal.Tracer(),
   314  	})
   315  }
   316  
   317  // listCreateRequest creates the List request.
   318  func (client *InboundNatRulesClient) listCreateRequest(ctx context.Context, resourceGroupName string, loadBalancerName string, options *InboundNatRulesClientListOptions) (*policy.Request, error) {
   319  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/inboundNatRules"
   320  	if resourceGroupName == "" {
   321  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   322  	}
   323  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   324  	if loadBalancerName == "" {
   325  		return nil, errors.New("parameter loadBalancerName cannot be empty")
   326  	}
   327  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancerName}", url.PathEscape(loadBalancerName))
   328  	if client.subscriptionID == "" {
   329  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   330  	}
   331  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   332  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   333  	if err != nil {
   334  		return nil, err
   335  	}
   336  	reqQP := req.Raw().URL.Query()
   337  	reqQP.Set("api-version", "2024-05-01")
   338  	req.Raw().URL.RawQuery = reqQP.Encode()
   339  	req.Raw().Header["Accept"] = []string{"application/json"}
   340  	return req, nil
   341  }
   342  
   343  // listHandleResponse handles the List response.
   344  func (client *InboundNatRulesClient) listHandleResponse(resp *http.Response) (InboundNatRulesClientListResponse, error) {
   345  	result := InboundNatRulesClientListResponse{}
   346  	if err := runtime.UnmarshalAsJSON(resp, &result.InboundNatRuleListResult); err != nil {
   347  		return InboundNatRulesClientListResponse{}, err
   348  	}
   349  	return result, nil
   350  }