github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/vipswap_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  // VipSwapClient contains the methods for the VipSwap group.
    24  // Don't use this type directly, use NewVipSwapClient() instead.
    25  type VipSwapClient struct {
    26  	internal       *arm.Client
    27  	subscriptionID string
    28  }
    29  
    30  // NewVipSwapClient creates a new instance of VipSwapClient 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 NewVipSwapClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*VipSwapClient, error) {
    36  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	client := &VipSwapClient{
    41  		subscriptionID: subscriptionID,
    42  		internal:       cl,
    43  	}
    44  	return client, nil
    45  }
    46  
    47  // BeginCreate - Performs vip swap operation on swappable cloud services.
    48  // If the operation fails it returns an *azcore.ResponseError type.
    49  //
    50  // Generated from API version 2024-05-01
    51  //   - groupName - The name of the resource group.
    52  //   - resourceName - The name of the cloud service.
    53  //   - parameters - SwapResource object where slot type should be the target slot after vip swap for the specified cloud service.
    54  //   - options - VipSwapClientBeginCreateOptions contains the optional parameters for the VipSwapClient.BeginCreate method.
    55  func (client *VipSwapClient) BeginCreate(ctx context.Context, groupName string, resourceName string, parameters SwapResource, options *VipSwapClientBeginCreateOptions) (*runtime.Poller[VipSwapClientCreateResponse], error) {
    56  	if options == nil || options.ResumeToken == "" {
    57  		resp, err := client.create(ctx, groupName, resourceName, parameters, options)
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[VipSwapClientCreateResponse]{
    62  			Tracer: client.internal.Tracer(),
    63  		})
    64  		return poller, err
    65  	} else {
    66  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[VipSwapClientCreateResponse]{
    67  			Tracer: client.internal.Tracer(),
    68  		})
    69  	}
    70  }
    71  
    72  // Create - Performs vip swap operation on swappable cloud services.
    73  // If the operation fails it returns an *azcore.ResponseError type.
    74  //
    75  // Generated from API version 2024-05-01
    76  func (client *VipSwapClient) create(ctx context.Context, groupName string, resourceName string, parameters SwapResource, options *VipSwapClientBeginCreateOptions) (*http.Response, error) {
    77  	var err error
    78  	const operationName = "VipSwapClient.BeginCreate"
    79  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    80  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    81  	defer func() { endSpan(err) }()
    82  	req, err := client.createCreateRequest(ctx, groupName, resourceName, parameters, options)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	httpResp, err := client.internal.Pipeline().Do(req)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
    91  		err = runtime.NewResponseError(httpResp)
    92  		return nil, err
    93  	}
    94  	return httpResp, nil
    95  }
    96  
    97  // createCreateRequest creates the Create request.
    98  func (client *VipSwapClient) createCreateRequest(ctx context.Context, groupName string, resourceName string, parameters SwapResource, options *VipSwapClientBeginCreateOptions) (*policy.Request, error) {
    99  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Compute/cloudServices/{resourceName}/providers/Microsoft.Network/cloudServiceSlots/{singletonResource}"
   100  	if groupName == "" {
   101  		return nil, errors.New("parameter groupName cannot be empty")
   102  	}
   103  	urlPath = strings.ReplaceAll(urlPath, "{groupName}", url.PathEscape(groupName))
   104  	if resourceName == "" {
   105  		return nil, errors.New("parameter resourceName cannot be empty")
   106  	}
   107  	urlPath = strings.ReplaceAll(urlPath, "{resourceName}", url.PathEscape(resourceName))
   108  	urlPath = strings.ReplaceAll(urlPath, "{singletonResource}", url.PathEscape("swap"))
   109  	if client.subscriptionID == "" {
   110  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   111  	}
   112  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   113  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  	reqQP := req.Raw().URL.Query()
   118  	reqQP.Set("api-version", "2024-05-01")
   119  	req.Raw().URL.RawQuery = reqQP.Encode()
   120  	req.Raw().Header["Accept"] = []string{"application/json"}
   121  	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
   122  		return nil, err
   123  	}
   124  	return req, nil
   125  }
   126  
   127  // Get - Gets the SwapResource which identifies the slot type for the specified cloud service. The slot type on a cloud service
   128  // can either be Staging or Production
   129  // If the operation fails it returns an *azcore.ResponseError type.
   130  //
   131  // Generated from API version 2024-05-01
   132  //   - groupName - The name of the resource group.
   133  //   - resourceName - The name of the cloud service.
   134  //   - options - VipSwapClientGetOptions contains the optional parameters for the VipSwapClient.Get method.
   135  func (client *VipSwapClient) Get(ctx context.Context, groupName string, resourceName string, options *VipSwapClientGetOptions) (VipSwapClientGetResponse, error) {
   136  	var err error
   137  	const operationName = "VipSwapClient.Get"
   138  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   139  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   140  	defer func() { endSpan(err) }()
   141  	req, err := client.getCreateRequest(ctx, groupName, resourceName, options)
   142  	if err != nil {
   143  		return VipSwapClientGetResponse{}, err
   144  	}
   145  	httpResp, err := client.internal.Pipeline().Do(req)
   146  	if err != nil {
   147  		return VipSwapClientGetResponse{}, err
   148  	}
   149  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   150  		err = runtime.NewResponseError(httpResp)
   151  		return VipSwapClientGetResponse{}, err
   152  	}
   153  	resp, err := client.getHandleResponse(httpResp)
   154  	return resp, err
   155  }
   156  
   157  // getCreateRequest creates the Get request.
   158  func (client *VipSwapClient) getCreateRequest(ctx context.Context, groupName string, resourceName string, options *VipSwapClientGetOptions) (*policy.Request, error) {
   159  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Compute/cloudServices/{resourceName}/providers/Microsoft.Network/cloudServiceSlots/{singletonResource}"
   160  	if groupName == "" {
   161  		return nil, errors.New("parameter groupName cannot be empty")
   162  	}
   163  	urlPath = strings.ReplaceAll(urlPath, "{groupName}", url.PathEscape(groupName))
   164  	if resourceName == "" {
   165  		return nil, errors.New("parameter resourceName cannot be empty")
   166  	}
   167  	urlPath = strings.ReplaceAll(urlPath, "{resourceName}", url.PathEscape(resourceName))
   168  	urlPath = strings.ReplaceAll(urlPath, "{singletonResource}", url.PathEscape("swap"))
   169  	if client.subscriptionID == "" {
   170  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   171  	}
   172  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   173  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	reqQP := req.Raw().URL.Query()
   178  	reqQP.Set("api-version", "2024-05-01")
   179  	req.Raw().URL.RawQuery = reqQP.Encode()
   180  	req.Raw().Header["Accept"] = []string{"application/json"}
   181  	return req, nil
   182  }
   183  
   184  // getHandleResponse handles the Get response.
   185  func (client *VipSwapClient) getHandleResponse(resp *http.Response) (VipSwapClientGetResponse, error) {
   186  	result := VipSwapClientGetResponse{}
   187  	if err := runtime.UnmarshalAsJSON(resp, &result.SwapResource); err != nil {
   188  		return VipSwapClientGetResponse{}, err
   189  	}
   190  	return result, nil
   191  }
   192  
   193  // List - Gets the list of SwapResource which identifies the slot type for the specified cloud service. The slot type on a
   194  // cloud service can either be Staging or Production
   195  // If the operation fails it returns an *azcore.ResponseError type.
   196  //
   197  // Generated from API version 2024-05-01
   198  //   - groupName - The name of the resource group.
   199  //   - resourceName - The name of the cloud service.
   200  //   - options - VipSwapClientListOptions contains the optional parameters for the VipSwapClient.List method.
   201  func (client *VipSwapClient) List(ctx context.Context, groupName string, resourceName string, options *VipSwapClientListOptions) (VipSwapClientListResponse, error) {
   202  	var err error
   203  	const operationName = "VipSwapClient.List"
   204  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   205  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   206  	defer func() { endSpan(err) }()
   207  	req, err := client.listCreateRequest(ctx, groupName, resourceName, options)
   208  	if err != nil {
   209  		return VipSwapClientListResponse{}, err
   210  	}
   211  	httpResp, err := client.internal.Pipeline().Do(req)
   212  	if err != nil {
   213  		return VipSwapClientListResponse{}, err
   214  	}
   215  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   216  		err = runtime.NewResponseError(httpResp)
   217  		return VipSwapClientListResponse{}, err
   218  	}
   219  	resp, err := client.listHandleResponse(httpResp)
   220  	return resp, err
   221  }
   222  
   223  // listCreateRequest creates the List request.
   224  func (client *VipSwapClient) listCreateRequest(ctx context.Context, groupName string, resourceName string, options *VipSwapClientListOptions) (*policy.Request, error) {
   225  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Compute/cloudServices/{resourceName}/providers/Microsoft.Network/cloudServiceSlots"
   226  	if groupName == "" {
   227  		return nil, errors.New("parameter groupName cannot be empty")
   228  	}
   229  	urlPath = strings.ReplaceAll(urlPath, "{groupName}", url.PathEscape(groupName))
   230  	if resourceName == "" {
   231  		return nil, errors.New("parameter resourceName cannot be empty")
   232  	}
   233  	urlPath = strings.ReplaceAll(urlPath, "{resourceName}", url.PathEscape(resourceName))
   234  	if client.subscriptionID == "" {
   235  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   236  	}
   237  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   238  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   239  	if err != nil {
   240  		return nil, err
   241  	}
   242  	reqQP := req.Raw().URL.Query()
   243  	reqQP.Set("api-version", "2024-05-01")
   244  	req.Raw().URL.RawQuery = reqQP.Encode()
   245  	req.Raw().Header["Accept"] = []string{"application/json"}
   246  	return req, nil
   247  }
   248  
   249  // listHandleResponse handles the List response.
   250  func (client *VipSwapClient) listHandleResponse(resp *http.Response) (VipSwapClientListResponse, error) {
   251  	result := VipSwapClientListResponse{}
   252  	if err := runtime.UnmarshalAsJSON(resp, &result.SwapResourceListResult); err != nil {
   253  		return VipSwapClientListResponse{}, err
   254  	}
   255  	return result, nil
   256  }