github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/natrules_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  // NatRulesClient contains the methods for the NatRules group.
    24  // Don't use this type directly, use NewNatRulesClient() instead.
    25  type NatRulesClient struct {
    26  	internal       *arm.Client
    27  	subscriptionID string
    28  }
    29  
    30  // NewNatRulesClient creates a new instance of NatRulesClient 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 NewNatRulesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*NatRulesClient, error) {
    36  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	client := &NatRulesClient{
    41  		subscriptionID: subscriptionID,
    42  		internal:       cl,
    43  	}
    44  	return client, nil
    45  }
    46  
    47  // BeginCreateOrUpdate - Creates a nat rule to a scalable vpn gateway if it doesn't exist else updates the existing nat rules.
    48  // If the operation fails it returns an *azcore.ResponseError type.
    49  //
    50  // Generated from API version 2024-05-01
    51  //   - resourceGroupName - The resource group name of the VpnGateway.
    52  //   - gatewayName - The name of the gateway.
    53  //   - natRuleName - The name of the nat rule.
    54  //   - natRuleParameters - Parameters supplied to create or Update a Nat Rule.
    55  //   - options - NatRulesClientBeginCreateOrUpdateOptions contains the optional parameters for the NatRulesClient.BeginCreateOrUpdate
    56  //     method.
    57  func (client *NatRulesClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, natRuleParameters VPNGatewayNatRule, options *NatRulesClientBeginCreateOrUpdateOptions) (*runtime.Poller[NatRulesClientCreateOrUpdateResponse], error) {
    58  	if options == nil || options.ResumeToken == "" {
    59  		resp, err := client.createOrUpdate(ctx, resourceGroupName, gatewayName, natRuleName, natRuleParameters, options)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[NatRulesClientCreateOrUpdateResponse]{
    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[NatRulesClientCreateOrUpdateResponse]{
    70  			Tracer: client.internal.Tracer(),
    71  		})
    72  	}
    73  }
    74  
    75  // CreateOrUpdate - Creates a nat rule to a scalable vpn gateway if it doesn't exist else updates the existing nat rules.
    76  // If the operation fails it returns an *azcore.ResponseError type.
    77  //
    78  // Generated from API version 2024-05-01
    79  func (client *NatRulesClient) createOrUpdate(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, natRuleParameters VPNGatewayNatRule, options *NatRulesClientBeginCreateOrUpdateOptions) (*http.Response, error) {
    80  	var err error
    81  	const operationName = "NatRulesClient.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, gatewayName, natRuleName, natRuleParameters, 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 *NatRulesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, natRuleParameters VPNGatewayNatRule, options *NatRulesClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
   102  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/vpnGateways/{gatewayName}/natRules/{natRuleName}"
   103  	if client.subscriptionID == "" {
   104  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   105  	}
   106  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   107  	if resourceGroupName == "" {
   108  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   109  	}
   110  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   111  	if gatewayName == "" {
   112  		return nil, errors.New("parameter gatewayName cannot be empty")
   113  	}
   114  	urlPath = strings.ReplaceAll(urlPath, "{gatewayName}", url.PathEscape(gatewayName))
   115  	if natRuleName == "" {
   116  		return nil, errors.New("parameter natRuleName cannot be empty")
   117  	}
   118  	urlPath = strings.ReplaceAll(urlPath, "{natRuleName}", url.PathEscape(natRuleName))
   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, natRuleParameters); err != nil {
   128  		return nil, err
   129  	}
   130  	return req, nil
   131  }
   132  
   133  // BeginDelete - Deletes a 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 resource group name of the VpnGateway.
   138  //   - gatewayName - The name of the gateway.
   139  //   - natRuleName - The name of the nat rule.
   140  //   - options - NatRulesClientBeginDeleteOptions contains the optional parameters for the NatRulesClient.BeginDelete method.
   141  func (client *NatRulesClient) BeginDelete(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, options *NatRulesClientBeginDeleteOptions) (*runtime.Poller[NatRulesClientDeleteResponse], error) {
   142  	if options == nil || options.ResumeToken == "" {
   143  		resp, err := client.deleteOperation(ctx, resourceGroupName, gatewayName, natRuleName, options)
   144  		if err != nil {
   145  			return nil, err
   146  		}
   147  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[NatRulesClientDeleteResponse]{
   148  			FinalStateVia: runtime.FinalStateViaLocation,
   149  			Tracer:        client.internal.Tracer(),
   150  		})
   151  		return poller, err
   152  	} else {
   153  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[NatRulesClientDeleteResponse]{
   154  			Tracer: client.internal.Tracer(),
   155  		})
   156  	}
   157  }
   158  
   159  // Delete - Deletes a nat rule.
   160  // If the operation fails it returns an *azcore.ResponseError type.
   161  //
   162  // Generated from API version 2024-05-01
   163  func (client *NatRulesClient) deleteOperation(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, options *NatRulesClientBeginDeleteOptions) (*http.Response, error) {
   164  	var err error
   165  	const operationName = "NatRulesClient.BeginDelete"
   166  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   167  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   168  	defer func() { endSpan(err) }()
   169  	req, err := client.deleteCreateRequest(ctx, resourceGroupName, gatewayName, natRuleName, options)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	httpResp, err := client.internal.Pipeline().Do(req)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
   178  		err = runtime.NewResponseError(httpResp)
   179  		return nil, err
   180  	}
   181  	return httpResp, nil
   182  }
   183  
   184  // deleteCreateRequest creates the Delete request.
   185  func (client *NatRulesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, options *NatRulesClientBeginDeleteOptions) (*policy.Request, error) {
   186  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/vpnGateways/{gatewayName}/natRules/{natRuleName}"
   187  	if client.subscriptionID == "" {
   188  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   189  	}
   190  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   191  	if resourceGroupName == "" {
   192  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   193  	}
   194  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   195  	if gatewayName == "" {
   196  		return nil, errors.New("parameter gatewayName cannot be empty")
   197  	}
   198  	urlPath = strings.ReplaceAll(urlPath, "{gatewayName}", url.PathEscape(gatewayName))
   199  	if natRuleName == "" {
   200  		return nil, errors.New("parameter natRuleName cannot be empty")
   201  	}
   202  	urlPath = strings.ReplaceAll(urlPath, "{natRuleName}", url.PathEscape(natRuleName))
   203  	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   204  	if err != nil {
   205  		return nil, err
   206  	}
   207  	reqQP := req.Raw().URL.Query()
   208  	reqQP.Set("api-version", "2024-05-01")
   209  	req.Raw().URL.RawQuery = reqQP.Encode()
   210  	req.Raw().Header["Accept"] = []string{"application/json"}
   211  	return req, nil
   212  }
   213  
   214  // Get - Retrieves the details of a nat ruleGet.
   215  // If the operation fails it returns an *azcore.ResponseError type.
   216  //
   217  // Generated from API version 2024-05-01
   218  //   - resourceGroupName - The resource group name of the VpnGateway.
   219  //   - gatewayName - The name of the gateway.
   220  //   - natRuleName - The name of the nat rule.
   221  //   - options - NatRulesClientGetOptions contains the optional parameters for the NatRulesClient.Get method.
   222  func (client *NatRulesClient) Get(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, options *NatRulesClientGetOptions) (NatRulesClientGetResponse, error) {
   223  	var err error
   224  	const operationName = "NatRulesClient.Get"
   225  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   226  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   227  	defer func() { endSpan(err) }()
   228  	req, err := client.getCreateRequest(ctx, resourceGroupName, gatewayName, natRuleName, options)
   229  	if err != nil {
   230  		return NatRulesClientGetResponse{}, err
   231  	}
   232  	httpResp, err := client.internal.Pipeline().Do(req)
   233  	if err != nil {
   234  		return NatRulesClientGetResponse{}, err
   235  	}
   236  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   237  		err = runtime.NewResponseError(httpResp)
   238  		return NatRulesClientGetResponse{}, err
   239  	}
   240  	resp, err := client.getHandleResponse(httpResp)
   241  	return resp, err
   242  }
   243  
   244  // getCreateRequest creates the Get request.
   245  func (client *NatRulesClient) getCreateRequest(ctx context.Context, resourceGroupName string, gatewayName string, natRuleName string, options *NatRulesClientGetOptions) (*policy.Request, error) {
   246  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/vpnGateways/{gatewayName}/natRules/{natRuleName}"
   247  	if client.subscriptionID == "" {
   248  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   249  	}
   250  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   251  	if resourceGroupName == "" {
   252  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   253  	}
   254  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   255  	if gatewayName == "" {
   256  		return nil, errors.New("parameter gatewayName cannot be empty")
   257  	}
   258  	urlPath = strings.ReplaceAll(urlPath, "{gatewayName}", url.PathEscape(gatewayName))
   259  	if natRuleName == "" {
   260  		return nil, errors.New("parameter natRuleName cannot be empty")
   261  	}
   262  	urlPath = strings.ReplaceAll(urlPath, "{natRuleName}", url.PathEscape(natRuleName))
   263  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   264  	if err != nil {
   265  		return nil, err
   266  	}
   267  	reqQP := req.Raw().URL.Query()
   268  	reqQP.Set("api-version", "2024-05-01")
   269  	req.Raw().URL.RawQuery = reqQP.Encode()
   270  	req.Raw().Header["Accept"] = []string{"application/json"}
   271  	return req, nil
   272  }
   273  
   274  // getHandleResponse handles the Get response.
   275  func (client *NatRulesClient) getHandleResponse(resp *http.Response) (NatRulesClientGetResponse, error) {
   276  	result := NatRulesClientGetResponse{}
   277  	if err := runtime.UnmarshalAsJSON(resp, &result.VPNGatewayNatRule); err != nil {
   278  		return NatRulesClientGetResponse{}, err
   279  	}
   280  	return result, nil
   281  }
   282  
   283  // NewListByVPNGatewayPager - Retrieves all nat rules for a particular virtual wan vpn gateway.
   284  //
   285  // Generated from API version 2024-05-01
   286  //   - resourceGroupName - The resource group name of the VpnGateway.
   287  //   - gatewayName - The name of the gateway.
   288  //   - options - NatRulesClientListByVPNGatewayOptions contains the optional parameters for the NatRulesClient.NewListByVPNGatewayPager
   289  //     method.
   290  func (client *NatRulesClient) NewListByVPNGatewayPager(resourceGroupName string, gatewayName string, options *NatRulesClientListByVPNGatewayOptions) *runtime.Pager[NatRulesClientListByVPNGatewayResponse] {
   291  	return runtime.NewPager(runtime.PagingHandler[NatRulesClientListByVPNGatewayResponse]{
   292  		More: func(page NatRulesClientListByVPNGatewayResponse) bool {
   293  			return page.NextLink != nil && len(*page.NextLink) > 0
   294  		},
   295  		Fetcher: func(ctx context.Context, page *NatRulesClientListByVPNGatewayResponse) (NatRulesClientListByVPNGatewayResponse, error) {
   296  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "NatRulesClient.NewListByVPNGatewayPager")
   297  			nextLink := ""
   298  			if page != nil {
   299  				nextLink = *page.NextLink
   300  			}
   301  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   302  				return client.listByVPNGatewayCreateRequest(ctx, resourceGroupName, gatewayName, options)
   303  			}, nil)
   304  			if err != nil {
   305  				return NatRulesClientListByVPNGatewayResponse{}, err
   306  			}
   307  			return client.listByVPNGatewayHandleResponse(resp)
   308  		},
   309  		Tracer: client.internal.Tracer(),
   310  	})
   311  }
   312  
   313  // listByVPNGatewayCreateRequest creates the ListByVPNGateway request.
   314  func (client *NatRulesClient) listByVPNGatewayCreateRequest(ctx context.Context, resourceGroupName string, gatewayName string, options *NatRulesClientListByVPNGatewayOptions) (*policy.Request, error) {
   315  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/vpnGateways/{gatewayName}/natRules"
   316  	if client.subscriptionID == "" {
   317  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   318  	}
   319  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   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 gatewayName == "" {
   325  		return nil, errors.New("parameter gatewayName cannot be empty")
   326  	}
   327  	urlPath = strings.ReplaceAll(urlPath, "{gatewayName}", url.PathEscape(gatewayName))
   328  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   329  	if err != nil {
   330  		return nil, err
   331  	}
   332  	reqQP := req.Raw().URL.Query()
   333  	reqQP.Set("api-version", "2024-05-01")
   334  	req.Raw().URL.RawQuery = reqQP.Encode()
   335  	req.Raw().Header["Accept"] = []string{"application/json"}
   336  	return req, nil
   337  }
   338  
   339  // listByVPNGatewayHandleResponse handles the ListByVPNGateway response.
   340  func (client *NatRulesClient) listByVPNGatewayHandleResponse(resp *http.Response) (NatRulesClientListByVPNGatewayResponse, error) {
   341  	result := NatRulesClientListByVPNGatewayResponse{}
   342  	if err := runtime.UnmarshalAsJSON(resp, &result.ListVPNGatewayNatRulesResult); err != nil {
   343  		return NatRulesClientListByVPNGatewayResponse{}, err
   344  	}
   345  	return result, nil
   346  }