github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/loadbalancerloadbalancingrules_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  // LoadBalancerLoadBalancingRulesClient contains the methods for the LoadBalancerLoadBalancingRules group.
    24  // Don't use this type directly, use NewLoadBalancerLoadBalancingRulesClient() instead.
    25  type LoadBalancerLoadBalancingRulesClient struct {
    26  	internal       *arm.Client
    27  	subscriptionID string
    28  }
    29  
    30  // NewLoadBalancerLoadBalancingRulesClient creates a new instance of LoadBalancerLoadBalancingRulesClient 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 NewLoadBalancerLoadBalancingRulesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*LoadBalancerLoadBalancingRulesClient, error) {
    36  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	client := &LoadBalancerLoadBalancingRulesClient{
    41  		subscriptionID: subscriptionID,
    42  		internal:       cl,
    43  	}
    44  	return client, nil
    45  }
    46  
    47  // Get - Gets the specified load balancer load balancing 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  //   - loadBalancingRuleName - The name of the load balancing rule.
    54  //   - options - LoadBalancerLoadBalancingRulesClientGetOptions contains the optional parameters for the LoadBalancerLoadBalancingRulesClient.Get
    55  //     method.
    56  func (client *LoadBalancerLoadBalancingRulesClient) Get(ctx context.Context, resourceGroupName string, loadBalancerName string, loadBalancingRuleName string, options *LoadBalancerLoadBalancingRulesClientGetOptions) (LoadBalancerLoadBalancingRulesClientGetResponse, error) {
    57  	var err error
    58  	const operationName = "LoadBalancerLoadBalancingRulesClient.Get"
    59  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    60  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    61  	defer func() { endSpan(err) }()
    62  	req, err := client.getCreateRequest(ctx, resourceGroupName, loadBalancerName, loadBalancingRuleName, options)
    63  	if err != nil {
    64  		return LoadBalancerLoadBalancingRulesClientGetResponse{}, err
    65  	}
    66  	httpResp, err := client.internal.Pipeline().Do(req)
    67  	if err != nil {
    68  		return LoadBalancerLoadBalancingRulesClientGetResponse{}, err
    69  	}
    70  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
    71  		err = runtime.NewResponseError(httpResp)
    72  		return LoadBalancerLoadBalancingRulesClientGetResponse{}, err
    73  	}
    74  	resp, err := client.getHandleResponse(httpResp)
    75  	return resp, err
    76  }
    77  
    78  // getCreateRequest creates the Get request.
    79  func (client *LoadBalancerLoadBalancingRulesClient) getCreateRequest(ctx context.Context, resourceGroupName string, loadBalancerName string, loadBalancingRuleName string, options *LoadBalancerLoadBalancingRulesClientGetOptions) (*policy.Request, error) {
    80  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/loadBalancingRules/{loadBalancingRuleName}"
    81  	if resourceGroupName == "" {
    82  		return nil, errors.New("parameter resourceGroupName cannot be empty")
    83  	}
    84  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
    85  	if loadBalancerName == "" {
    86  		return nil, errors.New("parameter loadBalancerName cannot be empty")
    87  	}
    88  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancerName}", url.PathEscape(loadBalancerName))
    89  	if loadBalancingRuleName == "" {
    90  		return nil, errors.New("parameter loadBalancingRuleName cannot be empty")
    91  	}
    92  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancingRuleName}", url.PathEscape(loadBalancingRuleName))
    93  	if client.subscriptionID == "" {
    94  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
    95  	}
    96  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
    97  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	reqQP := req.Raw().URL.Query()
   102  	reqQP.Set("api-version", "2024-05-01")
   103  	req.Raw().URL.RawQuery = reqQP.Encode()
   104  	req.Raw().Header["Accept"] = []string{"application/json"}
   105  	return req, nil
   106  }
   107  
   108  // getHandleResponse handles the Get response.
   109  func (client *LoadBalancerLoadBalancingRulesClient) getHandleResponse(resp *http.Response) (LoadBalancerLoadBalancingRulesClientGetResponse, error) {
   110  	result := LoadBalancerLoadBalancingRulesClientGetResponse{}
   111  	if err := runtime.UnmarshalAsJSON(resp, &result.LoadBalancingRule); err != nil {
   112  		return LoadBalancerLoadBalancingRulesClientGetResponse{}, err
   113  	}
   114  	return result, nil
   115  }
   116  
   117  // BeginHealth - Get health details of a load balancing rule.
   118  // If the operation fails it returns an *azcore.ResponseError type.
   119  //
   120  // Generated from API version 2024-05-01
   121  //   - groupName - The name of the resource group.
   122  //   - loadBalancerName - The name of the load balancer.
   123  //   - loadBalancingRuleName - The name of the load balancing rule.
   124  //   - options - LoadBalancerLoadBalancingRulesClientBeginHealthOptions contains the optional parameters for the LoadBalancerLoadBalancingRulesClient.BeginHealth
   125  //     method.
   126  func (client *LoadBalancerLoadBalancingRulesClient) BeginHealth(ctx context.Context, groupName string, loadBalancerName string, loadBalancingRuleName string, options *LoadBalancerLoadBalancingRulesClientBeginHealthOptions) (*runtime.Poller[LoadBalancerLoadBalancingRulesClientHealthResponse], error) {
   127  	if options == nil || options.ResumeToken == "" {
   128  		resp, err := client.health(ctx, groupName, loadBalancerName, loadBalancingRuleName, options)
   129  		if err != nil {
   130  			return nil, err
   131  		}
   132  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[LoadBalancerLoadBalancingRulesClientHealthResponse]{
   133  			FinalStateVia: runtime.FinalStateViaLocation,
   134  			Tracer:        client.internal.Tracer(),
   135  		})
   136  		return poller, err
   137  	} else {
   138  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[LoadBalancerLoadBalancingRulesClientHealthResponse]{
   139  			Tracer: client.internal.Tracer(),
   140  		})
   141  	}
   142  }
   143  
   144  // Health - Get health details of a load balancing rule.
   145  // If the operation fails it returns an *azcore.ResponseError type.
   146  //
   147  // Generated from API version 2024-05-01
   148  func (client *LoadBalancerLoadBalancingRulesClient) health(ctx context.Context, groupName string, loadBalancerName string, loadBalancingRuleName string, options *LoadBalancerLoadBalancingRulesClientBeginHealthOptions) (*http.Response, error) {
   149  	var err error
   150  	const operationName = "LoadBalancerLoadBalancingRulesClient.BeginHealth"
   151  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   152  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   153  	defer func() { endSpan(err) }()
   154  	req, err := client.healthCreateRequest(ctx, groupName, loadBalancerName, loadBalancingRuleName, options)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	httpResp, err := client.internal.Pipeline().Do(req)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
   163  		err = runtime.NewResponseError(httpResp)
   164  		return nil, err
   165  	}
   166  	return httpResp, nil
   167  }
   168  
   169  // healthCreateRequest creates the Health request.
   170  func (client *LoadBalancerLoadBalancingRulesClient) healthCreateRequest(ctx context.Context, groupName string, loadBalancerName string, loadBalancingRuleName string, options *LoadBalancerLoadBalancingRulesClientBeginHealthOptions) (*policy.Request, error) {
   171  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/loadBalancingRules/{loadBalancingRuleName}/health"
   172  	if groupName == "" {
   173  		return nil, errors.New("parameter groupName cannot be empty")
   174  	}
   175  	urlPath = strings.ReplaceAll(urlPath, "{groupName}", url.PathEscape(groupName))
   176  	if loadBalancerName == "" {
   177  		return nil, errors.New("parameter loadBalancerName cannot be empty")
   178  	}
   179  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancerName}", url.PathEscape(loadBalancerName))
   180  	if loadBalancingRuleName == "" {
   181  		return nil, errors.New("parameter loadBalancingRuleName cannot be empty")
   182  	}
   183  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancingRuleName}", url.PathEscape(loadBalancingRuleName))
   184  	if client.subscriptionID == "" {
   185  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   186  	}
   187  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   188  	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  	reqQP := req.Raw().URL.Query()
   193  	reqQP.Set("api-version", "2024-05-01")
   194  	req.Raw().URL.RawQuery = reqQP.Encode()
   195  	req.Raw().Header["Accept"] = []string{"application/json"}
   196  	return req, nil
   197  }
   198  
   199  // NewListPager - Gets all the load balancing rules in a load balancer.
   200  //
   201  // Generated from API version 2024-05-01
   202  //   - resourceGroupName - The name of the resource group.
   203  //   - loadBalancerName - The name of the load balancer.
   204  //   - options - LoadBalancerLoadBalancingRulesClientListOptions contains the optional parameters for the LoadBalancerLoadBalancingRulesClient.NewListPager
   205  //     method.
   206  func (client *LoadBalancerLoadBalancingRulesClient) NewListPager(resourceGroupName string, loadBalancerName string, options *LoadBalancerLoadBalancingRulesClientListOptions) *runtime.Pager[LoadBalancerLoadBalancingRulesClientListResponse] {
   207  	return runtime.NewPager(runtime.PagingHandler[LoadBalancerLoadBalancingRulesClientListResponse]{
   208  		More: func(page LoadBalancerLoadBalancingRulesClientListResponse) bool {
   209  			return page.NextLink != nil && len(*page.NextLink) > 0
   210  		},
   211  		Fetcher: func(ctx context.Context, page *LoadBalancerLoadBalancingRulesClientListResponse) (LoadBalancerLoadBalancingRulesClientListResponse, error) {
   212  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "LoadBalancerLoadBalancingRulesClient.NewListPager")
   213  			nextLink := ""
   214  			if page != nil {
   215  				nextLink = *page.NextLink
   216  			}
   217  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   218  				return client.listCreateRequest(ctx, resourceGroupName, loadBalancerName, options)
   219  			}, nil)
   220  			if err != nil {
   221  				return LoadBalancerLoadBalancingRulesClientListResponse{}, err
   222  			}
   223  			return client.listHandleResponse(resp)
   224  		},
   225  		Tracer: client.internal.Tracer(),
   226  	})
   227  }
   228  
   229  // listCreateRequest creates the List request.
   230  func (client *LoadBalancerLoadBalancingRulesClient) listCreateRequest(ctx context.Context, resourceGroupName string, loadBalancerName string, options *LoadBalancerLoadBalancingRulesClientListOptions) (*policy.Request, error) {
   231  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/loadBalancers/{loadBalancerName}/loadBalancingRules"
   232  	if resourceGroupName == "" {
   233  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   234  	}
   235  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   236  	if loadBalancerName == "" {
   237  		return nil, errors.New("parameter loadBalancerName cannot be empty")
   238  	}
   239  	urlPath = strings.ReplaceAll(urlPath, "{loadBalancerName}", url.PathEscape(loadBalancerName))
   240  	if client.subscriptionID == "" {
   241  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   242  	}
   243  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   244  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   245  	if err != nil {
   246  		return nil, err
   247  	}
   248  	reqQP := req.Raw().URL.Query()
   249  	reqQP.Set("api-version", "2024-05-01")
   250  	req.Raw().URL.RawQuery = reqQP.Encode()
   251  	req.Raw().Header["Accept"] = []string{"application/json"}
   252  	return req, nil
   253  }
   254  
   255  // listHandleResponse handles the List response.
   256  func (client *LoadBalancerLoadBalancingRulesClient) listHandleResponse(resp *http.Response) (LoadBalancerLoadBalancingRulesClientListResponse, error) {
   257  	result := LoadBalancerLoadBalancingRulesClientListResponse{}
   258  	if err := runtime.UnmarshalAsJSON(resp, &result.LoadBalancerLoadBalancingRuleListResult); err != nil {
   259  		return LoadBalancerLoadBalancingRulesClientListResponse{}, err
   260  	}
   261  	return result, nil
   262  }