...

Source file src/github.com/Azure/azure-sdk-for-go/services/frontdoor/mgmt/2020-01-01/frontdoor/policies.go

Documentation: github.com/Azure/azure-sdk-for-go/services/frontdoor/mgmt/2020-01-01/frontdoor

     1  package frontdoor
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  //
     6  // Code generated by Microsoft (R) AutoRest Code Generator.
     7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
     8  
     9  import (
    10  	"context"
    11  	"github.com/Azure/go-autorest/autorest"
    12  	"github.com/Azure/go-autorest/autorest/azure"
    13  	"github.com/Azure/go-autorest/autorest/validation"
    14  	"github.com/Azure/go-autorest/tracing"
    15  	"net/http"
    16  )
    17  
    18  // PoliciesClient is the frontDoor Client
    19  type PoliciesClient struct {
    20  	BaseClient
    21  }
    22  
    23  // NewPoliciesClient creates an instance of the PoliciesClient client.
    24  func NewPoliciesClient(subscriptionID string) PoliciesClient {
    25  	return NewPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID)
    26  }
    27  
    28  // NewPoliciesClientWithBaseURI creates an instance of the PoliciesClient client using a custom endpoint.  Use this
    29  // when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
    30  func NewPoliciesClientWithBaseURI(baseURI string, subscriptionID string) PoliciesClient {
    31  	return PoliciesClient{NewWithBaseURI(baseURI, subscriptionID)}
    32  }
    33  
    34  // CreateOrUpdate create or update policy with specified rule set name within a resource group.
    35  // Parameters:
    36  // resourceGroupName - name of the Resource group within the Azure subscription.
    37  // policyName - the name of the Web Application Firewall Policy.
    38  // parameters - policy to be created.
    39  func (client PoliciesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, policyName string, parameters WebApplicationFirewallPolicy) (result PoliciesCreateOrUpdateFuture, err error) {
    40  	if tracing.IsEnabled() {
    41  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.CreateOrUpdate")
    42  		defer func() {
    43  			sc := -1
    44  			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
    45  				sc = result.FutureAPI.Response().StatusCode
    46  			}
    47  			tracing.EndSpan(ctx, sc, err)
    48  		}()
    49  	}
    50  	if err := validation.Validate([]validation.Validation{
    51  		{TargetValue: resourceGroupName,
    52  			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
    53  				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
    54  				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}},
    55  		{TargetValue: policyName,
    56  			Constraints: []validation.Constraint{{Target: "policyName", Name: validation.MaxLength, Rule: 128, Chain: nil}}},
    57  		{TargetValue: parameters,
    58  			Constraints: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties", Name: validation.Null, Rule: false,
    59  				Chain: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties.PolicySettings", Name: validation.Null, Rule: false,
    60  					Chain: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties.PolicySettings.CustomBlockResponseBody", Name: validation.Null, Rule: false,
    61  						Chain: []validation.Constraint{{Target: "parameters.WebApplicationFirewallPolicyProperties.PolicySettings.CustomBlockResponseBody", Name: validation.Pattern, Rule: `^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$`, Chain: nil}}},
    62  					}},
    63  				}}}}}); err != nil {
    64  		return result, validation.NewError("frontdoor.PoliciesClient", "CreateOrUpdate", err.Error())
    65  	}
    66  
    67  	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, policyName, parameters)
    68  	if err != nil {
    69  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "CreateOrUpdate", nil, "Failure preparing request")
    70  		return
    71  	}
    72  
    73  	result, err = client.CreateOrUpdateSender(req)
    74  	if err != nil {
    75  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "CreateOrUpdate", result.Response(), "Failure sending request")
    76  		return
    77  	}
    78  
    79  	return
    80  }
    81  
    82  // CreateOrUpdatePreparer prepares the CreateOrUpdate request.
    83  func (client PoliciesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, policyName string, parameters WebApplicationFirewallPolicy) (*http.Request, error) {
    84  	pathParameters := map[string]interface{}{
    85  		"policyName":        autorest.Encode("path", policyName),
    86  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
    87  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
    88  	}
    89  
    90  	const APIVersion = "2019-10-01"
    91  	queryParameters := map[string]interface{}{
    92  		"api-version": APIVersion,
    93  	}
    94  
    95  	preparer := autorest.CreatePreparer(
    96  		autorest.AsContentType("application/json; charset=utf-8"),
    97  		autorest.AsPut(),
    98  		autorest.WithBaseURL(client.BaseURI),
    99  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies/{policyName}", pathParameters),
   100  		autorest.WithJSON(parameters),
   101  		autorest.WithQueryParameters(queryParameters))
   102  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   103  }
   104  
   105  // CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
   106  // http.Response Body if it receives an error.
   107  func (client PoliciesClient) CreateOrUpdateSender(req *http.Request) (future PoliciesCreateOrUpdateFuture, err error) {
   108  	var resp *http.Response
   109  	future.FutureAPI = &azure.Future{}
   110  	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
   111  	if err != nil {
   112  		return
   113  	}
   114  	var azf azure.Future
   115  	azf, err = azure.NewFutureFromResponse(resp)
   116  	future.FutureAPI = &azf
   117  	future.Result = future.result
   118  	return
   119  }
   120  
   121  // CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
   122  // closes the http.Response Body.
   123  func (client PoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result WebApplicationFirewallPolicy, err error) {
   124  	err = autorest.Respond(
   125  		resp,
   126  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
   127  		autorest.ByUnmarshallingJSON(&result),
   128  		autorest.ByClosing())
   129  	result.Response = autorest.Response{Response: resp}
   130  	return
   131  }
   132  
   133  // Delete deletes Policy
   134  // Parameters:
   135  // resourceGroupName - name of the Resource group within the Azure subscription.
   136  // policyName - the name of the Web Application Firewall Policy.
   137  func (client PoliciesClient) Delete(ctx context.Context, resourceGroupName string, policyName string) (result PoliciesDeleteFuture, err error) {
   138  	if tracing.IsEnabled() {
   139  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Delete")
   140  		defer func() {
   141  			sc := -1
   142  			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
   143  				sc = result.FutureAPI.Response().StatusCode
   144  			}
   145  			tracing.EndSpan(ctx, sc, err)
   146  		}()
   147  	}
   148  	if err := validation.Validate([]validation.Validation{
   149  		{TargetValue: resourceGroupName,
   150  			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
   151  				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
   152  				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}},
   153  		{TargetValue: policyName,
   154  			Constraints: []validation.Constraint{{Target: "policyName", Name: validation.MaxLength, Rule: 128, Chain: nil}}}}); err != nil {
   155  		return result, validation.NewError("frontdoor.PoliciesClient", "Delete", err.Error())
   156  	}
   157  
   158  	req, err := client.DeletePreparer(ctx, resourceGroupName, policyName)
   159  	if err != nil {
   160  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Delete", nil, "Failure preparing request")
   161  		return
   162  	}
   163  
   164  	result, err = client.DeleteSender(req)
   165  	if err != nil {
   166  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Delete", result.Response(), "Failure sending request")
   167  		return
   168  	}
   169  
   170  	return
   171  }
   172  
   173  // DeletePreparer prepares the Delete request.
   174  func (client PoliciesClient) DeletePreparer(ctx context.Context, resourceGroupName string, policyName string) (*http.Request, error) {
   175  	pathParameters := map[string]interface{}{
   176  		"policyName":        autorest.Encode("path", policyName),
   177  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   178  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   179  	}
   180  
   181  	const APIVersion = "2019-10-01"
   182  	queryParameters := map[string]interface{}{
   183  		"api-version": APIVersion,
   184  	}
   185  
   186  	preparer := autorest.CreatePreparer(
   187  		autorest.AsDelete(),
   188  		autorest.WithBaseURL(client.BaseURI),
   189  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies/{policyName}", pathParameters),
   190  		autorest.WithQueryParameters(queryParameters))
   191  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   192  }
   193  
   194  // DeleteSender sends the Delete request. The method will close the
   195  // http.Response Body if it receives an error.
   196  func (client PoliciesClient) DeleteSender(req *http.Request) (future PoliciesDeleteFuture, err error) {
   197  	var resp *http.Response
   198  	future.FutureAPI = &azure.Future{}
   199  	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
   200  	if err != nil {
   201  		return
   202  	}
   203  	var azf azure.Future
   204  	azf, err = azure.NewFutureFromResponse(resp)
   205  	future.FutureAPI = &azf
   206  	future.Result = future.result
   207  	return
   208  }
   209  
   210  // DeleteResponder handles the response to the Delete request. The method always
   211  // closes the http.Response Body.
   212  func (client PoliciesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
   213  	err = autorest.Respond(
   214  		resp,
   215  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
   216  		autorest.ByClosing())
   217  	result.Response = resp
   218  	return
   219  }
   220  
   221  // Get retrieve protection policy with specified name within a resource group.
   222  // Parameters:
   223  // resourceGroupName - name of the Resource group within the Azure subscription.
   224  // policyName - the name of the Web Application Firewall Policy.
   225  func (client PoliciesClient) Get(ctx context.Context, resourceGroupName string, policyName string) (result WebApplicationFirewallPolicy, err error) {
   226  	if tracing.IsEnabled() {
   227  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Get")
   228  		defer func() {
   229  			sc := -1
   230  			if result.Response.Response != nil {
   231  				sc = result.Response.Response.StatusCode
   232  			}
   233  			tracing.EndSpan(ctx, sc, err)
   234  		}()
   235  	}
   236  	if err := validation.Validate([]validation.Validation{
   237  		{TargetValue: resourceGroupName,
   238  			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
   239  				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
   240  				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}},
   241  		{TargetValue: policyName,
   242  			Constraints: []validation.Constraint{{Target: "policyName", Name: validation.MaxLength, Rule: 128, Chain: nil}}}}); err != nil {
   243  		return result, validation.NewError("frontdoor.PoliciesClient", "Get", err.Error())
   244  	}
   245  
   246  	req, err := client.GetPreparer(ctx, resourceGroupName, policyName)
   247  	if err != nil {
   248  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Get", nil, "Failure preparing request")
   249  		return
   250  	}
   251  
   252  	resp, err := client.GetSender(req)
   253  	if err != nil {
   254  		result.Response = autorest.Response{Response: resp}
   255  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Get", resp, "Failure sending request")
   256  		return
   257  	}
   258  
   259  	result, err = client.GetResponder(resp)
   260  	if err != nil {
   261  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "Get", resp, "Failure responding to request")
   262  		return
   263  	}
   264  
   265  	return
   266  }
   267  
   268  // GetPreparer prepares the Get request.
   269  func (client PoliciesClient) GetPreparer(ctx context.Context, resourceGroupName string, policyName string) (*http.Request, error) {
   270  	pathParameters := map[string]interface{}{
   271  		"policyName":        autorest.Encode("path", policyName),
   272  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   273  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   274  	}
   275  
   276  	const APIVersion = "2019-10-01"
   277  	queryParameters := map[string]interface{}{
   278  		"api-version": APIVersion,
   279  	}
   280  
   281  	preparer := autorest.CreatePreparer(
   282  		autorest.AsGet(),
   283  		autorest.WithBaseURL(client.BaseURI),
   284  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies/{policyName}", pathParameters),
   285  		autorest.WithQueryParameters(queryParameters))
   286  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   287  }
   288  
   289  // GetSender sends the Get request. The method will close the
   290  // http.Response Body if it receives an error.
   291  func (client PoliciesClient) GetSender(req *http.Request) (*http.Response, error) {
   292  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   293  }
   294  
   295  // GetResponder handles the response to the Get request. The method always
   296  // closes the http.Response Body.
   297  func (client PoliciesClient) GetResponder(resp *http.Response) (result WebApplicationFirewallPolicy, err error) {
   298  	err = autorest.Respond(
   299  		resp,
   300  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   301  		autorest.ByUnmarshallingJSON(&result),
   302  		autorest.ByClosing())
   303  	result.Response = autorest.Response{Response: resp}
   304  	return
   305  }
   306  
   307  // List lists all of the protection policies within a resource group.
   308  // Parameters:
   309  // resourceGroupName - name of the Resource group within the Azure subscription.
   310  func (client PoliciesClient) List(ctx context.Context, resourceGroupName string) (result WebApplicationFirewallPolicyListPage, err error) {
   311  	if tracing.IsEnabled() {
   312  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List")
   313  		defer func() {
   314  			sc := -1
   315  			if result.wafpl.Response.Response != nil {
   316  				sc = result.wafpl.Response.Response.StatusCode
   317  			}
   318  			tracing.EndSpan(ctx, sc, err)
   319  		}()
   320  	}
   321  	if err := validation.Validate([]validation.Validation{
   322  		{TargetValue: resourceGroupName,
   323  			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 80, Chain: nil},
   324  				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
   325  				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[a-zA-Z0-9_\-\(\)\.]*[^\.]$`, Chain: nil}}}}); err != nil {
   326  		return result, validation.NewError("frontdoor.PoliciesClient", "List", err.Error())
   327  	}
   328  
   329  	result.fn = client.listNextResults
   330  	req, err := client.ListPreparer(ctx, resourceGroupName)
   331  	if err != nil {
   332  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "List", nil, "Failure preparing request")
   333  		return
   334  	}
   335  
   336  	resp, err := client.ListSender(req)
   337  	if err != nil {
   338  		result.wafpl.Response = autorest.Response{Response: resp}
   339  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "List", resp, "Failure sending request")
   340  		return
   341  	}
   342  
   343  	result.wafpl, err = client.ListResponder(resp)
   344  	if err != nil {
   345  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "List", resp, "Failure responding to request")
   346  		return
   347  	}
   348  	if result.wafpl.hasNextLink() && result.wafpl.IsEmpty() {
   349  		err = result.NextWithContext(ctx)
   350  		return
   351  	}
   352  
   353  	return
   354  }
   355  
   356  // ListPreparer prepares the List request.
   357  func (client PoliciesClient) ListPreparer(ctx context.Context, resourceGroupName string) (*http.Request, error) {
   358  	pathParameters := map[string]interface{}{
   359  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   360  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   361  	}
   362  
   363  	const APIVersion = "2019-10-01"
   364  	queryParameters := map[string]interface{}{
   365  		"api-version": APIVersion,
   366  	}
   367  
   368  	preparer := autorest.CreatePreparer(
   369  		autorest.AsGet(),
   370  		autorest.WithBaseURL(client.BaseURI),
   371  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/FrontDoorWebApplicationFirewallPolicies", pathParameters),
   372  		autorest.WithQueryParameters(queryParameters))
   373  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   374  }
   375  
   376  // ListSender sends the List request. The method will close the
   377  // http.Response Body if it receives an error.
   378  func (client PoliciesClient) ListSender(req *http.Request) (*http.Response, error) {
   379  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   380  }
   381  
   382  // ListResponder handles the response to the List request. The method always
   383  // closes the http.Response Body.
   384  func (client PoliciesClient) ListResponder(resp *http.Response) (result WebApplicationFirewallPolicyList, err error) {
   385  	err = autorest.Respond(
   386  		resp,
   387  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   388  		autorest.ByUnmarshallingJSON(&result),
   389  		autorest.ByClosing())
   390  	result.Response = autorest.Response{Response: resp}
   391  	return
   392  }
   393  
   394  // listNextResults retrieves the next set of results, if any.
   395  func (client PoliciesClient) listNextResults(ctx context.Context, lastResults WebApplicationFirewallPolicyList) (result WebApplicationFirewallPolicyList, err error) {
   396  	req, err := lastResults.webApplicationFirewallPolicyListPreparer(ctx)
   397  	if err != nil {
   398  		return result, autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "listNextResults", nil, "Failure preparing next results request")
   399  	}
   400  	if req == nil {
   401  		return
   402  	}
   403  	resp, err := client.ListSender(req)
   404  	if err != nil {
   405  		result.Response = autorest.Response{Response: resp}
   406  		return result, autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "listNextResults", resp, "Failure sending next results request")
   407  	}
   408  	result, err = client.ListResponder(resp)
   409  	if err != nil {
   410  		err = autorest.NewErrorWithError(err, "frontdoor.PoliciesClient", "listNextResults", resp, "Failure responding to next results request")
   411  	}
   412  	return
   413  }
   414  
   415  // ListComplete enumerates all values, automatically crossing page boundaries as required.
   416  func (client PoliciesClient) ListComplete(ctx context.Context, resourceGroupName string) (result WebApplicationFirewallPolicyListIterator, err error) {
   417  	if tracing.IsEnabled() {
   418  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List")
   419  		defer func() {
   420  			sc := -1
   421  			if result.Response().Response.Response != nil {
   422  				sc = result.page.Response().Response.Response.StatusCode
   423  			}
   424  			tracing.EndSpan(ctx, sc, err)
   425  		}()
   426  	}
   427  	result.page, err = client.List(ctx, resourceGroupName)
   428  	return
   429  }
   430  

View as plain text