github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/routingrules_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  	"strconv"
    21  	"strings"
    22  )
    23  
    24  // RoutingRulesClient contains the methods for the RoutingRules group.
    25  // Don't use this type directly, use NewRoutingRulesClient() instead.
    26  type RoutingRulesClient struct {
    27  	internal       *arm.Client
    28  	subscriptionID string
    29  }
    30  
    31  // NewRoutingRulesClient creates a new instance of RoutingRulesClient with the specified values.
    32  //   - subscriptionID - The subscription credentials which uniquely identify the Microsoft Azure subscription. The subscription
    33  //     ID forms part of the URI for every service call.
    34  //   - credential - used to authorize requests. Usually a credential from azidentity.
    35  //   - options - pass nil to accept the default values.
    36  func NewRoutingRulesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*RoutingRulesClient, error) {
    37  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	client := &RoutingRulesClient{
    42  		subscriptionID: subscriptionID,
    43  		internal:       cl,
    44  	}
    45  	return client, nil
    46  }
    47  
    48  // CreateOrUpdate - Creates or updates an routing rule.
    49  // If the operation fails it returns an *azcore.ResponseError type.
    50  //
    51  // Generated from API version 2024-05-01
    52  //   - resourceGroupName - The name of the resource group. The name is case insensitive.
    53  //   - networkManagerName - The name of the network manager.
    54  //   - configurationName - The name of the network manager Routing Configuration.
    55  //   - ruleCollectionName - The name of the network manager routing Configuration rule collection.
    56  //   - ruleName - The name of the rule.
    57  //   - routingRule - The routing rule to create or update
    58  //   - options - RoutingRulesClientCreateOrUpdateOptions contains the optional parameters for the RoutingRulesClient.CreateOrUpdate
    59  //     method.
    60  func (client *RoutingRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, routingRule RoutingRule, options *RoutingRulesClientCreateOrUpdateOptions) (RoutingRulesClientCreateOrUpdateResponse, error) {
    61  	var err error
    62  	const operationName = "RoutingRulesClient.CreateOrUpdate"
    63  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    64  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    65  	defer func() { endSpan(err) }()
    66  	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, ruleName, routingRule, options)
    67  	if err != nil {
    68  		return RoutingRulesClientCreateOrUpdateResponse{}, err
    69  	}
    70  	httpResp, err := client.internal.Pipeline().Do(req)
    71  	if err != nil {
    72  		return RoutingRulesClientCreateOrUpdateResponse{}, err
    73  	}
    74  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
    75  		err = runtime.NewResponseError(httpResp)
    76  		return RoutingRulesClientCreateOrUpdateResponse{}, err
    77  	}
    78  	resp, err := client.createOrUpdateHandleResponse(httpResp)
    79  	return resp, err
    80  }
    81  
    82  // createOrUpdateCreateRequest creates the CreateOrUpdate request.
    83  func (client *RoutingRulesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, routingRule RoutingRule, options *RoutingRulesClientCreateOrUpdateOptions) (*policy.Request, error) {
    84  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/routingConfigurations/{configurationName}/ruleCollections/{ruleCollectionName}/rules/{ruleName}"
    85  	if client.subscriptionID == "" {
    86  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
    87  	}
    88  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
    89  	if resourceGroupName == "" {
    90  		return nil, errors.New("parameter resourceGroupName cannot be empty")
    91  	}
    92  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
    93  	if networkManagerName == "" {
    94  		return nil, errors.New("parameter networkManagerName cannot be empty")
    95  	}
    96  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
    97  	if configurationName == "" {
    98  		return nil, errors.New("parameter configurationName cannot be empty")
    99  	}
   100  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   101  	if ruleCollectionName == "" {
   102  		return nil, errors.New("parameter ruleCollectionName cannot be empty")
   103  	}
   104  	urlPath = strings.ReplaceAll(urlPath, "{ruleCollectionName}", url.PathEscape(ruleCollectionName))
   105  	if ruleName == "" {
   106  		return nil, errors.New("parameter ruleName cannot be empty")
   107  	}
   108  	urlPath = strings.ReplaceAll(urlPath, "{ruleName}", url.PathEscape(ruleName))
   109  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	reqQP := req.Raw().URL.Query()
   114  	reqQP.Set("api-version", "2024-05-01")
   115  	req.Raw().URL.RawQuery = reqQP.Encode()
   116  	req.Raw().Header["Accept"] = []string{"application/json"}
   117  	if err := runtime.MarshalAsJSON(req, routingRule); err != nil {
   118  		return nil, err
   119  	}
   120  	return req, nil
   121  }
   122  
   123  // createOrUpdateHandleResponse handles the CreateOrUpdate response.
   124  func (client *RoutingRulesClient) createOrUpdateHandleResponse(resp *http.Response) (RoutingRulesClientCreateOrUpdateResponse, error) {
   125  	result := RoutingRulesClientCreateOrUpdateResponse{}
   126  	if err := runtime.UnmarshalAsJSON(resp, &result.RoutingRule); err != nil {
   127  		return RoutingRulesClientCreateOrUpdateResponse{}, err
   128  	}
   129  	return result, nil
   130  }
   131  
   132  // BeginDelete - Deletes a routing rule.
   133  // If the operation fails it returns an *azcore.ResponseError type.
   134  //
   135  // Generated from API version 2024-05-01
   136  //   - resourceGroupName - The name of the resource group. The name is case insensitive.
   137  //   - networkManagerName - The name of the network manager.
   138  //   - configurationName - The name of the network manager Routing Configuration.
   139  //   - ruleCollectionName - The name of the network manager routing Configuration rule collection.
   140  //   - ruleName - The name of the rule.
   141  //   - options - RoutingRulesClientBeginDeleteOptions contains the optional parameters for the RoutingRulesClient.BeginDelete
   142  //     method.
   143  func (client *RoutingRulesClient) BeginDelete(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, options *RoutingRulesClientBeginDeleteOptions) (*runtime.Poller[RoutingRulesClientDeleteResponse], error) {
   144  	if options == nil || options.ResumeToken == "" {
   145  		resp, err := client.deleteOperation(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, ruleName, options)
   146  		if err != nil {
   147  			return nil, err
   148  		}
   149  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[RoutingRulesClientDeleteResponse]{
   150  			FinalStateVia: runtime.FinalStateViaLocation,
   151  			Tracer:        client.internal.Tracer(),
   152  		})
   153  		return poller, err
   154  	} else {
   155  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[RoutingRulesClientDeleteResponse]{
   156  			Tracer: client.internal.Tracer(),
   157  		})
   158  	}
   159  }
   160  
   161  // Delete - Deletes a routing rule.
   162  // If the operation fails it returns an *azcore.ResponseError type.
   163  //
   164  // Generated from API version 2024-05-01
   165  func (client *RoutingRulesClient) deleteOperation(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, options *RoutingRulesClientBeginDeleteOptions) (*http.Response, error) {
   166  	var err error
   167  	const operationName = "RoutingRulesClient.BeginDelete"
   168  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   169  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   170  	defer func() { endSpan(err) }()
   171  	req, err := client.deleteCreateRequest(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, ruleName, options)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	httpResp, err := client.internal.Pipeline().Do(req)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
   180  		err = runtime.NewResponseError(httpResp)
   181  		return nil, err
   182  	}
   183  	return httpResp, nil
   184  }
   185  
   186  // deleteCreateRequest creates the Delete request.
   187  func (client *RoutingRulesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, options *RoutingRulesClientBeginDeleteOptions) (*policy.Request, error) {
   188  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/routingConfigurations/{configurationName}/ruleCollections/{ruleCollectionName}/rules/{ruleName}"
   189  	if client.subscriptionID == "" {
   190  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   191  	}
   192  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   193  	if resourceGroupName == "" {
   194  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   195  	}
   196  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   197  	if networkManagerName == "" {
   198  		return nil, errors.New("parameter networkManagerName cannot be empty")
   199  	}
   200  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   201  	if configurationName == "" {
   202  		return nil, errors.New("parameter configurationName cannot be empty")
   203  	}
   204  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   205  	if ruleCollectionName == "" {
   206  		return nil, errors.New("parameter ruleCollectionName cannot be empty")
   207  	}
   208  	urlPath = strings.ReplaceAll(urlPath, "{ruleCollectionName}", url.PathEscape(ruleCollectionName))
   209  	if ruleName == "" {
   210  		return nil, errors.New("parameter ruleName cannot be empty")
   211  	}
   212  	urlPath = strings.ReplaceAll(urlPath, "{ruleName}", url.PathEscape(ruleName))
   213  	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  	reqQP := req.Raw().URL.Query()
   218  	reqQP.Set("api-version", "2024-05-01")
   219  	if options != nil && options.Force != nil {
   220  		reqQP.Set("force", strconv.FormatBool(*options.Force))
   221  	}
   222  	req.Raw().URL.RawQuery = reqQP.Encode()
   223  	req.Raw().Header["Accept"] = []string{"application/json"}
   224  	return req, nil
   225  }
   226  
   227  // Get - Gets a network manager routing configuration routing rule.
   228  // If the operation fails it returns an *azcore.ResponseError type.
   229  //
   230  // Generated from API version 2024-05-01
   231  //   - resourceGroupName - The name of the resource group. The name is case insensitive.
   232  //   - networkManagerName - The name of the network manager.
   233  //   - configurationName - The name of the network manager Routing Configuration.
   234  //   - ruleCollectionName - The name of the network manager routing Configuration rule collection.
   235  //   - ruleName - The name of the rule.
   236  //   - options - RoutingRulesClientGetOptions contains the optional parameters for the RoutingRulesClient.Get method.
   237  func (client *RoutingRulesClient) Get(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, options *RoutingRulesClientGetOptions) (RoutingRulesClientGetResponse, error) {
   238  	var err error
   239  	const operationName = "RoutingRulesClient.Get"
   240  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   241  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   242  	defer func() { endSpan(err) }()
   243  	req, err := client.getCreateRequest(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, ruleName, options)
   244  	if err != nil {
   245  		return RoutingRulesClientGetResponse{}, err
   246  	}
   247  	httpResp, err := client.internal.Pipeline().Do(req)
   248  	if err != nil {
   249  		return RoutingRulesClientGetResponse{}, err
   250  	}
   251  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   252  		err = runtime.NewResponseError(httpResp)
   253  		return RoutingRulesClientGetResponse{}, err
   254  	}
   255  	resp, err := client.getHandleResponse(httpResp)
   256  	return resp, err
   257  }
   258  
   259  // getCreateRequest creates the Get request.
   260  func (client *RoutingRulesClient) getCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, ruleName string, options *RoutingRulesClientGetOptions) (*policy.Request, error) {
   261  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/routingConfigurations/{configurationName}/ruleCollections/{ruleCollectionName}/rules/{ruleName}"
   262  	if client.subscriptionID == "" {
   263  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   264  	}
   265  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   266  	if resourceGroupName == "" {
   267  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   268  	}
   269  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   270  	if networkManagerName == "" {
   271  		return nil, errors.New("parameter networkManagerName cannot be empty")
   272  	}
   273  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   274  	if configurationName == "" {
   275  		return nil, errors.New("parameter configurationName cannot be empty")
   276  	}
   277  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   278  	if ruleCollectionName == "" {
   279  		return nil, errors.New("parameter ruleCollectionName cannot be empty")
   280  	}
   281  	urlPath = strings.ReplaceAll(urlPath, "{ruleCollectionName}", url.PathEscape(ruleCollectionName))
   282  	if ruleName == "" {
   283  		return nil, errors.New("parameter ruleName cannot be empty")
   284  	}
   285  	urlPath = strings.ReplaceAll(urlPath, "{ruleName}", url.PathEscape(ruleName))
   286  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   287  	if err != nil {
   288  		return nil, err
   289  	}
   290  	reqQP := req.Raw().URL.Query()
   291  	reqQP.Set("api-version", "2024-05-01")
   292  	req.Raw().URL.RawQuery = reqQP.Encode()
   293  	req.Raw().Header["Accept"] = []string{"application/json"}
   294  	return req, nil
   295  }
   296  
   297  // getHandleResponse handles the Get response.
   298  func (client *RoutingRulesClient) getHandleResponse(resp *http.Response) (RoutingRulesClientGetResponse, error) {
   299  	result := RoutingRulesClientGetResponse{}
   300  	if err := runtime.UnmarshalAsJSON(resp, &result.RoutingRule); err != nil {
   301  		return RoutingRulesClientGetResponse{}, err
   302  	}
   303  	return result, nil
   304  }
   305  
   306  // NewListPager - List all network manager routing configuration routing rules.
   307  //
   308  // Generated from API version 2024-05-01
   309  //   - resourceGroupName - The name of the resource group. The name is case insensitive.
   310  //   - networkManagerName - The name of the network manager.
   311  //   - configurationName - The name of the network manager Routing Configuration.
   312  //   - ruleCollectionName - The name of the network manager routing Configuration rule collection.
   313  //   - options - RoutingRulesClientListOptions contains the optional parameters for the RoutingRulesClient.NewListPager method.
   314  func (client *RoutingRulesClient) NewListPager(resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *RoutingRulesClientListOptions) *runtime.Pager[RoutingRulesClientListResponse] {
   315  	return runtime.NewPager(runtime.PagingHandler[RoutingRulesClientListResponse]{
   316  		More: func(page RoutingRulesClientListResponse) bool {
   317  			return page.NextLink != nil && len(*page.NextLink) > 0
   318  		},
   319  		Fetcher: func(ctx context.Context, page *RoutingRulesClientListResponse) (RoutingRulesClientListResponse, error) {
   320  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "RoutingRulesClient.NewListPager")
   321  			nextLink := ""
   322  			if page != nil {
   323  				nextLink = *page.NextLink
   324  			}
   325  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   326  				return client.listCreateRequest(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, options)
   327  			}, nil)
   328  			if err != nil {
   329  				return RoutingRulesClientListResponse{}, err
   330  			}
   331  			return client.listHandleResponse(resp)
   332  		},
   333  		Tracer: client.internal.Tracer(),
   334  	})
   335  }
   336  
   337  // listCreateRequest creates the List request.
   338  func (client *RoutingRulesClient) listCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *RoutingRulesClientListOptions) (*policy.Request, error) {
   339  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/routingConfigurations/{configurationName}/ruleCollections/{ruleCollectionName}/rules"
   340  	if client.subscriptionID == "" {
   341  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   342  	}
   343  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   344  	if resourceGroupName == "" {
   345  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   346  	}
   347  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   348  	if networkManagerName == "" {
   349  		return nil, errors.New("parameter networkManagerName cannot be empty")
   350  	}
   351  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   352  	if configurationName == "" {
   353  		return nil, errors.New("parameter configurationName cannot be empty")
   354  	}
   355  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   356  	if ruleCollectionName == "" {
   357  		return nil, errors.New("parameter ruleCollectionName cannot be empty")
   358  	}
   359  	urlPath = strings.ReplaceAll(urlPath, "{ruleCollectionName}", url.PathEscape(ruleCollectionName))
   360  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   361  	if err != nil {
   362  		return nil, err
   363  	}
   364  	reqQP := req.Raw().URL.Query()
   365  	if options != nil && options.SkipToken != nil {
   366  		reqQP.Set("$skipToken", *options.SkipToken)
   367  	}
   368  	if options != nil && options.Top != nil {
   369  		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
   370  	}
   371  	reqQP.Set("api-version", "2024-05-01")
   372  	req.Raw().URL.RawQuery = reqQP.Encode()
   373  	req.Raw().Header["Accept"] = []string{"application/json"}
   374  	return req, nil
   375  }
   376  
   377  // listHandleResponse handles the List response.
   378  func (client *RoutingRulesClient) listHandleResponse(resp *http.Response) (RoutingRulesClientListResponse, error) {
   379  	result := RoutingRulesClientListResponse{}
   380  	if err := runtime.UnmarshalAsJSON(resp, &result.RoutingRuleListResult); err != nil {
   381  		return RoutingRulesClientListResponse{}, err
   382  	}
   383  	return result, nil
   384  }