github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/ddoscustompolicies_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  // DdosCustomPoliciesClient contains the methods for the DdosCustomPolicies group.
    24  // Don't use this type directly, use NewDdosCustomPoliciesClient() instead.
    25  type DdosCustomPoliciesClient struct {
    26  	internal       *arm.Client
    27  	subscriptionID string
    28  }
    29  
    30  // NewDdosCustomPoliciesClient creates a new instance of DdosCustomPoliciesClient 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 NewDdosCustomPoliciesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*DdosCustomPoliciesClient, error) {
    36  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	client := &DdosCustomPoliciesClient{
    41  		subscriptionID: subscriptionID,
    42  		internal:       cl,
    43  	}
    44  	return client, nil
    45  }
    46  
    47  // BeginCreateOrUpdate - Creates or updates a DDoS custom policy.
    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  //   - ddosCustomPolicyName - The name of the DDoS custom policy.
    53  //   - parameters - Parameters supplied to the create or update operation.
    54  //   - options - DdosCustomPoliciesClientBeginCreateOrUpdateOptions contains the optional parameters for the DdosCustomPoliciesClient.BeginCreateOrUpdate
    55  //     method.
    56  func (client *DdosCustomPoliciesClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters DdosCustomPolicy, options *DdosCustomPoliciesClientBeginCreateOrUpdateOptions) (*runtime.Poller[DdosCustomPoliciesClientCreateOrUpdateResponse], error) {
    57  	if options == nil || options.ResumeToken == "" {
    58  		resp, err := client.createOrUpdate(ctx, resourceGroupName, ddosCustomPolicyName, parameters, options)
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[DdosCustomPoliciesClientCreateOrUpdateResponse]{
    63  			FinalStateVia: runtime.FinalStateViaAzureAsyncOp,
    64  			Tracer:        client.internal.Tracer(),
    65  		})
    66  		return poller, err
    67  	} else {
    68  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[DdosCustomPoliciesClientCreateOrUpdateResponse]{
    69  			Tracer: client.internal.Tracer(),
    70  		})
    71  	}
    72  }
    73  
    74  // CreateOrUpdate - Creates or updates a DDoS custom policy.
    75  // If the operation fails it returns an *azcore.ResponseError type.
    76  //
    77  // Generated from API version 2024-05-01
    78  func (client *DdosCustomPoliciesClient) createOrUpdate(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters DdosCustomPolicy, options *DdosCustomPoliciesClientBeginCreateOrUpdateOptions) (*http.Response, error) {
    79  	var err error
    80  	const operationName = "DdosCustomPoliciesClient.BeginCreateOrUpdate"
    81  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    82  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    83  	defer func() { endSpan(err) }()
    84  	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, parameters, options)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	httpResp, err := client.internal.Pipeline().Do(req)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
    93  		err = runtime.NewResponseError(httpResp)
    94  		return nil, err
    95  	}
    96  	return httpResp, nil
    97  }
    98  
    99  // createOrUpdateCreateRequest creates the CreateOrUpdate request.
   100  func (client *DdosCustomPoliciesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters DdosCustomPolicy, options *DdosCustomPoliciesClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
   101  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}"
   102  	if resourceGroupName == "" {
   103  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   104  	}
   105  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   106  	if ddosCustomPolicyName == "" {
   107  		return nil, errors.New("parameter ddosCustomPolicyName cannot be empty")
   108  	}
   109  	urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName))
   110  	if client.subscriptionID == "" {
   111  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   112  	}
   113  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   114  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	reqQP := req.Raw().URL.Query()
   119  	reqQP.Set("api-version", "2024-05-01")
   120  	req.Raw().URL.RawQuery = reqQP.Encode()
   121  	req.Raw().Header["Accept"] = []string{"application/json"}
   122  	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
   123  		return nil, err
   124  	}
   125  	return req, nil
   126  }
   127  
   128  // BeginDelete - Deletes the specified DDoS custom policy.
   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  //   - ddosCustomPolicyName - The name of the DDoS custom policy.
   134  //   - options - DdosCustomPoliciesClientBeginDeleteOptions contains the optional parameters for the DdosCustomPoliciesClient.BeginDelete
   135  //     method.
   136  func (client *DdosCustomPoliciesClient) BeginDelete(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesClientBeginDeleteOptions) (*runtime.Poller[DdosCustomPoliciesClientDeleteResponse], error) {
   137  	if options == nil || options.ResumeToken == "" {
   138  		resp, err := client.deleteOperation(ctx, resourceGroupName, ddosCustomPolicyName, options)
   139  		if err != nil {
   140  			return nil, err
   141  		}
   142  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[DdosCustomPoliciesClientDeleteResponse]{
   143  			FinalStateVia: runtime.FinalStateViaLocation,
   144  			Tracer:        client.internal.Tracer(),
   145  		})
   146  		return poller, err
   147  	} else {
   148  		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[DdosCustomPoliciesClientDeleteResponse]{
   149  			Tracer: client.internal.Tracer(),
   150  		})
   151  	}
   152  }
   153  
   154  // Delete - Deletes the specified DDoS custom policy.
   155  // If the operation fails it returns an *azcore.ResponseError type.
   156  //
   157  // Generated from API version 2024-05-01
   158  func (client *DdosCustomPoliciesClient) deleteOperation(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesClientBeginDeleteOptions) (*http.Response, error) {
   159  	var err error
   160  	const operationName = "DdosCustomPoliciesClient.BeginDelete"
   161  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   162  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   163  	defer func() { endSpan(err) }()
   164  	req, err := client.deleteCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, options)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  	httpResp, err := client.internal.Pipeline().Do(req)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
   173  		err = runtime.NewResponseError(httpResp)
   174  		return nil, err
   175  	}
   176  	return httpResp, nil
   177  }
   178  
   179  // deleteCreateRequest creates the Delete request.
   180  func (client *DdosCustomPoliciesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesClientBeginDeleteOptions) (*policy.Request, error) {
   181  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}"
   182  	if resourceGroupName == "" {
   183  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   184  	}
   185  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   186  	if ddosCustomPolicyName == "" {
   187  		return nil, errors.New("parameter ddosCustomPolicyName cannot be empty")
   188  	}
   189  	urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName))
   190  	if client.subscriptionID == "" {
   191  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   192  	}
   193  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   194  	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   195  	if err != nil {
   196  		return nil, err
   197  	}
   198  	reqQP := req.Raw().URL.Query()
   199  	reqQP.Set("api-version", "2024-05-01")
   200  	req.Raw().URL.RawQuery = reqQP.Encode()
   201  	req.Raw().Header["Accept"] = []string{"application/json"}
   202  	return req, nil
   203  }
   204  
   205  // Get - Gets information about the specified DDoS custom policy.
   206  // If the operation fails it returns an *azcore.ResponseError type.
   207  //
   208  // Generated from API version 2024-05-01
   209  //   - resourceGroupName - The name of the resource group.
   210  //   - ddosCustomPolicyName - The name of the DDoS custom policy.
   211  //   - options - DdosCustomPoliciesClientGetOptions contains the optional parameters for the DdosCustomPoliciesClient.Get method.
   212  func (client *DdosCustomPoliciesClient) Get(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesClientGetOptions) (DdosCustomPoliciesClientGetResponse, error) {
   213  	var err error
   214  	const operationName = "DdosCustomPoliciesClient.Get"
   215  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   216  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   217  	defer func() { endSpan(err) }()
   218  	req, err := client.getCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, options)
   219  	if err != nil {
   220  		return DdosCustomPoliciesClientGetResponse{}, err
   221  	}
   222  	httpResp, err := client.internal.Pipeline().Do(req)
   223  	if err != nil {
   224  		return DdosCustomPoliciesClientGetResponse{}, err
   225  	}
   226  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   227  		err = runtime.NewResponseError(httpResp)
   228  		return DdosCustomPoliciesClientGetResponse{}, err
   229  	}
   230  	resp, err := client.getHandleResponse(httpResp)
   231  	return resp, err
   232  }
   233  
   234  // getCreateRequest creates the Get request.
   235  func (client *DdosCustomPoliciesClient) getCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, options *DdosCustomPoliciesClientGetOptions) (*policy.Request, error) {
   236  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}"
   237  	if resourceGroupName == "" {
   238  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   239  	}
   240  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   241  	if ddosCustomPolicyName == "" {
   242  		return nil, errors.New("parameter ddosCustomPolicyName cannot be empty")
   243  	}
   244  	urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName))
   245  	if client.subscriptionID == "" {
   246  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   247  	}
   248  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   249  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  	reqQP := req.Raw().URL.Query()
   254  	reqQP.Set("api-version", "2024-05-01")
   255  	req.Raw().URL.RawQuery = reqQP.Encode()
   256  	req.Raw().Header["Accept"] = []string{"application/json"}
   257  	return req, nil
   258  }
   259  
   260  // getHandleResponse handles the Get response.
   261  func (client *DdosCustomPoliciesClient) getHandleResponse(resp *http.Response) (DdosCustomPoliciesClientGetResponse, error) {
   262  	result := DdosCustomPoliciesClientGetResponse{}
   263  	if err := runtime.UnmarshalAsJSON(resp, &result.DdosCustomPolicy); err != nil {
   264  		return DdosCustomPoliciesClientGetResponse{}, err
   265  	}
   266  	return result, nil
   267  }
   268  
   269  // UpdateTags - Update a DDoS custom policy tags.
   270  // If the operation fails it returns an *azcore.ResponseError type.
   271  //
   272  // Generated from API version 2024-05-01
   273  //   - resourceGroupName - The name of the resource group.
   274  //   - ddosCustomPolicyName - The name of the DDoS custom policy.
   275  //   - parameters - Parameters supplied to update DDoS custom policy resource tags.
   276  //   - options - DdosCustomPoliciesClientUpdateTagsOptions contains the optional parameters for the DdosCustomPoliciesClient.UpdateTags
   277  //     method.
   278  func (client *DdosCustomPoliciesClient) UpdateTags(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters TagsObject, options *DdosCustomPoliciesClientUpdateTagsOptions) (DdosCustomPoliciesClientUpdateTagsResponse, error) {
   279  	var err error
   280  	const operationName = "DdosCustomPoliciesClient.UpdateTags"
   281  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   282  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   283  	defer func() { endSpan(err) }()
   284  	req, err := client.updateTagsCreateRequest(ctx, resourceGroupName, ddosCustomPolicyName, parameters, options)
   285  	if err != nil {
   286  		return DdosCustomPoliciesClientUpdateTagsResponse{}, err
   287  	}
   288  	httpResp, err := client.internal.Pipeline().Do(req)
   289  	if err != nil {
   290  		return DdosCustomPoliciesClientUpdateTagsResponse{}, err
   291  	}
   292  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   293  		err = runtime.NewResponseError(httpResp)
   294  		return DdosCustomPoliciesClientUpdateTagsResponse{}, err
   295  	}
   296  	resp, err := client.updateTagsHandleResponse(httpResp)
   297  	return resp, err
   298  }
   299  
   300  // updateTagsCreateRequest creates the UpdateTags request.
   301  func (client *DdosCustomPoliciesClient) updateTagsCreateRequest(ctx context.Context, resourceGroupName string, ddosCustomPolicyName string, parameters TagsObject, options *DdosCustomPoliciesClientUpdateTagsOptions) (*policy.Request, error) {
   302  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ddosCustomPolicies/{ddosCustomPolicyName}"
   303  	if resourceGroupName == "" {
   304  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   305  	}
   306  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   307  	if ddosCustomPolicyName == "" {
   308  		return nil, errors.New("parameter ddosCustomPolicyName cannot be empty")
   309  	}
   310  	urlPath = strings.ReplaceAll(urlPath, "{ddosCustomPolicyName}", url.PathEscape(ddosCustomPolicyName))
   311  	if client.subscriptionID == "" {
   312  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   313  	}
   314  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   315  	req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   316  	if err != nil {
   317  		return nil, err
   318  	}
   319  	reqQP := req.Raw().URL.Query()
   320  	reqQP.Set("api-version", "2024-05-01")
   321  	req.Raw().URL.RawQuery = reqQP.Encode()
   322  	req.Raw().Header["Accept"] = []string{"application/json"}
   323  	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
   324  		return nil, err
   325  	}
   326  	return req, nil
   327  }
   328  
   329  // updateTagsHandleResponse handles the UpdateTags response.
   330  func (client *DdosCustomPoliciesClient) updateTagsHandleResponse(resp *http.Response) (DdosCustomPoliciesClientUpdateTagsResponse, error) {
   331  	result := DdosCustomPoliciesClientUpdateTagsResponse{}
   332  	if err := runtime.UnmarshalAsJSON(resp, &result.DdosCustomPolicy); err != nil {
   333  		return DdosCustomPoliciesClientUpdateTagsResponse{}, err
   334  	}
   335  	return result, nil
   336  }