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