...

Source file src/github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-04-01/policy/assignments.go

Documentation: github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-04-01/policy

     1  package policy
     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  // AssignmentsClient is the to manage and control access to your resources, you can define customized policies and
    19  // assign them at a scope.
    20  type AssignmentsClient struct {
    21  	BaseClient
    22  }
    23  
    24  // NewAssignmentsClient creates an instance of the AssignmentsClient client.
    25  func NewAssignmentsClient(subscriptionID string) AssignmentsClient {
    26  	return NewAssignmentsClientWithBaseURI(DefaultBaseURI, subscriptionID)
    27  }
    28  
    29  // NewAssignmentsClientWithBaseURI creates an instance of the AssignmentsClient client using a custom endpoint.  Use
    30  // this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
    31  func NewAssignmentsClientWithBaseURI(baseURI string, subscriptionID string) AssignmentsClient {
    32  	return AssignmentsClient{NewWithBaseURI(baseURI, subscriptionID)}
    33  }
    34  
    35  // Create policy assignments are inherited by child resources. For example, when you apply a policy to a resource group
    36  // that policy is assigned to all resources in the group.
    37  // Parameters:
    38  // scope - the scope of the policy assignment.
    39  // policyAssignmentName - the name of the policy assignment.
    40  // parameters - parameters for the policy assignment.
    41  func (client AssignmentsClient) Create(ctx context.Context, scope string, policyAssignmentName string, parameters Assignment) (result Assignment, err error) {
    42  	if tracing.IsEnabled() {
    43  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.Create")
    44  		defer func() {
    45  			sc := -1
    46  			if result.Response.Response != nil {
    47  				sc = result.Response.Response.StatusCode
    48  			}
    49  			tracing.EndSpan(ctx, sc, err)
    50  		}()
    51  	}
    52  	req, err := client.CreatePreparer(ctx, scope, policyAssignmentName, parameters)
    53  	if err != nil {
    54  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Create", nil, "Failure preparing request")
    55  		return
    56  	}
    57  
    58  	resp, err := client.CreateSender(req)
    59  	if err != nil {
    60  		result.Response = autorest.Response{Response: resp}
    61  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Create", resp, "Failure sending request")
    62  		return
    63  	}
    64  
    65  	result, err = client.CreateResponder(resp)
    66  	if err != nil {
    67  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Create", resp, "Failure responding to request")
    68  		return
    69  	}
    70  
    71  	return
    72  }
    73  
    74  // CreatePreparer prepares the Create request.
    75  func (client AssignmentsClient) CreatePreparer(ctx context.Context, scope string, policyAssignmentName string, parameters Assignment) (*http.Request, error) {
    76  	pathParameters := map[string]interface{}{
    77  		"policyAssignmentName": autorest.Encode("path", policyAssignmentName),
    78  		"scope":                scope,
    79  	}
    80  
    81  	const APIVersion = "2016-04-01"
    82  	queryParameters := map[string]interface{}{
    83  		"api-version": APIVersion,
    84  	}
    85  
    86  	preparer := autorest.CreatePreparer(
    87  		autorest.AsContentType("application/json; charset=utf-8"),
    88  		autorest.AsPut(),
    89  		autorest.WithBaseURL(client.BaseURI),
    90  		autorest.WithPathParameters("/{scope}/providers/Microsoft.Authorization/policyassignments/{policyAssignmentName}", pathParameters),
    91  		autorest.WithJSON(parameters),
    92  		autorest.WithQueryParameters(queryParameters))
    93  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
    94  }
    95  
    96  // CreateSender sends the Create request. The method will close the
    97  // http.Response Body if it receives an error.
    98  func (client AssignmentsClient) CreateSender(req *http.Request) (*http.Response, error) {
    99  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   100  }
   101  
   102  // CreateResponder handles the response to the Create request. The method always
   103  // closes the http.Response Body.
   104  func (client AssignmentsClient) CreateResponder(resp *http.Response) (result Assignment, err error) {
   105  	err = autorest.Respond(
   106  		resp,
   107  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
   108  		autorest.ByUnmarshallingJSON(&result),
   109  		autorest.ByClosing())
   110  	result.Response = autorest.Response{Response: resp}
   111  	return
   112  }
   113  
   114  // CreateByID policy assignments are inherited by child resources. For example, when you apply a policy to a resource
   115  // group that policy is assigned to all resources in the group. When providing a scope for the assignment, use
   116  // '/subscriptions/{subscription-id}/' for subscriptions,
   117  // '/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}' for resource groups, and
   118  // '/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider-namespace}/{resource-type}/{resource-name}'
   119  // for resources.
   120  // Parameters:
   121  // policyAssignmentID - the ID of the policy assignment to create. Use the format
   122  // '/{scope}/providers/Microsoft.Authorization/policyAssignments/{policy-assignment-name}'.
   123  // parameters - parameters for policy assignment.
   124  func (client AssignmentsClient) CreateByID(ctx context.Context, policyAssignmentID string, parameters Assignment) (result Assignment, err error) {
   125  	if tracing.IsEnabled() {
   126  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.CreateByID")
   127  		defer func() {
   128  			sc := -1
   129  			if result.Response.Response != nil {
   130  				sc = result.Response.Response.StatusCode
   131  			}
   132  			tracing.EndSpan(ctx, sc, err)
   133  		}()
   134  	}
   135  	req, err := client.CreateByIDPreparer(ctx, policyAssignmentID, parameters)
   136  	if err != nil {
   137  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "CreateByID", nil, "Failure preparing request")
   138  		return
   139  	}
   140  
   141  	resp, err := client.CreateByIDSender(req)
   142  	if err != nil {
   143  		result.Response = autorest.Response{Response: resp}
   144  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "CreateByID", resp, "Failure sending request")
   145  		return
   146  	}
   147  
   148  	result, err = client.CreateByIDResponder(resp)
   149  	if err != nil {
   150  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "CreateByID", resp, "Failure responding to request")
   151  		return
   152  	}
   153  
   154  	return
   155  }
   156  
   157  // CreateByIDPreparer prepares the CreateByID request.
   158  func (client AssignmentsClient) CreateByIDPreparer(ctx context.Context, policyAssignmentID string, parameters Assignment) (*http.Request, error) {
   159  	pathParameters := map[string]interface{}{
   160  		"policyAssignmentId": policyAssignmentID,
   161  	}
   162  
   163  	const APIVersion = "2016-04-01"
   164  	queryParameters := map[string]interface{}{
   165  		"api-version": APIVersion,
   166  	}
   167  
   168  	preparer := autorest.CreatePreparer(
   169  		autorest.AsContentType("application/json; charset=utf-8"),
   170  		autorest.AsPut(),
   171  		autorest.WithBaseURL(client.BaseURI),
   172  		autorest.WithPathParameters("/{policyAssignmentId}", pathParameters),
   173  		autorest.WithJSON(parameters),
   174  		autorest.WithQueryParameters(queryParameters))
   175  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   176  }
   177  
   178  // CreateByIDSender sends the CreateByID request. The method will close the
   179  // http.Response Body if it receives an error.
   180  func (client AssignmentsClient) CreateByIDSender(req *http.Request) (*http.Response, error) {
   181  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   182  }
   183  
   184  // CreateByIDResponder handles the response to the CreateByID request. The method always
   185  // closes the http.Response Body.
   186  func (client AssignmentsClient) CreateByIDResponder(resp *http.Response) (result Assignment, err error) {
   187  	err = autorest.Respond(
   188  		resp,
   189  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
   190  		autorest.ByUnmarshallingJSON(&result),
   191  		autorest.ByClosing())
   192  	result.Response = autorest.Response{Response: resp}
   193  	return
   194  }
   195  
   196  // Delete deletes a policy assignment.
   197  // Parameters:
   198  // scope - the scope of the policy assignment.
   199  // policyAssignmentName - the name of the policy assignment to delete.
   200  func (client AssignmentsClient) Delete(ctx context.Context, scope string, policyAssignmentName string) (result Assignment, err error) {
   201  	if tracing.IsEnabled() {
   202  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.Delete")
   203  		defer func() {
   204  			sc := -1
   205  			if result.Response.Response != nil {
   206  				sc = result.Response.Response.StatusCode
   207  			}
   208  			tracing.EndSpan(ctx, sc, err)
   209  		}()
   210  	}
   211  	req, err := client.DeletePreparer(ctx, scope, policyAssignmentName)
   212  	if err != nil {
   213  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Delete", nil, "Failure preparing request")
   214  		return
   215  	}
   216  
   217  	resp, err := client.DeleteSender(req)
   218  	if err != nil {
   219  		result.Response = autorest.Response{Response: resp}
   220  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Delete", resp, "Failure sending request")
   221  		return
   222  	}
   223  
   224  	result, err = client.DeleteResponder(resp)
   225  	if err != nil {
   226  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Delete", resp, "Failure responding to request")
   227  		return
   228  	}
   229  
   230  	return
   231  }
   232  
   233  // DeletePreparer prepares the Delete request.
   234  func (client AssignmentsClient) DeletePreparer(ctx context.Context, scope string, policyAssignmentName string) (*http.Request, error) {
   235  	pathParameters := map[string]interface{}{
   236  		"policyAssignmentName": autorest.Encode("path", policyAssignmentName),
   237  		"scope":                scope,
   238  	}
   239  
   240  	const APIVersion = "2016-04-01"
   241  	queryParameters := map[string]interface{}{
   242  		"api-version": APIVersion,
   243  	}
   244  
   245  	preparer := autorest.CreatePreparer(
   246  		autorest.AsDelete(),
   247  		autorest.WithBaseURL(client.BaseURI),
   248  		autorest.WithPathParameters("/{scope}/providers/Microsoft.Authorization/policyassignments/{policyAssignmentName}", pathParameters),
   249  		autorest.WithQueryParameters(queryParameters))
   250  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   251  }
   252  
   253  // DeleteSender sends the Delete request. The method will close the
   254  // http.Response Body if it receives an error.
   255  func (client AssignmentsClient) DeleteSender(req *http.Request) (*http.Response, error) {
   256  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   257  }
   258  
   259  // DeleteResponder handles the response to the Delete request. The method always
   260  // closes the http.Response Body.
   261  func (client AssignmentsClient) DeleteResponder(resp *http.Response) (result Assignment, err error) {
   262  	err = autorest.Respond(
   263  		resp,
   264  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   265  		autorest.ByUnmarshallingJSON(&result),
   266  		autorest.ByClosing())
   267  	result.Response = autorest.Response{Response: resp}
   268  	return
   269  }
   270  
   271  // DeleteByID when providing a scope for the assignment, use '/subscriptions/{subscription-id}/' for subscriptions,
   272  // '/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}' for resource groups, and
   273  // '/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider-namespace}/{resource-type}/{resource-name}'
   274  // for resources.
   275  // Parameters:
   276  // policyAssignmentID - the ID of the policy assignment to delete. Use the format
   277  // '/{scope}/providers/Microsoft.Authorization/policyAssignments/{policy-assignment-name}'.
   278  func (client AssignmentsClient) DeleteByID(ctx context.Context, policyAssignmentID string) (result Assignment, err error) {
   279  	if tracing.IsEnabled() {
   280  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.DeleteByID")
   281  		defer func() {
   282  			sc := -1
   283  			if result.Response.Response != nil {
   284  				sc = result.Response.Response.StatusCode
   285  			}
   286  			tracing.EndSpan(ctx, sc, err)
   287  		}()
   288  	}
   289  	req, err := client.DeleteByIDPreparer(ctx, policyAssignmentID)
   290  	if err != nil {
   291  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "DeleteByID", nil, "Failure preparing request")
   292  		return
   293  	}
   294  
   295  	resp, err := client.DeleteByIDSender(req)
   296  	if err != nil {
   297  		result.Response = autorest.Response{Response: resp}
   298  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "DeleteByID", resp, "Failure sending request")
   299  		return
   300  	}
   301  
   302  	result, err = client.DeleteByIDResponder(resp)
   303  	if err != nil {
   304  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "DeleteByID", resp, "Failure responding to request")
   305  		return
   306  	}
   307  
   308  	return
   309  }
   310  
   311  // DeleteByIDPreparer prepares the DeleteByID request.
   312  func (client AssignmentsClient) DeleteByIDPreparer(ctx context.Context, policyAssignmentID string) (*http.Request, error) {
   313  	pathParameters := map[string]interface{}{
   314  		"policyAssignmentId": policyAssignmentID,
   315  	}
   316  
   317  	const APIVersion = "2016-04-01"
   318  	queryParameters := map[string]interface{}{
   319  		"api-version": APIVersion,
   320  	}
   321  
   322  	preparer := autorest.CreatePreparer(
   323  		autorest.AsDelete(),
   324  		autorest.WithBaseURL(client.BaseURI),
   325  		autorest.WithPathParameters("/{policyAssignmentId}", pathParameters),
   326  		autorest.WithQueryParameters(queryParameters))
   327  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   328  }
   329  
   330  // DeleteByIDSender sends the DeleteByID request. The method will close the
   331  // http.Response Body if it receives an error.
   332  func (client AssignmentsClient) DeleteByIDSender(req *http.Request) (*http.Response, error) {
   333  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   334  }
   335  
   336  // DeleteByIDResponder handles the response to the DeleteByID request. The method always
   337  // closes the http.Response Body.
   338  func (client AssignmentsClient) DeleteByIDResponder(resp *http.Response) (result Assignment, err error) {
   339  	err = autorest.Respond(
   340  		resp,
   341  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   342  		autorest.ByUnmarshallingJSON(&result),
   343  		autorest.ByClosing())
   344  	result.Response = autorest.Response{Response: resp}
   345  	return
   346  }
   347  
   348  // Get gets a policy assignment.
   349  // Parameters:
   350  // scope - the scope of the policy assignment.
   351  // policyAssignmentName - the name of the policy assignment to get.
   352  func (client AssignmentsClient) Get(ctx context.Context, scope string, policyAssignmentName string) (result Assignment, err error) {
   353  	if tracing.IsEnabled() {
   354  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.Get")
   355  		defer func() {
   356  			sc := -1
   357  			if result.Response.Response != nil {
   358  				sc = result.Response.Response.StatusCode
   359  			}
   360  			tracing.EndSpan(ctx, sc, err)
   361  		}()
   362  	}
   363  	req, err := client.GetPreparer(ctx, scope, policyAssignmentName)
   364  	if err != nil {
   365  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Get", nil, "Failure preparing request")
   366  		return
   367  	}
   368  
   369  	resp, err := client.GetSender(req)
   370  	if err != nil {
   371  		result.Response = autorest.Response{Response: resp}
   372  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Get", resp, "Failure sending request")
   373  		return
   374  	}
   375  
   376  	result, err = client.GetResponder(resp)
   377  	if err != nil {
   378  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "Get", resp, "Failure responding to request")
   379  		return
   380  	}
   381  
   382  	return
   383  }
   384  
   385  // GetPreparer prepares the Get request.
   386  func (client AssignmentsClient) GetPreparer(ctx context.Context, scope string, policyAssignmentName string) (*http.Request, error) {
   387  	pathParameters := map[string]interface{}{
   388  		"policyAssignmentName": autorest.Encode("path", policyAssignmentName),
   389  		"scope":                scope,
   390  	}
   391  
   392  	const APIVersion = "2016-04-01"
   393  	queryParameters := map[string]interface{}{
   394  		"api-version": APIVersion,
   395  	}
   396  
   397  	preparer := autorest.CreatePreparer(
   398  		autorest.AsGet(),
   399  		autorest.WithBaseURL(client.BaseURI),
   400  		autorest.WithPathParameters("/{scope}/providers/Microsoft.Authorization/policyassignments/{policyAssignmentName}", pathParameters),
   401  		autorest.WithQueryParameters(queryParameters))
   402  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   403  }
   404  
   405  // GetSender sends the Get request. The method will close the
   406  // http.Response Body if it receives an error.
   407  func (client AssignmentsClient) GetSender(req *http.Request) (*http.Response, error) {
   408  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   409  }
   410  
   411  // GetResponder handles the response to the Get request. The method always
   412  // closes the http.Response Body.
   413  func (client AssignmentsClient) GetResponder(resp *http.Response) (result Assignment, err error) {
   414  	err = autorest.Respond(
   415  		resp,
   416  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   417  		autorest.ByUnmarshallingJSON(&result),
   418  		autorest.ByClosing())
   419  	result.Response = autorest.Response{Response: resp}
   420  	return
   421  }
   422  
   423  // GetByID when providing a scope for the assignment, use '/subscriptions/{subscription-id}/' for subscriptions,
   424  // '/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}' for resource groups, and
   425  // '/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider-namespace}/{resource-type}/{resource-name}'
   426  // for resources.
   427  // Parameters:
   428  // policyAssignmentID - the ID of the policy assignment to get. Use the format
   429  // '/{scope}/providers/Microsoft.Authorization/policyAssignments/{policy-assignment-name}'.
   430  func (client AssignmentsClient) GetByID(ctx context.Context, policyAssignmentID string) (result Assignment, err error) {
   431  	if tracing.IsEnabled() {
   432  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.GetByID")
   433  		defer func() {
   434  			sc := -1
   435  			if result.Response.Response != nil {
   436  				sc = result.Response.Response.StatusCode
   437  			}
   438  			tracing.EndSpan(ctx, sc, err)
   439  		}()
   440  	}
   441  	req, err := client.GetByIDPreparer(ctx, policyAssignmentID)
   442  	if err != nil {
   443  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "GetByID", nil, "Failure preparing request")
   444  		return
   445  	}
   446  
   447  	resp, err := client.GetByIDSender(req)
   448  	if err != nil {
   449  		result.Response = autorest.Response{Response: resp}
   450  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "GetByID", resp, "Failure sending request")
   451  		return
   452  	}
   453  
   454  	result, err = client.GetByIDResponder(resp)
   455  	if err != nil {
   456  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "GetByID", resp, "Failure responding to request")
   457  		return
   458  	}
   459  
   460  	return
   461  }
   462  
   463  // GetByIDPreparer prepares the GetByID request.
   464  func (client AssignmentsClient) GetByIDPreparer(ctx context.Context, policyAssignmentID string) (*http.Request, error) {
   465  	pathParameters := map[string]interface{}{
   466  		"policyAssignmentId": policyAssignmentID,
   467  	}
   468  
   469  	const APIVersion = "2016-04-01"
   470  	queryParameters := map[string]interface{}{
   471  		"api-version": APIVersion,
   472  	}
   473  
   474  	preparer := autorest.CreatePreparer(
   475  		autorest.AsGet(),
   476  		autorest.WithBaseURL(client.BaseURI),
   477  		autorest.WithPathParameters("/{policyAssignmentId}", pathParameters),
   478  		autorest.WithQueryParameters(queryParameters))
   479  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   480  }
   481  
   482  // GetByIDSender sends the GetByID request. The method will close the
   483  // http.Response Body if it receives an error.
   484  func (client AssignmentsClient) GetByIDSender(req *http.Request) (*http.Response, error) {
   485  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   486  }
   487  
   488  // GetByIDResponder handles the response to the GetByID request. The method always
   489  // closes the http.Response Body.
   490  func (client AssignmentsClient) GetByIDResponder(resp *http.Response) (result Assignment, err error) {
   491  	err = autorest.Respond(
   492  		resp,
   493  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   494  		autorest.ByUnmarshallingJSON(&result),
   495  		autorest.ByClosing())
   496  	result.Response = autorest.Response{Response: resp}
   497  	return
   498  }
   499  
   500  // List gets all the policy assignments for a subscription.
   501  // Parameters:
   502  // filter - the filter to apply on the operation.
   503  func (client AssignmentsClient) List(ctx context.Context, filter string) (result AssignmentListResultPage, err error) {
   504  	if tracing.IsEnabled() {
   505  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.List")
   506  		defer func() {
   507  			sc := -1
   508  			if result.alr.Response.Response != nil {
   509  				sc = result.alr.Response.Response.StatusCode
   510  			}
   511  			tracing.EndSpan(ctx, sc, err)
   512  		}()
   513  	}
   514  	result.fn = client.listNextResults
   515  	req, err := client.ListPreparer(ctx, filter)
   516  	if err != nil {
   517  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "List", nil, "Failure preparing request")
   518  		return
   519  	}
   520  
   521  	resp, err := client.ListSender(req)
   522  	if err != nil {
   523  		result.alr.Response = autorest.Response{Response: resp}
   524  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "List", resp, "Failure sending request")
   525  		return
   526  	}
   527  
   528  	result.alr, err = client.ListResponder(resp)
   529  	if err != nil {
   530  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "List", resp, "Failure responding to request")
   531  		return
   532  	}
   533  	if result.alr.hasNextLink() && result.alr.IsEmpty() {
   534  		err = result.NextWithContext(ctx)
   535  		return
   536  	}
   537  
   538  	return
   539  }
   540  
   541  // ListPreparer prepares the List request.
   542  func (client AssignmentsClient) ListPreparer(ctx context.Context, filter string) (*http.Request, error) {
   543  	pathParameters := map[string]interface{}{
   544  		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
   545  	}
   546  
   547  	const APIVersion = "2016-04-01"
   548  	queryParameters := map[string]interface{}{
   549  		"api-version": APIVersion,
   550  	}
   551  	if len(filter) > 0 {
   552  		queryParameters["$filter"] = autorest.Encode("query", filter)
   553  	}
   554  
   555  	preparer := autorest.CreatePreparer(
   556  		autorest.AsGet(),
   557  		autorest.WithBaseURL(client.BaseURI),
   558  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policyassignments", pathParameters),
   559  		autorest.WithQueryParameters(queryParameters))
   560  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   561  }
   562  
   563  // ListSender sends the List request. The method will close the
   564  // http.Response Body if it receives an error.
   565  func (client AssignmentsClient) ListSender(req *http.Request) (*http.Response, error) {
   566  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   567  }
   568  
   569  // ListResponder handles the response to the List request. The method always
   570  // closes the http.Response Body.
   571  func (client AssignmentsClient) ListResponder(resp *http.Response) (result AssignmentListResult, err error) {
   572  	err = autorest.Respond(
   573  		resp,
   574  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   575  		autorest.ByUnmarshallingJSON(&result),
   576  		autorest.ByClosing())
   577  	result.Response = autorest.Response{Response: resp}
   578  	return
   579  }
   580  
   581  // listNextResults retrieves the next set of results, if any.
   582  func (client AssignmentsClient) listNextResults(ctx context.Context, lastResults AssignmentListResult) (result AssignmentListResult, err error) {
   583  	req, err := lastResults.assignmentListResultPreparer(ctx)
   584  	if err != nil {
   585  		return result, autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listNextResults", nil, "Failure preparing next results request")
   586  	}
   587  	if req == nil {
   588  		return
   589  	}
   590  	resp, err := client.ListSender(req)
   591  	if err != nil {
   592  		result.Response = autorest.Response{Response: resp}
   593  		return result, autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listNextResults", resp, "Failure sending next results request")
   594  	}
   595  	result, err = client.ListResponder(resp)
   596  	if err != nil {
   597  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listNextResults", resp, "Failure responding to next results request")
   598  	}
   599  	return
   600  }
   601  
   602  // ListComplete enumerates all values, automatically crossing page boundaries as required.
   603  func (client AssignmentsClient) ListComplete(ctx context.Context, filter string) (result AssignmentListResultIterator, err error) {
   604  	if tracing.IsEnabled() {
   605  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.List")
   606  		defer func() {
   607  			sc := -1
   608  			if result.Response().Response.Response != nil {
   609  				sc = result.page.Response().Response.Response.StatusCode
   610  			}
   611  			tracing.EndSpan(ctx, sc, err)
   612  		}()
   613  	}
   614  	result.page, err = client.List(ctx, filter)
   615  	return
   616  }
   617  
   618  // ListForResource gets policy assignments for a resource.
   619  // Parameters:
   620  // resourceGroupName - the name of the resource group containing the resource. The name is case insensitive.
   621  // resourceProviderNamespace - the namespace of the resource provider.
   622  // parentResourcePath - the parent resource path.
   623  // resourceType - the resource type.
   624  // resourceName - the name of the resource with policy assignments.
   625  // filter - the filter to apply on the operation.
   626  func (client AssignmentsClient) ListForResource(ctx context.Context, resourceGroupName string, resourceProviderNamespace string, parentResourcePath string, resourceType string, resourceName string, filter string) (result AssignmentListResultPage, err error) {
   627  	if tracing.IsEnabled() {
   628  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.ListForResource")
   629  		defer func() {
   630  			sc := -1
   631  			if result.alr.Response.Response != nil {
   632  				sc = result.alr.Response.Response.StatusCode
   633  			}
   634  			tracing.EndSpan(ctx, sc, err)
   635  		}()
   636  	}
   637  	if err := validation.Validate([]validation.Validation{
   638  		{TargetValue: resourceGroupName,
   639  			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
   640  				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
   641  				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\p{L}\._\(\)\w]+$`, Chain: nil}}}}); err != nil {
   642  		return result, validation.NewError("policy.AssignmentsClient", "ListForResource", err.Error())
   643  	}
   644  
   645  	result.fn = client.listForResourceNextResults
   646  	req, err := client.ListForResourcePreparer(ctx, resourceGroupName, resourceProviderNamespace, parentResourcePath, resourceType, resourceName, filter)
   647  	if err != nil {
   648  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "ListForResource", nil, "Failure preparing request")
   649  		return
   650  	}
   651  
   652  	resp, err := client.ListForResourceSender(req)
   653  	if err != nil {
   654  		result.alr.Response = autorest.Response{Response: resp}
   655  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "ListForResource", resp, "Failure sending request")
   656  		return
   657  	}
   658  
   659  	result.alr, err = client.ListForResourceResponder(resp)
   660  	if err != nil {
   661  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "ListForResource", resp, "Failure responding to request")
   662  		return
   663  	}
   664  	if result.alr.hasNextLink() && result.alr.IsEmpty() {
   665  		err = result.NextWithContext(ctx)
   666  		return
   667  	}
   668  
   669  	return
   670  }
   671  
   672  // ListForResourcePreparer prepares the ListForResource request.
   673  func (client AssignmentsClient) ListForResourcePreparer(ctx context.Context, resourceGroupName string, resourceProviderNamespace string, parentResourcePath string, resourceType string, resourceName string, filter string) (*http.Request, error) {
   674  	pathParameters := map[string]interface{}{
   675  		"parentResourcePath":        parentResourcePath,
   676  		"resourceGroupName":         autorest.Encode("path", resourceGroupName),
   677  		"resourceName":              autorest.Encode("path", resourceName),
   678  		"resourceProviderNamespace": autorest.Encode("path", resourceProviderNamespace),
   679  		"resourceType":              resourceType,
   680  		"subscriptionId":            autorest.Encode("path", client.SubscriptionID),
   681  	}
   682  
   683  	const APIVersion = "2016-04-01"
   684  	queryParameters := map[string]interface{}{
   685  		"api-version": APIVersion,
   686  	}
   687  	if len(filter) > 0 {
   688  		queryParameters["$filter"] = autorest.Encode("query", filter)
   689  	}
   690  
   691  	preparer := autorest.CreatePreparer(
   692  		autorest.AsGet(),
   693  		autorest.WithBaseURL(client.BaseURI),
   694  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{parentResourcePath}/{resourceType}/{resourceName}/providers/Microsoft.Authorization/policyassignments", pathParameters),
   695  		autorest.WithQueryParameters(queryParameters))
   696  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   697  }
   698  
   699  // ListForResourceSender sends the ListForResource request. The method will close the
   700  // http.Response Body if it receives an error.
   701  func (client AssignmentsClient) ListForResourceSender(req *http.Request) (*http.Response, error) {
   702  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   703  }
   704  
   705  // ListForResourceResponder handles the response to the ListForResource request. The method always
   706  // closes the http.Response Body.
   707  func (client AssignmentsClient) ListForResourceResponder(resp *http.Response) (result AssignmentListResult, err error) {
   708  	err = autorest.Respond(
   709  		resp,
   710  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   711  		autorest.ByUnmarshallingJSON(&result),
   712  		autorest.ByClosing())
   713  	result.Response = autorest.Response{Response: resp}
   714  	return
   715  }
   716  
   717  // listForResourceNextResults retrieves the next set of results, if any.
   718  func (client AssignmentsClient) listForResourceNextResults(ctx context.Context, lastResults AssignmentListResult) (result AssignmentListResult, err error) {
   719  	req, err := lastResults.assignmentListResultPreparer(ctx)
   720  	if err != nil {
   721  		return result, autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listForResourceNextResults", nil, "Failure preparing next results request")
   722  	}
   723  	if req == nil {
   724  		return
   725  	}
   726  	resp, err := client.ListForResourceSender(req)
   727  	if err != nil {
   728  		result.Response = autorest.Response{Response: resp}
   729  		return result, autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listForResourceNextResults", resp, "Failure sending next results request")
   730  	}
   731  	result, err = client.ListForResourceResponder(resp)
   732  	if err != nil {
   733  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listForResourceNextResults", resp, "Failure responding to next results request")
   734  	}
   735  	return
   736  }
   737  
   738  // ListForResourceComplete enumerates all values, automatically crossing page boundaries as required.
   739  func (client AssignmentsClient) ListForResourceComplete(ctx context.Context, resourceGroupName string, resourceProviderNamespace string, parentResourcePath string, resourceType string, resourceName string, filter string) (result AssignmentListResultIterator, err error) {
   740  	if tracing.IsEnabled() {
   741  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.ListForResource")
   742  		defer func() {
   743  			sc := -1
   744  			if result.Response().Response.Response != nil {
   745  				sc = result.page.Response().Response.Response.StatusCode
   746  			}
   747  			tracing.EndSpan(ctx, sc, err)
   748  		}()
   749  	}
   750  	result.page, err = client.ListForResource(ctx, resourceGroupName, resourceProviderNamespace, parentResourcePath, resourceType, resourceName, filter)
   751  	return
   752  }
   753  
   754  // ListForResourceGroup gets policy assignments for the resource group.
   755  // Parameters:
   756  // resourceGroupName - the name of the resource group that contains policy assignments.
   757  // filter - the filter to apply on the operation.
   758  func (client AssignmentsClient) ListForResourceGroup(ctx context.Context, resourceGroupName string, filter string) (result AssignmentListResultPage, err error) {
   759  	if tracing.IsEnabled() {
   760  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.ListForResourceGroup")
   761  		defer func() {
   762  			sc := -1
   763  			if result.alr.Response.Response != nil {
   764  				sc = result.alr.Response.Response.StatusCode
   765  			}
   766  			tracing.EndSpan(ctx, sc, err)
   767  		}()
   768  	}
   769  	if err := validation.Validate([]validation.Validation{
   770  		{TargetValue: resourceGroupName,
   771  			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
   772  				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
   773  				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\p{L}\._\(\)\w]+$`, Chain: nil}}}}); err != nil {
   774  		return result, validation.NewError("policy.AssignmentsClient", "ListForResourceGroup", err.Error())
   775  	}
   776  
   777  	result.fn = client.listForResourceGroupNextResults
   778  	req, err := client.ListForResourceGroupPreparer(ctx, resourceGroupName, filter)
   779  	if err != nil {
   780  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "ListForResourceGroup", nil, "Failure preparing request")
   781  		return
   782  	}
   783  
   784  	resp, err := client.ListForResourceGroupSender(req)
   785  	if err != nil {
   786  		result.alr.Response = autorest.Response{Response: resp}
   787  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "ListForResourceGroup", resp, "Failure sending request")
   788  		return
   789  	}
   790  
   791  	result.alr, err = client.ListForResourceGroupResponder(resp)
   792  	if err != nil {
   793  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "ListForResourceGroup", resp, "Failure responding to request")
   794  		return
   795  	}
   796  	if result.alr.hasNextLink() && result.alr.IsEmpty() {
   797  		err = result.NextWithContext(ctx)
   798  		return
   799  	}
   800  
   801  	return
   802  }
   803  
   804  // ListForResourceGroupPreparer prepares the ListForResourceGroup request.
   805  func (client AssignmentsClient) ListForResourceGroupPreparer(ctx context.Context, resourceGroupName string, filter string) (*http.Request, error) {
   806  	pathParameters := map[string]interface{}{
   807  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   808  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   809  	}
   810  
   811  	const APIVersion = "2016-04-01"
   812  	queryParameters := map[string]interface{}{
   813  		"api-version": APIVersion,
   814  	}
   815  	if len(filter) > 0 {
   816  		queryParameters["$filter"] = filter
   817  	}
   818  
   819  	preparer := autorest.CreatePreparer(
   820  		autorest.AsGet(),
   821  		autorest.WithBaseURL(client.BaseURI),
   822  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Authorization/policyAssignments", pathParameters),
   823  		autorest.WithQueryParameters(queryParameters))
   824  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   825  }
   826  
   827  // ListForResourceGroupSender sends the ListForResourceGroup request. The method will close the
   828  // http.Response Body if it receives an error.
   829  func (client AssignmentsClient) ListForResourceGroupSender(req *http.Request) (*http.Response, error) {
   830  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   831  }
   832  
   833  // ListForResourceGroupResponder handles the response to the ListForResourceGroup request. The method always
   834  // closes the http.Response Body.
   835  func (client AssignmentsClient) ListForResourceGroupResponder(resp *http.Response) (result AssignmentListResult, err error) {
   836  	err = autorest.Respond(
   837  		resp,
   838  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   839  		autorest.ByUnmarshallingJSON(&result),
   840  		autorest.ByClosing())
   841  	result.Response = autorest.Response{Response: resp}
   842  	return
   843  }
   844  
   845  // listForResourceGroupNextResults retrieves the next set of results, if any.
   846  func (client AssignmentsClient) listForResourceGroupNextResults(ctx context.Context, lastResults AssignmentListResult) (result AssignmentListResult, err error) {
   847  	req, err := lastResults.assignmentListResultPreparer(ctx)
   848  	if err != nil {
   849  		return result, autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listForResourceGroupNextResults", nil, "Failure preparing next results request")
   850  	}
   851  	if req == nil {
   852  		return
   853  	}
   854  	resp, err := client.ListForResourceGroupSender(req)
   855  	if err != nil {
   856  		result.Response = autorest.Response{Response: resp}
   857  		return result, autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listForResourceGroupNextResults", resp, "Failure sending next results request")
   858  	}
   859  	result, err = client.ListForResourceGroupResponder(resp)
   860  	if err != nil {
   861  		err = autorest.NewErrorWithError(err, "policy.AssignmentsClient", "listForResourceGroupNextResults", resp, "Failure responding to next results request")
   862  	}
   863  	return
   864  }
   865  
   866  // ListForResourceGroupComplete enumerates all values, automatically crossing page boundaries as required.
   867  func (client AssignmentsClient) ListForResourceGroupComplete(ctx context.Context, resourceGroupName string, filter string) (result AssignmentListResultIterator, err error) {
   868  	if tracing.IsEnabled() {
   869  		ctx = tracing.StartSpan(ctx, fqdn+"/AssignmentsClient.ListForResourceGroup")
   870  		defer func() {
   871  			sc := -1
   872  			if result.Response().Response.Response != nil {
   873  				sc = result.page.Response().Response.Response.StatusCode
   874  			}
   875  			tracing.EndSpan(ctx, sc, err)
   876  		}()
   877  	}
   878  	result.page, err = client.ListForResourceGroup(ctx, resourceGroupName, filter)
   879  	return
   880  }
   881  

View as plain text