github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/staticcidrs_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  // StaticCidrsClient contains the methods for the StaticCidrs group.
    25  // Don't use this type directly, use NewStaticCidrsClient() instead.
    26  type StaticCidrsClient struct {
    27  	internal       *arm.Client
    28  	subscriptionID string
    29  }
    30  
    31  // NewStaticCidrsClient creates a new instance of StaticCidrsClient 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 NewStaticCidrsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*StaticCidrsClient, error) {
    37  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	client := &StaticCidrsClient{
    42  		subscriptionID: subscriptionID,
    43  		internal:       cl,
    44  	}
    45  	return client, nil
    46  }
    47  
    48  // Create - Creates/Updates the Static CIDR resource.
    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.
    53  //   - networkManagerName - The name of the network manager.
    54  //   - poolName - IP Address Manager Pool resource name.
    55  //   - staticCidrName - Static Cidr allocation name.
    56  //   - options - StaticCidrsClientCreateOptions contains the optional parameters for the StaticCidrsClient.Create method.
    57  func (client *StaticCidrsClient) Create(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, staticCidrName string, options *StaticCidrsClientCreateOptions) (StaticCidrsClientCreateResponse, error) {
    58  	var err error
    59  	const operationName = "StaticCidrsClient.Create"
    60  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    61  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    62  	defer func() { endSpan(err) }()
    63  	req, err := client.createCreateRequest(ctx, resourceGroupName, networkManagerName, poolName, staticCidrName, options)
    64  	if err != nil {
    65  		return StaticCidrsClientCreateResponse{}, err
    66  	}
    67  	httpResp, err := client.internal.Pipeline().Do(req)
    68  	if err != nil {
    69  		return StaticCidrsClientCreateResponse{}, err
    70  	}
    71  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
    72  		err = runtime.NewResponseError(httpResp)
    73  		return StaticCidrsClientCreateResponse{}, err
    74  	}
    75  	resp, err := client.createHandleResponse(httpResp)
    76  	return resp, err
    77  }
    78  
    79  // createCreateRequest creates the Create request.
    80  func (client *StaticCidrsClient) createCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, staticCidrName string, options *StaticCidrsClientCreateOptions) (*policy.Request, error) {
    81  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/ipamPools/{poolName}/staticCidrs/{staticCidrName}"
    82  	if client.subscriptionID == "" {
    83  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
    84  	}
    85  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
    86  	if resourceGroupName == "" {
    87  		return nil, errors.New("parameter resourceGroupName cannot be empty")
    88  	}
    89  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
    90  	if networkManagerName == "" {
    91  		return nil, errors.New("parameter networkManagerName cannot be empty")
    92  	}
    93  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
    94  	if poolName == "" {
    95  		return nil, errors.New("parameter poolName cannot be empty")
    96  	}
    97  	urlPath = strings.ReplaceAll(urlPath, "{poolName}", url.PathEscape(poolName))
    98  	if staticCidrName == "" {
    99  		return nil, errors.New("parameter staticCidrName cannot be empty")
   100  	}
   101  	urlPath = strings.ReplaceAll(urlPath, "{staticCidrName}", url.PathEscape(staticCidrName))
   102  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	reqQP := req.Raw().URL.Query()
   107  	reqQP.Set("api-version", "2024-05-01")
   108  	req.Raw().URL.RawQuery = reqQP.Encode()
   109  	req.Raw().Header["Accept"] = []string{"application/json"}
   110  	if options != nil && options.Body != nil {
   111  		if err := runtime.MarshalAsJSON(req, *options.Body); err != nil {
   112  			return nil, err
   113  		}
   114  		return req, nil
   115  	}
   116  	return req, nil
   117  }
   118  
   119  // createHandleResponse handles the Create response.
   120  func (client *StaticCidrsClient) createHandleResponse(resp *http.Response) (StaticCidrsClientCreateResponse, error) {
   121  	result := StaticCidrsClientCreateResponse{}
   122  	if err := runtime.UnmarshalAsJSON(resp, &result.StaticCidr); err != nil {
   123  		return StaticCidrsClientCreateResponse{}, err
   124  	}
   125  	return result, nil
   126  }
   127  
   128  // BeginDelete - Delete the Static CIDR resource.
   129  // If the operation fails it returns an *azcore.ResponseError type.
   130  //
   131  // Generated from API version 2024-05-01
   132  //   - resourceGroupName - The name of the resource group.
   133  //   - networkManagerName - The name of the network manager.
   134  //   - poolName - Pool resource name.
   135  //   - staticCidrName - StaticCidr resource name to delete.
   136  //   - options - StaticCidrsClientBeginDeleteOptions contains the optional parameters for the StaticCidrsClient.BeginDelete method.
   137  func (client *StaticCidrsClient) BeginDelete(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, staticCidrName string, options *StaticCidrsClientBeginDeleteOptions) (*runtime.Poller[StaticCidrsClientDeleteResponse], error) {
   138  	if options == nil || options.ResumeToken == "" {
   139  		resp, err := client.deleteOperation(ctx, resourceGroupName, networkManagerName, poolName, staticCidrName, options)
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[StaticCidrsClientDeleteResponse]{
   144  			FinalStateVia: runtime.FinalStateViaLocation,
   145  			Tracer:        client.internal.Tracer(),
   146  		})
   147  		return poller, err
   148  	} else {
   149  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[StaticCidrsClientDeleteResponse]{
   150  			Tracer: client.internal.Tracer(),
   151  		})
   152  	}
   153  }
   154  
   155  // Delete - Delete the Static CIDR resource.
   156  // If the operation fails it returns an *azcore.ResponseError type.
   157  //
   158  // Generated from API version 2024-05-01
   159  func (client *StaticCidrsClient) deleteOperation(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, staticCidrName string, options *StaticCidrsClientBeginDeleteOptions) (*http.Response, error) {
   160  	var err error
   161  	const operationName = "StaticCidrsClient.BeginDelete"
   162  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   163  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   164  	defer func() { endSpan(err) }()
   165  	req, err := client.deleteCreateRequest(ctx, resourceGroupName, networkManagerName, poolName, staticCidrName, options)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	httpResp, err := client.internal.Pipeline().Do(req)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	if !runtime.HasStatusCode(httpResp, http.StatusAccepted, http.StatusNoContent) {
   174  		err = runtime.NewResponseError(httpResp)
   175  		return nil, err
   176  	}
   177  	return httpResp, nil
   178  }
   179  
   180  // deleteCreateRequest creates the Delete request.
   181  func (client *StaticCidrsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, staticCidrName string, options *StaticCidrsClientBeginDeleteOptions) (*policy.Request, error) {
   182  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/ipamPools/{poolName}/staticCidrs/{staticCidrName}"
   183  	if client.subscriptionID == "" {
   184  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   185  	}
   186  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   187  	if resourceGroupName == "" {
   188  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   189  	}
   190  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   191  	if networkManagerName == "" {
   192  		return nil, errors.New("parameter networkManagerName cannot be empty")
   193  	}
   194  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   195  	if poolName == "" {
   196  		return nil, errors.New("parameter poolName cannot be empty")
   197  	}
   198  	urlPath = strings.ReplaceAll(urlPath, "{poolName}", url.PathEscape(poolName))
   199  	if staticCidrName == "" {
   200  		return nil, errors.New("parameter staticCidrName cannot be empty")
   201  	}
   202  	urlPath = strings.ReplaceAll(urlPath, "{staticCidrName}", url.PathEscape(staticCidrName))
   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 - Gets the specific Static CIDR resource.
   215  // If the operation fails it returns an *azcore.ResponseError type.
   216  //
   217  // Generated from API version 2024-05-01
   218  //   - resourceGroupName - The name of the resource group.
   219  //   - networkManagerName - The name of the network manager.
   220  //   - poolName - Pool resource name.
   221  //   - staticCidrName - StaticCidr resource name to retrieve.
   222  //   - options - StaticCidrsClientGetOptions contains the optional parameters for the StaticCidrsClient.Get method.
   223  func (client *StaticCidrsClient) Get(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, staticCidrName string, options *StaticCidrsClientGetOptions) (StaticCidrsClientGetResponse, error) {
   224  	var err error
   225  	const operationName = "StaticCidrsClient.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, networkManagerName, poolName, staticCidrName, options)
   230  	if err != nil {
   231  		return StaticCidrsClientGetResponse{}, err
   232  	}
   233  	httpResp, err := client.internal.Pipeline().Do(req)
   234  	if err != nil {
   235  		return StaticCidrsClientGetResponse{}, err
   236  	}
   237  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   238  		err = runtime.NewResponseError(httpResp)
   239  		return StaticCidrsClientGetResponse{}, err
   240  	}
   241  	resp, err := client.getHandleResponse(httpResp)
   242  	return resp, err
   243  }
   244  
   245  // getCreateRequest creates the Get request.
   246  func (client *StaticCidrsClient) getCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, staticCidrName string, options *StaticCidrsClientGetOptions) (*policy.Request, error) {
   247  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/ipamPools/{poolName}/staticCidrs/{staticCidrName}"
   248  	if client.subscriptionID == "" {
   249  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   250  	}
   251  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   252  	if resourceGroupName == "" {
   253  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   254  	}
   255  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   256  	if networkManagerName == "" {
   257  		return nil, errors.New("parameter networkManagerName cannot be empty")
   258  	}
   259  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   260  	if poolName == "" {
   261  		return nil, errors.New("parameter poolName cannot be empty")
   262  	}
   263  	urlPath = strings.ReplaceAll(urlPath, "{poolName}", url.PathEscape(poolName))
   264  	if staticCidrName == "" {
   265  		return nil, errors.New("parameter staticCidrName cannot be empty")
   266  	}
   267  	urlPath = strings.ReplaceAll(urlPath, "{staticCidrName}", url.PathEscape(staticCidrName))
   268  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   269  	if err != nil {
   270  		return nil, err
   271  	}
   272  	reqQP := req.Raw().URL.Query()
   273  	reqQP.Set("api-version", "2024-05-01")
   274  	req.Raw().URL.RawQuery = reqQP.Encode()
   275  	req.Raw().Header["Accept"] = []string{"application/json"}
   276  	return req, nil
   277  }
   278  
   279  // getHandleResponse handles the Get response.
   280  func (client *StaticCidrsClient) getHandleResponse(resp *http.Response) (StaticCidrsClientGetResponse, error) {
   281  	result := StaticCidrsClientGetResponse{}
   282  	if err := runtime.UnmarshalAsJSON(resp, &result.StaticCidr); err != nil {
   283  		return StaticCidrsClientGetResponse{}, err
   284  	}
   285  	return result, nil
   286  }
   287  
   288  // NewListPager - Gets list of Static CIDR resources at Network Manager level.
   289  //
   290  // Generated from API version 2024-05-01
   291  //   - resourceGroupName - The name of the resource group.
   292  //   - networkManagerName - The name of the network manager.
   293  //   - poolName - Pool resource name.
   294  //   - options - StaticCidrsClientListOptions contains the optional parameters for the StaticCidrsClient.NewListPager method.
   295  func (client *StaticCidrsClient) NewListPager(resourceGroupName string, networkManagerName string, poolName string, options *StaticCidrsClientListOptions) *runtime.Pager[StaticCidrsClientListResponse] {
   296  	return runtime.NewPager(runtime.PagingHandler[StaticCidrsClientListResponse]{
   297  		More: func(page StaticCidrsClientListResponse) bool {
   298  			return page.NextLink != nil && len(*page.NextLink) > 0
   299  		},
   300  		Fetcher: func(ctx context.Context, page *StaticCidrsClientListResponse) (StaticCidrsClientListResponse, error) {
   301  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "StaticCidrsClient.NewListPager")
   302  			nextLink := ""
   303  			if page != nil {
   304  				nextLink = *page.NextLink
   305  			}
   306  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   307  				return client.listCreateRequest(ctx, resourceGroupName, networkManagerName, poolName, options)
   308  			}, nil)
   309  			if err != nil {
   310  				return StaticCidrsClientListResponse{}, err
   311  			}
   312  			return client.listHandleResponse(resp)
   313  		},
   314  		Tracer: client.internal.Tracer(),
   315  	})
   316  }
   317  
   318  // listCreateRequest creates the List request.
   319  func (client *StaticCidrsClient) listCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, poolName string, options *StaticCidrsClientListOptions) (*policy.Request, error) {
   320  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/ipamPools/{poolName}/staticCidrs"
   321  	if client.subscriptionID == "" {
   322  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   323  	}
   324  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   325  	if resourceGroupName == "" {
   326  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   327  	}
   328  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   329  	if networkManagerName == "" {
   330  		return nil, errors.New("parameter networkManagerName cannot be empty")
   331  	}
   332  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   333  	if poolName == "" {
   334  		return nil, errors.New("parameter poolName cannot be empty")
   335  	}
   336  	urlPath = strings.ReplaceAll(urlPath, "{poolName}", url.PathEscape(poolName))
   337  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   338  	if err != nil {
   339  		return nil, err
   340  	}
   341  	reqQP := req.Raw().URL.Query()
   342  	reqQP.Set("api-version", "2024-05-01")
   343  	if options != nil && options.Skip != nil {
   344  		reqQP.Set("skip", strconv.FormatInt(int64(*options.Skip), 10))
   345  	}
   346  	if options != nil && options.SkipToken != nil {
   347  		reqQP.Set("skipToken", *options.SkipToken)
   348  	}
   349  	if options != nil && options.SortKey != nil {
   350  		reqQP.Set("sortKey", *options.SortKey)
   351  	}
   352  	if options != nil && options.SortValue != nil {
   353  		reqQP.Set("sortValue", *options.SortValue)
   354  	}
   355  	if options != nil && options.Top != nil {
   356  		reqQP.Set("top", strconv.FormatInt(int64(*options.Top), 10))
   357  	}
   358  	req.Raw().URL.RawQuery = reqQP.Encode()
   359  	req.Raw().Header["Accept"] = []string{"application/json"}
   360  	return req, nil
   361  }
   362  
   363  // listHandleResponse handles the List response.
   364  func (client *StaticCidrsClient) listHandleResponse(resp *http.Response) (StaticCidrsClientListResponse, error) {
   365  	result := StaticCidrsClientListResponse{}
   366  	if err := runtime.UnmarshalAsJSON(resp, &result.StaticCidrList); err != nil {
   367  		return StaticCidrsClientListResponse{}, err
   368  	}
   369  	return result, nil
   370  }