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