...

Source file src/github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2015-10-01-preview/policy/definitions.go

Documentation: github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2015-10-01-preview/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/tracing"
    14  	"net/http"
    15  )
    16  
    17  // DefinitionsClient is the to manage and control access to your resources, you can define customized policies and
    18  // assign them at a scope.
    19  type DefinitionsClient struct {
    20  	BaseClient
    21  }
    22  
    23  // NewDefinitionsClient creates an instance of the DefinitionsClient client.
    24  func NewDefinitionsClient(subscriptionID string) DefinitionsClient {
    25  	return NewDefinitionsClientWithBaseURI(DefaultBaseURI, subscriptionID)
    26  }
    27  
    28  // NewDefinitionsClientWithBaseURI creates an instance of the DefinitionsClient client using a custom endpoint.  Use
    29  // this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
    30  func NewDefinitionsClientWithBaseURI(baseURI string, subscriptionID string) DefinitionsClient {
    31  	return DefinitionsClient{NewWithBaseURI(baseURI, subscriptionID)}
    32  }
    33  
    34  // CreateOrUpdate creates or updates a policy definition.
    35  // Parameters:
    36  // policyDefinitionName - the name of the policy definition to create.
    37  // parameters - the policy definition properties.
    38  func (client DefinitionsClient) CreateOrUpdate(ctx context.Context, policyDefinitionName string, parameters Definition) (result Definition, err error) {
    39  	if tracing.IsEnabled() {
    40  		ctx = tracing.StartSpan(ctx, fqdn+"/DefinitionsClient.CreateOrUpdate")
    41  		defer func() {
    42  			sc := -1
    43  			if result.Response.Response != nil {
    44  				sc = result.Response.Response.StatusCode
    45  			}
    46  			tracing.EndSpan(ctx, sc, err)
    47  		}()
    48  	}
    49  	req, err := client.CreateOrUpdatePreparer(ctx, policyDefinitionName, parameters)
    50  	if err != nil {
    51  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "CreateOrUpdate", nil, "Failure preparing request")
    52  		return
    53  	}
    54  
    55  	resp, err := client.CreateOrUpdateSender(req)
    56  	if err != nil {
    57  		result.Response = autorest.Response{Response: resp}
    58  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "CreateOrUpdate", resp, "Failure sending request")
    59  		return
    60  	}
    61  
    62  	result, err = client.CreateOrUpdateResponder(resp)
    63  	if err != nil {
    64  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "CreateOrUpdate", resp, "Failure responding to request")
    65  		return
    66  	}
    67  
    68  	return
    69  }
    70  
    71  // CreateOrUpdatePreparer prepares the CreateOrUpdate request.
    72  func (client DefinitionsClient) CreateOrUpdatePreparer(ctx context.Context, policyDefinitionName string, parameters Definition) (*http.Request, error) {
    73  	pathParameters := map[string]interface{}{
    74  		"policyDefinitionName": autorest.Encode("path", policyDefinitionName),
    75  		"subscriptionId":       autorest.Encode("path", client.SubscriptionID),
    76  	}
    77  
    78  	const APIVersion = "2015-10-01-preview"
    79  	queryParameters := map[string]interface{}{
    80  		"api-version": APIVersion,
    81  	}
    82  
    83  	parameters.ID = nil
    84  	preparer := autorest.CreatePreparer(
    85  		autorest.AsContentType("application/json; charset=utf-8"),
    86  		autorest.AsPut(),
    87  		autorest.WithBaseURL(client.BaseURI),
    88  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policydefinitions/{policyDefinitionName}", pathParameters),
    89  		autorest.WithJSON(parameters),
    90  		autorest.WithQueryParameters(queryParameters))
    91  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
    92  }
    93  
    94  // CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
    95  // http.Response Body if it receives an error.
    96  func (client DefinitionsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
    97  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
    98  }
    99  
   100  // CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
   101  // closes the http.Response Body.
   102  func (client DefinitionsClient) CreateOrUpdateResponder(resp *http.Response) (result Definition, err error) {
   103  	err = autorest.Respond(
   104  		resp,
   105  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
   106  		autorest.ByUnmarshallingJSON(&result),
   107  		autorest.ByClosing())
   108  	result.Response = autorest.Response{Response: resp}
   109  	return
   110  }
   111  
   112  // Delete deletes a policy definition.
   113  // Parameters:
   114  // policyDefinitionName - the name of the policy definition to delete.
   115  func (client DefinitionsClient) Delete(ctx context.Context, policyDefinitionName string) (result autorest.Response, err error) {
   116  	if tracing.IsEnabled() {
   117  		ctx = tracing.StartSpan(ctx, fqdn+"/DefinitionsClient.Delete")
   118  		defer func() {
   119  			sc := -1
   120  			if result.Response != nil {
   121  				sc = result.Response.StatusCode
   122  			}
   123  			tracing.EndSpan(ctx, sc, err)
   124  		}()
   125  	}
   126  	req, err := client.DeletePreparer(ctx, policyDefinitionName)
   127  	if err != nil {
   128  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "Delete", nil, "Failure preparing request")
   129  		return
   130  	}
   131  
   132  	resp, err := client.DeleteSender(req)
   133  	if err != nil {
   134  		result.Response = resp
   135  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "Delete", resp, "Failure sending request")
   136  		return
   137  	}
   138  
   139  	result, err = client.DeleteResponder(resp)
   140  	if err != nil {
   141  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "Delete", resp, "Failure responding to request")
   142  		return
   143  	}
   144  
   145  	return
   146  }
   147  
   148  // DeletePreparer prepares the Delete request.
   149  func (client DefinitionsClient) DeletePreparer(ctx context.Context, policyDefinitionName string) (*http.Request, error) {
   150  	pathParameters := map[string]interface{}{
   151  		"policyDefinitionName": autorest.Encode("path", policyDefinitionName),
   152  		"subscriptionId":       autorest.Encode("path", client.SubscriptionID),
   153  	}
   154  
   155  	const APIVersion = "2015-10-01-preview"
   156  	queryParameters := map[string]interface{}{
   157  		"api-version": APIVersion,
   158  	}
   159  
   160  	preparer := autorest.CreatePreparer(
   161  		autorest.AsDelete(),
   162  		autorest.WithBaseURL(client.BaseURI),
   163  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policydefinitions/{policyDefinitionName}", pathParameters),
   164  		autorest.WithQueryParameters(queryParameters))
   165  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   166  }
   167  
   168  // DeleteSender sends the Delete request. The method will close the
   169  // http.Response Body if it receives an error.
   170  func (client DefinitionsClient) DeleteSender(req *http.Request) (*http.Response, error) {
   171  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   172  }
   173  
   174  // DeleteResponder handles the response to the Delete request. The method always
   175  // closes the http.Response Body.
   176  func (client DefinitionsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
   177  	err = autorest.Respond(
   178  		resp,
   179  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
   180  		autorest.ByClosing())
   181  	result.Response = resp
   182  	return
   183  }
   184  
   185  // Get gets the policy definition.
   186  // Parameters:
   187  // policyDefinitionName - the name of the policy definition to get.
   188  func (client DefinitionsClient) Get(ctx context.Context, policyDefinitionName string) (result Definition, err error) {
   189  	if tracing.IsEnabled() {
   190  		ctx = tracing.StartSpan(ctx, fqdn+"/DefinitionsClient.Get")
   191  		defer func() {
   192  			sc := -1
   193  			if result.Response.Response != nil {
   194  				sc = result.Response.Response.StatusCode
   195  			}
   196  			tracing.EndSpan(ctx, sc, err)
   197  		}()
   198  	}
   199  	req, err := client.GetPreparer(ctx, policyDefinitionName)
   200  	if err != nil {
   201  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "Get", nil, "Failure preparing request")
   202  		return
   203  	}
   204  
   205  	resp, err := client.GetSender(req)
   206  	if err != nil {
   207  		result.Response = autorest.Response{Response: resp}
   208  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "Get", resp, "Failure sending request")
   209  		return
   210  	}
   211  
   212  	result, err = client.GetResponder(resp)
   213  	if err != nil {
   214  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "Get", resp, "Failure responding to request")
   215  		return
   216  	}
   217  
   218  	return
   219  }
   220  
   221  // GetPreparer prepares the Get request.
   222  func (client DefinitionsClient) GetPreparer(ctx context.Context, policyDefinitionName string) (*http.Request, error) {
   223  	pathParameters := map[string]interface{}{
   224  		"policyDefinitionName": autorest.Encode("path", policyDefinitionName),
   225  		"subscriptionId":       autorest.Encode("path", client.SubscriptionID),
   226  	}
   227  
   228  	const APIVersion = "2015-10-01-preview"
   229  	queryParameters := map[string]interface{}{
   230  		"api-version": APIVersion,
   231  	}
   232  
   233  	preparer := autorest.CreatePreparer(
   234  		autorest.AsGet(),
   235  		autorest.WithBaseURL(client.BaseURI),
   236  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policydefinitions/{policyDefinitionName}", pathParameters),
   237  		autorest.WithQueryParameters(queryParameters))
   238  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   239  }
   240  
   241  // GetSender sends the Get request. The method will close the
   242  // http.Response Body if it receives an error.
   243  func (client DefinitionsClient) GetSender(req *http.Request) (*http.Response, error) {
   244  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   245  }
   246  
   247  // GetResponder handles the response to the Get request. The method always
   248  // closes the http.Response Body.
   249  func (client DefinitionsClient) GetResponder(resp *http.Response) (result Definition, err error) {
   250  	err = autorest.Respond(
   251  		resp,
   252  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   253  		autorest.ByUnmarshallingJSON(&result),
   254  		autorest.ByClosing())
   255  	result.Response = autorest.Response{Response: resp}
   256  	return
   257  }
   258  
   259  // List gets all the policy definitions for a subscription.
   260  // Parameters:
   261  // filter - the filter to apply on the operation.
   262  func (client DefinitionsClient) List(ctx context.Context, filter string) (result DefinitionListResultPage, err error) {
   263  	if tracing.IsEnabled() {
   264  		ctx = tracing.StartSpan(ctx, fqdn+"/DefinitionsClient.List")
   265  		defer func() {
   266  			sc := -1
   267  			if result.dlr.Response.Response != nil {
   268  				sc = result.dlr.Response.Response.StatusCode
   269  			}
   270  			tracing.EndSpan(ctx, sc, err)
   271  		}()
   272  	}
   273  	result.fn = client.listNextResults
   274  	req, err := client.ListPreparer(ctx, filter)
   275  	if err != nil {
   276  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "List", nil, "Failure preparing request")
   277  		return
   278  	}
   279  
   280  	resp, err := client.ListSender(req)
   281  	if err != nil {
   282  		result.dlr.Response = autorest.Response{Response: resp}
   283  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "List", resp, "Failure sending request")
   284  		return
   285  	}
   286  
   287  	result.dlr, err = client.ListResponder(resp)
   288  	if err != nil {
   289  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "List", resp, "Failure responding to request")
   290  		return
   291  	}
   292  	if result.dlr.hasNextLink() && result.dlr.IsEmpty() {
   293  		err = result.NextWithContext(ctx)
   294  		return
   295  	}
   296  
   297  	return
   298  }
   299  
   300  // ListPreparer prepares the List request.
   301  func (client DefinitionsClient) ListPreparer(ctx context.Context, filter string) (*http.Request, error) {
   302  	pathParameters := map[string]interface{}{
   303  		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
   304  	}
   305  
   306  	const APIVersion = "2015-10-01-preview"
   307  	queryParameters := map[string]interface{}{
   308  		"api-version": APIVersion,
   309  	}
   310  	if len(filter) > 0 {
   311  		queryParameters["$filter"] = autorest.Encode("query", filter)
   312  	}
   313  
   314  	preparer := autorest.CreatePreparer(
   315  		autorest.AsGet(),
   316  		autorest.WithBaseURL(client.BaseURI),
   317  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policydefinitions", pathParameters),
   318  		autorest.WithQueryParameters(queryParameters))
   319  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   320  }
   321  
   322  // ListSender sends the List request. The method will close the
   323  // http.Response Body if it receives an error.
   324  func (client DefinitionsClient) ListSender(req *http.Request) (*http.Response, error) {
   325  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   326  }
   327  
   328  // ListResponder handles the response to the List request. The method always
   329  // closes the http.Response Body.
   330  func (client DefinitionsClient) ListResponder(resp *http.Response) (result DefinitionListResult, err error) {
   331  	err = autorest.Respond(
   332  		resp,
   333  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   334  		autorest.ByUnmarshallingJSON(&result),
   335  		autorest.ByClosing())
   336  	result.Response = autorest.Response{Response: resp}
   337  	return
   338  }
   339  
   340  // listNextResults retrieves the next set of results, if any.
   341  func (client DefinitionsClient) listNextResults(ctx context.Context, lastResults DefinitionListResult) (result DefinitionListResult, err error) {
   342  	req, err := lastResults.definitionListResultPreparer(ctx)
   343  	if err != nil {
   344  		return result, autorest.NewErrorWithError(err, "policy.DefinitionsClient", "listNextResults", nil, "Failure preparing next results request")
   345  	}
   346  	if req == nil {
   347  		return
   348  	}
   349  	resp, err := client.ListSender(req)
   350  	if err != nil {
   351  		result.Response = autorest.Response{Response: resp}
   352  		return result, autorest.NewErrorWithError(err, "policy.DefinitionsClient", "listNextResults", resp, "Failure sending next results request")
   353  	}
   354  	result, err = client.ListResponder(resp)
   355  	if err != nil {
   356  		err = autorest.NewErrorWithError(err, "policy.DefinitionsClient", "listNextResults", resp, "Failure responding to next results request")
   357  	}
   358  	return
   359  }
   360  
   361  // ListComplete enumerates all values, automatically crossing page boundaries as required.
   362  func (client DefinitionsClient) ListComplete(ctx context.Context, filter string) (result DefinitionListResultIterator, err error) {
   363  	if tracing.IsEnabled() {
   364  		ctx = tracing.StartSpan(ctx, fqdn+"/DefinitionsClient.List")
   365  		defer func() {
   366  			sc := -1
   367  			if result.Response().Response.Response != nil {
   368  				sc = result.page.Response().Response.Response.StatusCode
   369  			}
   370  			tracing.EndSpan(ctx, sc, err)
   371  		}()
   372  	}
   373  	result.page, err = client.List(ctx, filter)
   374  	return
   375  }
   376  

View as plain text