...

Source file src/github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2018-09-15/dtl/policies.go

Documentation: github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2018-09-15/dtl

     1  package dtl
     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 the DevTest Labs 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 replace an existing policy.
    35  // Parameters:
    36  // resourceGroupName - the name of the resource group.
    37  // labName - the name of the lab.
    38  // policySetName - the name of the policy set.
    39  // name - the name of the policy.
    40  // policy - a Policy.
    41  func (client PoliciesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy Policy) (result Policy, err error) {
    42  	if tracing.IsEnabled() {
    43  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.CreateOrUpdate")
    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  	if err := validation.Validate([]validation.Validation{
    53  		{TargetValue: policy,
    54  			Constraints: []validation.Constraint{{Target: "policy.PolicyProperties", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
    55  		return result, validation.NewError("dtl.PoliciesClient", "CreateOrUpdate", err.Error())
    56  	}
    57  
    58  	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, labName, policySetName, name, policy)
    59  	if err != nil {
    60  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "CreateOrUpdate", nil, "Failure preparing request")
    61  		return
    62  	}
    63  
    64  	resp, err := client.CreateOrUpdateSender(req)
    65  	if err != nil {
    66  		result.Response = autorest.Response{Response: resp}
    67  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "CreateOrUpdate", resp, "Failure sending request")
    68  		return
    69  	}
    70  
    71  	result, err = client.CreateOrUpdateResponder(resp)
    72  	if err != nil {
    73  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "CreateOrUpdate", resp, "Failure responding to request")
    74  		return
    75  	}
    76  
    77  	return
    78  }
    79  
    80  // CreateOrUpdatePreparer prepares the CreateOrUpdate request.
    81  func (client PoliciesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy Policy) (*http.Request, error) {
    82  	pathParameters := map[string]interface{}{
    83  		"labName":           autorest.Encode("path", labName),
    84  		"name":              autorest.Encode("path", name),
    85  		"policySetName":     autorest.Encode("path", policySetName),
    86  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
    87  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
    88  	}
    89  
    90  	const APIVersion = "2018-09-15"
    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.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters),
   100  		autorest.WithJSON(policy),
   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) (*http.Response, error) {
   108  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   109  }
   110  
   111  // CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
   112  // closes the http.Response Body.
   113  func (client PoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result Policy, err error) {
   114  	err = autorest.Respond(
   115  		resp,
   116  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
   117  		autorest.ByUnmarshallingJSON(&result),
   118  		autorest.ByClosing())
   119  	result.Response = autorest.Response{Response: resp}
   120  	return
   121  }
   122  
   123  // Delete delete policy.
   124  // Parameters:
   125  // resourceGroupName - the name of the resource group.
   126  // labName - the name of the lab.
   127  // policySetName - the name of the policy set.
   128  // name - the name of the policy.
   129  func (client PoliciesClient) Delete(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string) (result autorest.Response, err error) {
   130  	if tracing.IsEnabled() {
   131  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Delete")
   132  		defer func() {
   133  			sc := -1
   134  			if result.Response != nil {
   135  				sc = result.Response.StatusCode
   136  			}
   137  			tracing.EndSpan(ctx, sc, err)
   138  		}()
   139  	}
   140  	req, err := client.DeletePreparer(ctx, resourceGroupName, labName, policySetName, name)
   141  	if err != nil {
   142  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Delete", nil, "Failure preparing request")
   143  		return
   144  	}
   145  
   146  	resp, err := client.DeleteSender(req)
   147  	if err != nil {
   148  		result.Response = resp
   149  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Delete", resp, "Failure sending request")
   150  		return
   151  	}
   152  
   153  	result, err = client.DeleteResponder(resp)
   154  	if err != nil {
   155  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Delete", resp, "Failure responding to request")
   156  		return
   157  	}
   158  
   159  	return
   160  }
   161  
   162  // DeletePreparer prepares the Delete request.
   163  func (client PoliciesClient) DeletePreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string) (*http.Request, error) {
   164  	pathParameters := map[string]interface{}{
   165  		"labName":           autorest.Encode("path", labName),
   166  		"name":              autorest.Encode("path", name),
   167  		"policySetName":     autorest.Encode("path", policySetName),
   168  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   169  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   170  	}
   171  
   172  	const APIVersion = "2018-09-15"
   173  	queryParameters := map[string]interface{}{
   174  		"api-version": APIVersion,
   175  	}
   176  
   177  	preparer := autorest.CreatePreparer(
   178  		autorest.AsDelete(),
   179  		autorest.WithBaseURL(client.BaseURI),
   180  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters),
   181  		autorest.WithQueryParameters(queryParameters))
   182  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   183  }
   184  
   185  // DeleteSender sends the Delete request. The method will close the
   186  // http.Response Body if it receives an error.
   187  func (client PoliciesClient) DeleteSender(req *http.Request) (*http.Response, error) {
   188  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   189  }
   190  
   191  // DeleteResponder handles the response to the Delete request. The method always
   192  // closes the http.Response Body.
   193  func (client PoliciesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
   194  	err = autorest.Respond(
   195  		resp,
   196  		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
   197  		autorest.ByClosing())
   198  	result.Response = resp
   199  	return
   200  }
   201  
   202  // Get get policy.
   203  // Parameters:
   204  // resourceGroupName - the name of the resource group.
   205  // labName - the name of the lab.
   206  // policySetName - the name of the policy set.
   207  // name - the name of the policy.
   208  // expand - specify the $expand query. Example: 'properties($select=description)'
   209  func (client PoliciesClient) Get(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, expand string) (result Policy, err error) {
   210  	if tracing.IsEnabled() {
   211  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Get")
   212  		defer func() {
   213  			sc := -1
   214  			if result.Response.Response != nil {
   215  				sc = result.Response.Response.StatusCode
   216  			}
   217  			tracing.EndSpan(ctx, sc, err)
   218  		}()
   219  	}
   220  	req, err := client.GetPreparer(ctx, resourceGroupName, labName, policySetName, name, expand)
   221  	if err != nil {
   222  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Get", nil, "Failure preparing request")
   223  		return
   224  	}
   225  
   226  	resp, err := client.GetSender(req)
   227  	if err != nil {
   228  		result.Response = autorest.Response{Response: resp}
   229  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Get", resp, "Failure sending request")
   230  		return
   231  	}
   232  
   233  	result, err = client.GetResponder(resp)
   234  	if err != nil {
   235  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Get", resp, "Failure responding to request")
   236  		return
   237  	}
   238  
   239  	return
   240  }
   241  
   242  // GetPreparer prepares the Get request.
   243  func (client PoliciesClient) GetPreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, expand string) (*http.Request, error) {
   244  	pathParameters := map[string]interface{}{
   245  		"labName":           autorest.Encode("path", labName),
   246  		"name":              autorest.Encode("path", name),
   247  		"policySetName":     autorest.Encode("path", policySetName),
   248  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   249  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   250  	}
   251  
   252  	const APIVersion = "2018-09-15"
   253  	queryParameters := map[string]interface{}{
   254  		"api-version": APIVersion,
   255  	}
   256  	if len(expand) > 0 {
   257  		queryParameters["$expand"] = autorest.Encode("query", expand)
   258  	}
   259  
   260  	preparer := autorest.CreatePreparer(
   261  		autorest.AsGet(),
   262  		autorest.WithBaseURL(client.BaseURI),
   263  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters),
   264  		autorest.WithQueryParameters(queryParameters))
   265  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   266  }
   267  
   268  // GetSender sends the Get request. The method will close the
   269  // http.Response Body if it receives an error.
   270  func (client PoliciesClient) GetSender(req *http.Request) (*http.Response, error) {
   271  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   272  }
   273  
   274  // GetResponder handles the response to the Get request. The method always
   275  // closes the http.Response Body.
   276  func (client PoliciesClient) GetResponder(resp *http.Response) (result Policy, err error) {
   277  	err = autorest.Respond(
   278  		resp,
   279  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   280  		autorest.ByUnmarshallingJSON(&result),
   281  		autorest.ByClosing())
   282  	result.Response = autorest.Response{Response: resp}
   283  	return
   284  }
   285  
   286  // List list policies in a given policy set.
   287  // Parameters:
   288  // resourceGroupName - the name of the resource group.
   289  // labName - the name of the lab.
   290  // policySetName - the name of the policy set.
   291  // expand - specify the $expand query. Example: 'properties($select=description)'
   292  // filter - the filter to apply to the operation. Example: '$filter=contains(name,'myName')
   293  // top - the maximum number of resources to return from the operation. Example: '$top=10'
   294  // orderby - the ordering expression for the results, using OData notation. Example: '$orderby=name desc'
   295  func (client PoliciesClient) List(ctx context.Context, resourceGroupName string, labName string, policySetName string, expand string, filter string, top *int32, orderby string) (result PolicyListPage, err error) {
   296  	if tracing.IsEnabled() {
   297  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List")
   298  		defer func() {
   299  			sc := -1
   300  			if result.pl.Response.Response != nil {
   301  				sc = result.pl.Response.Response.StatusCode
   302  			}
   303  			tracing.EndSpan(ctx, sc, err)
   304  		}()
   305  	}
   306  	result.fn = client.listNextResults
   307  	req, err := client.ListPreparer(ctx, resourceGroupName, labName, policySetName, expand, filter, top, orderby)
   308  	if err != nil {
   309  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "List", nil, "Failure preparing request")
   310  		return
   311  	}
   312  
   313  	resp, err := client.ListSender(req)
   314  	if err != nil {
   315  		result.pl.Response = autorest.Response{Response: resp}
   316  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "List", resp, "Failure sending request")
   317  		return
   318  	}
   319  
   320  	result.pl, err = client.ListResponder(resp)
   321  	if err != nil {
   322  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "List", resp, "Failure responding to request")
   323  		return
   324  	}
   325  	if result.pl.hasNextLink() && result.pl.IsEmpty() {
   326  		err = result.NextWithContext(ctx)
   327  		return
   328  	}
   329  
   330  	return
   331  }
   332  
   333  // ListPreparer prepares the List request.
   334  func (client PoliciesClient) ListPreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, expand string, filter string, top *int32, orderby string) (*http.Request, error) {
   335  	pathParameters := map[string]interface{}{
   336  		"labName":           autorest.Encode("path", labName),
   337  		"policySetName":     autorest.Encode("path", policySetName),
   338  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   339  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   340  	}
   341  
   342  	const APIVersion = "2018-09-15"
   343  	queryParameters := map[string]interface{}{
   344  		"api-version": APIVersion,
   345  	}
   346  	if len(expand) > 0 {
   347  		queryParameters["$expand"] = autorest.Encode("query", expand)
   348  	}
   349  	if len(filter) > 0 {
   350  		queryParameters["$filter"] = autorest.Encode("query", filter)
   351  	}
   352  	if top != nil {
   353  		queryParameters["$top"] = autorest.Encode("query", *top)
   354  	}
   355  	if len(orderby) > 0 {
   356  		queryParameters["$orderby"] = autorest.Encode("query", orderby)
   357  	}
   358  
   359  	preparer := autorest.CreatePreparer(
   360  		autorest.AsGet(),
   361  		autorest.WithBaseURL(client.BaseURI),
   362  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies", pathParameters),
   363  		autorest.WithQueryParameters(queryParameters))
   364  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   365  }
   366  
   367  // ListSender sends the List request. The method will close the
   368  // http.Response Body if it receives an error.
   369  func (client PoliciesClient) ListSender(req *http.Request) (*http.Response, error) {
   370  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   371  }
   372  
   373  // ListResponder handles the response to the List request. The method always
   374  // closes the http.Response Body.
   375  func (client PoliciesClient) ListResponder(resp *http.Response) (result PolicyList, err error) {
   376  	err = autorest.Respond(
   377  		resp,
   378  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   379  		autorest.ByUnmarshallingJSON(&result),
   380  		autorest.ByClosing())
   381  	result.Response = autorest.Response{Response: resp}
   382  	return
   383  }
   384  
   385  // listNextResults retrieves the next set of results, if any.
   386  func (client PoliciesClient) listNextResults(ctx context.Context, lastResults PolicyList) (result PolicyList, err error) {
   387  	req, err := lastResults.policyListPreparer(ctx)
   388  	if err != nil {
   389  		return result, autorest.NewErrorWithError(err, "dtl.PoliciesClient", "listNextResults", nil, "Failure preparing next results request")
   390  	}
   391  	if req == nil {
   392  		return
   393  	}
   394  	resp, err := client.ListSender(req)
   395  	if err != nil {
   396  		result.Response = autorest.Response{Response: resp}
   397  		return result, autorest.NewErrorWithError(err, "dtl.PoliciesClient", "listNextResults", resp, "Failure sending next results request")
   398  	}
   399  	result, err = client.ListResponder(resp)
   400  	if err != nil {
   401  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "listNextResults", resp, "Failure responding to next results request")
   402  	}
   403  	return
   404  }
   405  
   406  // ListComplete enumerates all values, automatically crossing page boundaries as required.
   407  func (client PoliciesClient) ListComplete(ctx context.Context, resourceGroupName string, labName string, policySetName string, expand string, filter string, top *int32, orderby string) (result PolicyListIterator, err error) {
   408  	if tracing.IsEnabled() {
   409  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List")
   410  		defer func() {
   411  			sc := -1
   412  			if result.Response().Response.Response != nil {
   413  				sc = result.page.Response().Response.Response.StatusCode
   414  			}
   415  			tracing.EndSpan(ctx, sc, err)
   416  		}()
   417  	}
   418  	result.page, err = client.List(ctx, resourceGroupName, labName, policySetName, expand, filter, top, orderby)
   419  	return
   420  }
   421  
   422  // Update allows modifying tags of policies. All other properties will be ignored.
   423  // Parameters:
   424  // resourceGroupName - the name of the resource group.
   425  // labName - the name of the lab.
   426  // policySetName - the name of the policy set.
   427  // name - the name of the policy.
   428  // policy - a Policy.
   429  func (client PoliciesClient) Update(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy PolicyFragment) (result Policy, err error) {
   430  	if tracing.IsEnabled() {
   431  		ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Update")
   432  		defer func() {
   433  			sc := -1
   434  			if result.Response.Response != nil {
   435  				sc = result.Response.Response.StatusCode
   436  			}
   437  			tracing.EndSpan(ctx, sc, err)
   438  		}()
   439  	}
   440  	req, err := client.UpdatePreparer(ctx, resourceGroupName, labName, policySetName, name, policy)
   441  	if err != nil {
   442  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Update", nil, "Failure preparing request")
   443  		return
   444  	}
   445  
   446  	resp, err := client.UpdateSender(req)
   447  	if err != nil {
   448  		result.Response = autorest.Response{Response: resp}
   449  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Update", resp, "Failure sending request")
   450  		return
   451  	}
   452  
   453  	result, err = client.UpdateResponder(resp)
   454  	if err != nil {
   455  		err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Update", resp, "Failure responding to request")
   456  		return
   457  	}
   458  
   459  	return
   460  }
   461  
   462  // UpdatePreparer prepares the Update request.
   463  func (client PoliciesClient) UpdatePreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy PolicyFragment) (*http.Request, error) {
   464  	pathParameters := map[string]interface{}{
   465  		"labName":           autorest.Encode("path", labName),
   466  		"name":              autorest.Encode("path", name),
   467  		"policySetName":     autorest.Encode("path", policySetName),
   468  		"resourceGroupName": autorest.Encode("path", resourceGroupName),
   469  		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
   470  	}
   471  
   472  	const APIVersion = "2018-09-15"
   473  	queryParameters := map[string]interface{}{
   474  		"api-version": APIVersion,
   475  	}
   476  
   477  	preparer := autorest.CreatePreparer(
   478  		autorest.AsContentType("application/json; charset=utf-8"),
   479  		autorest.AsPatch(),
   480  		autorest.WithBaseURL(client.BaseURI),
   481  		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters),
   482  		autorest.WithJSON(policy),
   483  		autorest.WithQueryParameters(queryParameters))
   484  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   485  }
   486  
   487  // UpdateSender sends the Update request. The method will close the
   488  // http.Response Body if it receives an error.
   489  func (client PoliciesClient) UpdateSender(req *http.Request) (*http.Response, error) {
   490  	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
   491  }
   492  
   493  // UpdateResponder handles the response to the Update request. The method always
   494  // closes the http.Response Body.
   495  func (client PoliciesClient) UpdateResponder(resp *http.Response) (result Policy, err error) {
   496  	err = autorest.Respond(
   497  		resp,
   498  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   499  		autorest.ByUnmarshallingJSON(&result),
   500  		autorest.ByClosing())
   501  	result.Response = autorest.Response{Response: resp}
   502  	return
   503  }
   504  

View as plain text