github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/securityuserrulecollections_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  // SecurityUserRuleCollectionsClient contains the methods for the SecurityUserRuleCollections group.
    25  // Don't use this type directly, use NewSecurityUserRuleCollectionsClient() instead.
    26  type SecurityUserRuleCollectionsClient struct {
    27  	internal       *arm.Client
    28  	subscriptionID string
    29  }
    30  
    31  // NewSecurityUserRuleCollectionsClient creates a new instance of SecurityUserRuleCollectionsClient 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 NewSecurityUserRuleCollectionsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*SecurityUserRuleCollectionsClient, error) {
    37  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	client := &SecurityUserRuleCollectionsClient{
    42  		subscriptionID: subscriptionID,
    43  		internal:       cl,
    44  	}
    45  	return client, nil
    46  }
    47  
    48  // CreateOrUpdate - Creates or updates a security user rule collection.
    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. The name is case insensitive.
    53  //   - networkManagerName - The name of the network manager.
    54  //   - configurationName - The name of the network manager Security Configuration.
    55  //   - ruleCollectionName - The name of the network manager security Configuration rule collection.
    56  //   - securityUserRuleCollection - The Security User Rule Collection to create or update
    57  //   - options - SecurityUserRuleCollectionsClientCreateOrUpdateOptions contains the optional parameters for the SecurityUserRuleCollectionsClient.CreateOrUpdate
    58  //     method.
    59  func (client *SecurityUserRuleCollectionsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, securityUserRuleCollection SecurityUserRuleCollection, options *SecurityUserRuleCollectionsClientCreateOrUpdateOptions) (SecurityUserRuleCollectionsClientCreateOrUpdateResponse, error) {
    60  	var err error
    61  	const operationName = "SecurityUserRuleCollectionsClient.CreateOrUpdate"
    62  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
    63  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
    64  	defer func() { endSpan(err) }()
    65  	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, securityUserRuleCollection, options)
    66  	if err != nil {
    67  		return SecurityUserRuleCollectionsClientCreateOrUpdateResponse{}, err
    68  	}
    69  	httpResp, err := client.internal.Pipeline().Do(req)
    70  	if err != nil {
    71  		return SecurityUserRuleCollectionsClientCreateOrUpdateResponse{}, err
    72  	}
    73  	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
    74  		err = runtime.NewResponseError(httpResp)
    75  		return SecurityUserRuleCollectionsClientCreateOrUpdateResponse{}, err
    76  	}
    77  	resp, err := client.createOrUpdateHandleResponse(httpResp)
    78  	return resp, err
    79  }
    80  
    81  // createOrUpdateCreateRequest creates the CreateOrUpdate request.
    82  func (client *SecurityUserRuleCollectionsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, securityUserRuleCollection SecurityUserRuleCollection, options *SecurityUserRuleCollectionsClientCreateOrUpdateOptions) (*policy.Request, error) {
    83  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/securityUserConfigurations/{configurationName}/ruleCollections/{ruleCollectionName}"
    84  	if client.subscriptionID == "" {
    85  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
    86  	}
    87  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
    88  	if resourceGroupName == "" {
    89  		return nil, errors.New("parameter resourceGroupName cannot be empty")
    90  	}
    91  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
    92  	if networkManagerName == "" {
    93  		return nil, errors.New("parameter networkManagerName cannot be empty")
    94  	}
    95  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
    96  	if configurationName == "" {
    97  		return nil, errors.New("parameter configurationName cannot be empty")
    98  	}
    99  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   100  	if ruleCollectionName == "" {
   101  		return nil, errors.New("parameter ruleCollectionName cannot be empty")
   102  	}
   103  	urlPath = strings.ReplaceAll(urlPath, "{ruleCollectionName}", url.PathEscape(ruleCollectionName))
   104  	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	reqQP := req.Raw().URL.Query()
   109  	reqQP.Set("api-version", "2024-05-01")
   110  	req.Raw().URL.RawQuery = reqQP.Encode()
   111  	req.Raw().Header["Accept"] = []string{"application/json"}
   112  	if err := runtime.MarshalAsJSON(req, securityUserRuleCollection); err != nil {
   113  		return nil, err
   114  	}
   115  	return req, nil
   116  }
   117  
   118  // createOrUpdateHandleResponse handles the CreateOrUpdate response.
   119  func (client *SecurityUserRuleCollectionsClient) createOrUpdateHandleResponse(resp *http.Response) (SecurityUserRuleCollectionsClientCreateOrUpdateResponse, error) {
   120  	result := SecurityUserRuleCollectionsClientCreateOrUpdateResponse{}
   121  	if err := runtime.UnmarshalAsJSON(resp, &result.SecurityUserRuleCollection); err != nil {
   122  		return SecurityUserRuleCollectionsClientCreateOrUpdateResponse{}, err
   123  	}
   124  	return result, nil
   125  }
   126  
   127  // BeginDelete - Deletes a Security User Rule collection.
   128  // If the operation fails it returns an *azcore.ResponseError type.
   129  //
   130  // Generated from API version 2024-05-01
   131  //   - resourceGroupName - The name of the resource group. The name is case insensitive.
   132  //   - networkManagerName - The name of the network manager.
   133  //   - configurationName - The name of the network manager Security Configuration.
   134  //   - ruleCollectionName - The name of the network manager security Configuration rule collection.
   135  //   - options - SecurityUserRuleCollectionsClientBeginDeleteOptions contains the optional parameters for the SecurityUserRuleCollectionsClient.BeginDelete
   136  //     method.
   137  func (client *SecurityUserRuleCollectionsClient) BeginDelete(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *SecurityUserRuleCollectionsClientBeginDeleteOptions) (*runtime.Poller[SecurityUserRuleCollectionsClientDeleteResponse], error) {
   138  	if options == nil || options.ResumeToken == "" {
   139  		resp, err := client.deleteOperation(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, options)
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SecurityUserRuleCollectionsClientDeleteResponse]{
   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[SecurityUserRuleCollectionsClientDeleteResponse]{
   150  			Tracer: client.internal.Tracer(),
   151  		})
   152  	}
   153  }
   154  
   155  // Delete - Deletes a Security User Rule collection.
   156  // If the operation fails it returns an *azcore.ResponseError type.
   157  //
   158  // Generated from API version 2024-05-01
   159  func (client *SecurityUserRuleCollectionsClient) deleteOperation(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *SecurityUserRuleCollectionsClientBeginDeleteOptions) (*http.Response, error) {
   160  	var err error
   161  	const operationName = "SecurityUserRuleCollectionsClient.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, configurationName, ruleCollectionName, 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.StatusOK, 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 *SecurityUserRuleCollectionsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *SecurityUserRuleCollectionsClientBeginDeleteOptions) (*policy.Request, error) {
   182  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/securityUserConfigurations/{configurationName}/ruleCollections/{ruleCollectionName}"
   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 configurationName == "" {
   196  		return nil, errors.New("parameter configurationName cannot be empty")
   197  	}
   198  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   199  	if ruleCollectionName == "" {
   200  		return nil, errors.New("parameter ruleCollectionName cannot be empty")
   201  	}
   202  	urlPath = strings.ReplaceAll(urlPath, "{ruleCollectionName}", url.PathEscape(ruleCollectionName))
   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  	if options != nil && options.Force != nil {
   210  		reqQP.Set("force", strconv.FormatBool(*options.Force))
   211  	}
   212  	req.Raw().URL.RawQuery = reqQP.Encode()
   213  	req.Raw().Header["Accept"] = []string{"application/json"}
   214  	return req, nil
   215  }
   216  
   217  // Get - Gets a network manager security user configuration rule collection.
   218  // If the operation fails it returns an *azcore.ResponseError type.
   219  //
   220  // Generated from API version 2024-05-01
   221  //   - resourceGroupName - The name of the resource group. The name is case insensitive.
   222  //   - networkManagerName - The name of the network manager.
   223  //   - configurationName - The name of the network manager Security Configuration.
   224  //   - ruleCollectionName - The name of the network manager security Configuration rule collection.
   225  //   - options - SecurityUserRuleCollectionsClientGetOptions contains the optional parameters for the SecurityUserRuleCollectionsClient.Get
   226  //     method.
   227  func (client *SecurityUserRuleCollectionsClient) Get(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *SecurityUserRuleCollectionsClientGetOptions) (SecurityUserRuleCollectionsClientGetResponse, error) {
   228  	var err error
   229  	const operationName = "SecurityUserRuleCollectionsClient.Get"
   230  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
   231  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
   232  	defer func() { endSpan(err) }()
   233  	req, err := client.getCreateRequest(ctx, resourceGroupName, networkManagerName, configurationName, ruleCollectionName, options)
   234  	if err != nil {
   235  		return SecurityUserRuleCollectionsClientGetResponse{}, err
   236  	}
   237  	httpResp, err := client.internal.Pipeline().Do(req)
   238  	if err != nil {
   239  		return SecurityUserRuleCollectionsClientGetResponse{}, err
   240  	}
   241  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
   242  		err = runtime.NewResponseError(httpResp)
   243  		return SecurityUserRuleCollectionsClientGetResponse{}, err
   244  	}
   245  	resp, err := client.getHandleResponse(httpResp)
   246  	return resp, err
   247  }
   248  
   249  // getCreateRequest creates the Get request.
   250  func (client *SecurityUserRuleCollectionsClient) getCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, ruleCollectionName string, options *SecurityUserRuleCollectionsClientGetOptions) (*policy.Request, error) {
   251  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/securityUserConfigurations/{configurationName}/ruleCollections/{ruleCollectionName}"
   252  	if client.subscriptionID == "" {
   253  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   254  	}
   255  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   256  	if resourceGroupName == "" {
   257  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   258  	}
   259  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   260  	if networkManagerName == "" {
   261  		return nil, errors.New("parameter networkManagerName cannot be empty")
   262  	}
   263  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   264  	if configurationName == "" {
   265  		return nil, errors.New("parameter configurationName cannot be empty")
   266  	}
   267  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   268  	if ruleCollectionName == "" {
   269  		return nil, errors.New("parameter ruleCollectionName cannot be empty")
   270  	}
   271  	urlPath = strings.ReplaceAll(urlPath, "{ruleCollectionName}", url.PathEscape(ruleCollectionName))
   272  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   273  	if err != nil {
   274  		return nil, err
   275  	}
   276  	reqQP := req.Raw().URL.Query()
   277  	reqQP.Set("api-version", "2024-05-01")
   278  	req.Raw().URL.RawQuery = reqQP.Encode()
   279  	req.Raw().Header["Accept"] = []string{"application/json"}
   280  	return req, nil
   281  }
   282  
   283  // getHandleResponse handles the Get response.
   284  func (client *SecurityUserRuleCollectionsClient) getHandleResponse(resp *http.Response) (SecurityUserRuleCollectionsClientGetResponse, error) {
   285  	result := SecurityUserRuleCollectionsClientGetResponse{}
   286  	if err := runtime.UnmarshalAsJSON(resp, &result.SecurityUserRuleCollection); err != nil {
   287  		return SecurityUserRuleCollectionsClientGetResponse{}, err
   288  	}
   289  	return result, nil
   290  }
   291  
   292  // NewListPager - Lists all the security user rule collections in a security configuration, in a paginated format.
   293  //
   294  // Generated from API version 2024-05-01
   295  //   - resourceGroupName - The name of the resource group. The name is case insensitive.
   296  //   - networkManagerName - The name of the network manager.
   297  //   - configurationName - The name of the network manager Security Configuration.
   298  //   - options - SecurityUserRuleCollectionsClientListOptions contains the optional parameters for the SecurityUserRuleCollectionsClient.NewListPager
   299  //     method.
   300  func (client *SecurityUserRuleCollectionsClient) NewListPager(resourceGroupName string, networkManagerName string, configurationName string, options *SecurityUserRuleCollectionsClientListOptions) *runtime.Pager[SecurityUserRuleCollectionsClientListResponse] {
   301  	return runtime.NewPager(runtime.PagingHandler[SecurityUserRuleCollectionsClientListResponse]{
   302  		More: func(page SecurityUserRuleCollectionsClientListResponse) bool {
   303  			return page.NextLink != nil && len(*page.NextLink) > 0
   304  		},
   305  		Fetcher: func(ctx context.Context, page *SecurityUserRuleCollectionsClientListResponse) (SecurityUserRuleCollectionsClientListResponse, error) {
   306  			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "SecurityUserRuleCollectionsClient.NewListPager")
   307  			nextLink := ""
   308  			if page != nil {
   309  				nextLink = *page.NextLink
   310  			}
   311  			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
   312  				return client.listCreateRequest(ctx, resourceGroupName, networkManagerName, configurationName, options)
   313  			}, nil)
   314  			if err != nil {
   315  				return SecurityUserRuleCollectionsClientListResponse{}, err
   316  			}
   317  			return client.listHandleResponse(resp)
   318  		},
   319  		Tracer: client.internal.Tracer(),
   320  	})
   321  }
   322  
   323  // listCreateRequest creates the List request.
   324  func (client *SecurityUserRuleCollectionsClient) listCreateRequest(ctx context.Context, resourceGroupName string, networkManagerName string, configurationName string, options *SecurityUserRuleCollectionsClientListOptions) (*policy.Request, error) {
   325  	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkManagers/{networkManagerName}/securityUserConfigurations/{configurationName}/ruleCollections"
   326  	if client.subscriptionID == "" {
   327  		return nil, errors.New("parameter client.subscriptionID cannot be empty")
   328  	}
   329  	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
   330  	if resourceGroupName == "" {
   331  		return nil, errors.New("parameter resourceGroupName cannot be empty")
   332  	}
   333  	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
   334  	if networkManagerName == "" {
   335  		return nil, errors.New("parameter networkManagerName cannot be empty")
   336  	}
   337  	urlPath = strings.ReplaceAll(urlPath, "{networkManagerName}", url.PathEscape(networkManagerName))
   338  	if configurationName == "" {
   339  		return nil, errors.New("parameter configurationName cannot be empty")
   340  	}
   341  	urlPath = strings.ReplaceAll(urlPath, "{configurationName}", url.PathEscape(configurationName))
   342  	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
   343  	if err != nil {
   344  		return nil, err
   345  	}
   346  	reqQP := req.Raw().URL.Query()
   347  	if options != nil && options.SkipToken != nil {
   348  		reqQP.Set("$skipToken", *options.SkipToken)
   349  	}
   350  	if options != nil && options.Top != nil {
   351  		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
   352  	}
   353  	reqQP.Set("api-version", "2024-05-01")
   354  	req.Raw().URL.RawQuery = reqQP.Encode()
   355  	req.Raw().Header["Accept"] = []string{"application/json"}
   356  	return req, nil
   357  }
   358  
   359  // listHandleResponse handles the List response.
   360  func (client *SecurityUserRuleCollectionsClient) listHandleResponse(resp *http.Response) (SecurityUserRuleCollectionsClientListResponse, error) {
   361  	result := SecurityUserRuleCollectionsClientListResponse{}
   362  	if err := runtime.UnmarshalAsJSON(resp, &result.SecurityUserRuleCollectionListResult); err != nil {
   363  		return SecurityUserRuleCollectionsClientListResponse{}, err
   364  	}
   365  	return result, nil
   366  }