1package dtl 2 3// Copyright (c) Microsoft and contributors. All rights reserved. 4// 5// Licensed under the Apache License, Version 2.0 (the "License"); 6// you may not use this file except in compliance with the License. 7// You may obtain a copy of the License at 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// 14// See the License for the specific language governing permissions and 15// limitations under the License. 16// 17// Code generated by Microsoft (R) AutoRest Code Generator. 18// Changes may cause incorrect behavior and will be lost if the code is regenerated. 19 20import ( 21 "context" 22 "github.com/Azure/go-autorest/autorest" 23 "github.com/Azure/go-autorest/autorest/azure" 24 "github.com/Azure/go-autorest/autorest/validation" 25 "github.com/Azure/go-autorest/tracing" 26 "net/http" 27) 28 29// PoliciesClient is the the DevTest Labs Client. 30type PoliciesClient struct { 31 BaseClient 32} 33 34// NewPoliciesClient creates an instance of the PoliciesClient client. 35func NewPoliciesClient(subscriptionID string) PoliciesClient { 36 return NewPoliciesClientWithBaseURI(DefaultBaseURI, subscriptionID) 37} 38 39// NewPoliciesClientWithBaseURI creates an instance of the PoliciesClient client using a custom endpoint. Use this 40// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack). 41func NewPoliciesClientWithBaseURI(baseURI string, subscriptionID string) PoliciesClient { 42 return PoliciesClient{NewWithBaseURI(baseURI, subscriptionID)} 43} 44 45// CreateOrUpdate create or replace an existing policy. 46// Parameters: 47// resourceGroupName - the name of the resource group. 48// labName - the name of the lab. 49// policySetName - the name of the policy set. 50// name - the name of the policy. 51// policy - a Policy. 52func (client PoliciesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy Policy) (result Policy, err error) { 53 if tracing.IsEnabled() { 54 ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.CreateOrUpdate") 55 defer func() { 56 sc := -1 57 if result.Response.Response != nil { 58 sc = result.Response.Response.StatusCode 59 } 60 tracing.EndSpan(ctx, sc, err) 61 }() 62 } 63 if err := validation.Validate([]validation.Validation{ 64 {TargetValue: policy, 65 Constraints: []validation.Constraint{{Target: "policy.PolicyProperties", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil { 66 return result, validation.NewError("dtl.PoliciesClient", "CreateOrUpdate", err.Error()) 67 } 68 69 req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, labName, policySetName, name, policy) 70 if err != nil { 71 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "CreateOrUpdate", nil, "Failure preparing request") 72 return 73 } 74 75 resp, err := client.CreateOrUpdateSender(req) 76 if err != nil { 77 result.Response = autorest.Response{Response: resp} 78 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "CreateOrUpdate", resp, "Failure sending request") 79 return 80 } 81 82 result, err = client.CreateOrUpdateResponder(resp) 83 if err != nil { 84 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "CreateOrUpdate", resp, "Failure responding to request") 85 } 86 87 return 88} 89 90// CreateOrUpdatePreparer prepares the CreateOrUpdate request. 91func (client PoliciesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy Policy) (*http.Request, error) { 92 pathParameters := map[string]interface{}{ 93 "labName": autorest.Encode("path", labName), 94 "name": autorest.Encode("path", name), 95 "policySetName": autorest.Encode("path", policySetName), 96 "resourceGroupName": autorest.Encode("path", resourceGroupName), 97 "subscriptionId": autorest.Encode("path", client.SubscriptionID), 98 } 99 100 const APIVersion = "2016-05-15" 101 queryParameters := map[string]interface{}{ 102 "api-version": APIVersion, 103 } 104 105 preparer := autorest.CreatePreparer( 106 autorest.AsContentType("application/json; charset=utf-8"), 107 autorest.AsPut(), 108 autorest.WithBaseURL(client.BaseURI), 109 autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters), 110 autorest.WithJSON(policy), 111 autorest.WithQueryParameters(queryParameters)) 112 return preparer.Prepare((&http.Request{}).WithContext(ctx)) 113} 114 115// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the 116// http.Response Body if it receives an error. 117func (client PoliciesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) { 118 return client.Send(req, azure.DoRetryWithRegistration(client.Client)) 119} 120 121// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always 122// closes the http.Response Body. 123func (client PoliciesClient) CreateOrUpdateResponder(resp *http.Response) (result Policy, err error) { 124 err = autorest.Respond( 125 resp, 126 client.ByInspecting(), 127 azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated), 128 autorest.ByUnmarshallingJSON(&result), 129 autorest.ByClosing()) 130 result.Response = autorest.Response{Response: resp} 131 return 132} 133 134// Delete delete policy. 135// Parameters: 136// resourceGroupName - the name of the resource group. 137// labName - the name of the lab. 138// policySetName - the name of the policy set. 139// name - the name of the policy. 140func (client PoliciesClient) Delete(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string) (result autorest.Response, err error) { 141 if tracing.IsEnabled() { 142 ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Delete") 143 defer func() { 144 sc := -1 145 if result.Response != nil { 146 sc = result.Response.StatusCode 147 } 148 tracing.EndSpan(ctx, sc, err) 149 }() 150 } 151 req, err := client.DeletePreparer(ctx, resourceGroupName, labName, policySetName, name) 152 if err != nil { 153 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Delete", nil, "Failure preparing request") 154 return 155 } 156 157 resp, err := client.DeleteSender(req) 158 if err != nil { 159 result.Response = resp 160 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Delete", resp, "Failure sending request") 161 return 162 } 163 164 result, err = client.DeleteResponder(resp) 165 if err != nil { 166 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Delete", resp, "Failure responding to request") 167 } 168 169 return 170} 171 172// DeletePreparer prepares the Delete request. 173func (client PoliciesClient) DeletePreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string) (*http.Request, error) { 174 pathParameters := map[string]interface{}{ 175 "labName": autorest.Encode("path", labName), 176 "name": autorest.Encode("path", name), 177 "policySetName": autorest.Encode("path", policySetName), 178 "resourceGroupName": autorest.Encode("path", resourceGroupName), 179 "subscriptionId": autorest.Encode("path", client.SubscriptionID), 180 } 181 182 const APIVersion = "2016-05-15" 183 queryParameters := map[string]interface{}{ 184 "api-version": APIVersion, 185 } 186 187 preparer := autorest.CreatePreparer( 188 autorest.AsDelete(), 189 autorest.WithBaseURL(client.BaseURI), 190 autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters), 191 autorest.WithQueryParameters(queryParameters)) 192 return preparer.Prepare((&http.Request{}).WithContext(ctx)) 193} 194 195// DeleteSender sends the Delete request. The method will close the 196// http.Response Body if it receives an error. 197func (client PoliciesClient) DeleteSender(req *http.Request) (*http.Response, error) { 198 return client.Send(req, azure.DoRetryWithRegistration(client.Client)) 199} 200 201// DeleteResponder handles the response to the Delete request. The method always 202// closes the http.Response Body. 203func (client PoliciesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) { 204 err = autorest.Respond( 205 resp, 206 client.ByInspecting(), 207 azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent), 208 autorest.ByClosing()) 209 result.Response = resp 210 return 211} 212 213// Get get policy. 214// Parameters: 215// resourceGroupName - the name of the resource group. 216// labName - the name of the lab. 217// policySetName - the name of the policy set. 218// name - the name of the policy. 219// expand - specify the $expand query. Example: 'properties($select=description)' 220func (client PoliciesClient) Get(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, expand string) (result Policy, err error) { 221 if tracing.IsEnabled() { 222 ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Get") 223 defer func() { 224 sc := -1 225 if result.Response.Response != nil { 226 sc = result.Response.Response.StatusCode 227 } 228 tracing.EndSpan(ctx, sc, err) 229 }() 230 } 231 req, err := client.GetPreparer(ctx, resourceGroupName, labName, policySetName, name, expand) 232 if err != nil { 233 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Get", nil, "Failure preparing request") 234 return 235 } 236 237 resp, err := client.GetSender(req) 238 if err != nil { 239 result.Response = autorest.Response{Response: resp} 240 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Get", resp, "Failure sending request") 241 return 242 } 243 244 result, err = client.GetResponder(resp) 245 if err != nil { 246 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Get", resp, "Failure responding to request") 247 } 248 249 return 250} 251 252// GetPreparer prepares the Get request. 253func (client PoliciesClient) GetPreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, expand string) (*http.Request, error) { 254 pathParameters := map[string]interface{}{ 255 "labName": autorest.Encode("path", labName), 256 "name": autorest.Encode("path", name), 257 "policySetName": autorest.Encode("path", policySetName), 258 "resourceGroupName": autorest.Encode("path", resourceGroupName), 259 "subscriptionId": autorest.Encode("path", client.SubscriptionID), 260 } 261 262 const APIVersion = "2016-05-15" 263 queryParameters := map[string]interface{}{ 264 "api-version": APIVersion, 265 } 266 if len(expand) > 0 { 267 queryParameters["$expand"] = autorest.Encode("query", expand) 268 } 269 270 preparer := autorest.CreatePreparer( 271 autorest.AsGet(), 272 autorest.WithBaseURL(client.BaseURI), 273 autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters), 274 autorest.WithQueryParameters(queryParameters)) 275 return preparer.Prepare((&http.Request{}).WithContext(ctx)) 276} 277 278// GetSender sends the Get request. The method will close the 279// http.Response Body if it receives an error. 280func (client PoliciesClient) GetSender(req *http.Request) (*http.Response, error) { 281 return client.Send(req, azure.DoRetryWithRegistration(client.Client)) 282} 283 284// GetResponder handles the response to the Get request. The method always 285// closes the http.Response Body. 286func (client PoliciesClient) GetResponder(resp *http.Response) (result Policy, err error) { 287 err = autorest.Respond( 288 resp, 289 client.ByInspecting(), 290 azure.WithErrorUnlessStatusCode(http.StatusOK), 291 autorest.ByUnmarshallingJSON(&result), 292 autorest.ByClosing()) 293 result.Response = autorest.Response{Response: resp} 294 return 295} 296 297// List list policies in a given policy set. 298// Parameters: 299// resourceGroupName - the name of the resource group. 300// labName - the name of the lab. 301// policySetName - the name of the policy set. 302// expand - specify the $expand query. Example: 'properties($select=description)' 303// filter - the filter to apply to the operation. 304// top - the maximum number of resources to return from the operation. 305// orderby - the ordering expression for the results, using OData notation. 306func (client PoliciesClient) List(ctx context.Context, resourceGroupName string, labName string, policySetName string, expand string, filter string, top *int32, orderby string) (result ResponseWithContinuationPolicyPage, err error) { 307 if tracing.IsEnabled() { 308 ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List") 309 defer func() { 310 sc := -1 311 if result.rwcp.Response.Response != nil { 312 sc = result.rwcp.Response.Response.StatusCode 313 } 314 tracing.EndSpan(ctx, sc, err) 315 }() 316 } 317 result.fn = client.listNextResults 318 req, err := client.ListPreparer(ctx, resourceGroupName, labName, policySetName, expand, filter, top, orderby) 319 if err != nil { 320 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "List", nil, "Failure preparing request") 321 return 322 } 323 324 resp, err := client.ListSender(req) 325 if err != nil { 326 result.rwcp.Response = autorest.Response{Response: resp} 327 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "List", resp, "Failure sending request") 328 return 329 } 330 331 result.rwcp, err = client.ListResponder(resp) 332 if err != nil { 333 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "List", resp, "Failure responding to request") 334 } 335 336 return 337} 338 339// ListPreparer prepares the List request. 340func (client PoliciesClient) ListPreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, expand string, filter string, top *int32, orderby string) (*http.Request, error) { 341 pathParameters := map[string]interface{}{ 342 "labName": autorest.Encode("path", labName), 343 "policySetName": autorest.Encode("path", policySetName), 344 "resourceGroupName": autorest.Encode("path", resourceGroupName), 345 "subscriptionId": autorest.Encode("path", client.SubscriptionID), 346 } 347 348 const APIVersion = "2016-05-15" 349 queryParameters := map[string]interface{}{ 350 "api-version": APIVersion, 351 } 352 if len(expand) > 0 { 353 queryParameters["$expand"] = autorest.Encode("query", expand) 354 } 355 if len(filter) > 0 { 356 queryParameters["$filter"] = autorest.Encode("query", filter) 357 } 358 if top != nil { 359 queryParameters["$top"] = autorest.Encode("query", *top) 360 } 361 if len(orderby) > 0 { 362 queryParameters["$orderby"] = autorest.Encode("query", orderby) 363 } 364 365 preparer := autorest.CreatePreparer( 366 autorest.AsGet(), 367 autorest.WithBaseURL(client.BaseURI), 368 autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies", pathParameters), 369 autorest.WithQueryParameters(queryParameters)) 370 return preparer.Prepare((&http.Request{}).WithContext(ctx)) 371} 372 373// ListSender sends the List request. The method will close the 374// http.Response Body if it receives an error. 375func (client PoliciesClient) ListSender(req *http.Request) (*http.Response, error) { 376 return client.Send(req, azure.DoRetryWithRegistration(client.Client)) 377} 378 379// ListResponder handles the response to the List request. The method always 380// closes the http.Response Body. 381func (client PoliciesClient) ListResponder(resp *http.Response) (result ResponseWithContinuationPolicy, err error) { 382 err = autorest.Respond( 383 resp, 384 client.ByInspecting(), 385 azure.WithErrorUnlessStatusCode(http.StatusOK), 386 autorest.ByUnmarshallingJSON(&result), 387 autorest.ByClosing()) 388 result.Response = autorest.Response{Response: resp} 389 return 390} 391 392// listNextResults retrieves the next set of results, if any. 393func (client PoliciesClient) listNextResults(ctx context.Context, lastResults ResponseWithContinuationPolicy) (result ResponseWithContinuationPolicy, err error) { 394 req, err := lastResults.responseWithContinuationPolicyPreparer(ctx) 395 if err != nil { 396 return result, autorest.NewErrorWithError(err, "dtl.PoliciesClient", "listNextResults", nil, "Failure preparing next results request") 397 } 398 if req == nil { 399 return 400 } 401 resp, err := client.ListSender(req) 402 if err != nil { 403 result.Response = autorest.Response{Response: resp} 404 return result, autorest.NewErrorWithError(err, "dtl.PoliciesClient", "listNextResults", resp, "Failure sending next results request") 405 } 406 result, err = client.ListResponder(resp) 407 if err != nil { 408 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "listNextResults", resp, "Failure responding to next results request") 409 } 410 return 411} 412 413// ListComplete enumerates all values, automatically crossing page boundaries as required. 414func (client PoliciesClient) ListComplete(ctx context.Context, resourceGroupName string, labName string, policySetName string, expand string, filter string, top *int32, orderby string) (result ResponseWithContinuationPolicyIterator, err error) { 415 if tracing.IsEnabled() { 416 ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.List") 417 defer func() { 418 sc := -1 419 if result.Response().Response.Response != nil { 420 sc = result.page.Response().Response.Response.StatusCode 421 } 422 tracing.EndSpan(ctx, sc, err) 423 }() 424 } 425 result.page, err = client.List(ctx, resourceGroupName, labName, policySetName, expand, filter, top, orderby) 426 return 427} 428 429// Update modify properties of policies. 430// Parameters: 431// resourceGroupName - the name of the resource group. 432// labName - the name of the lab. 433// policySetName - the name of the policy set. 434// name - the name of the policy. 435// policy - a Policy. 436func (client PoliciesClient) Update(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy PolicyFragment) (result Policy, err error) { 437 if tracing.IsEnabled() { 438 ctx = tracing.StartSpan(ctx, fqdn+"/PoliciesClient.Update") 439 defer func() { 440 sc := -1 441 if result.Response.Response != nil { 442 sc = result.Response.Response.StatusCode 443 } 444 tracing.EndSpan(ctx, sc, err) 445 }() 446 } 447 req, err := client.UpdatePreparer(ctx, resourceGroupName, labName, policySetName, name, policy) 448 if err != nil { 449 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Update", nil, "Failure preparing request") 450 return 451 } 452 453 resp, err := client.UpdateSender(req) 454 if err != nil { 455 result.Response = autorest.Response{Response: resp} 456 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Update", resp, "Failure sending request") 457 return 458 } 459 460 result, err = client.UpdateResponder(resp) 461 if err != nil { 462 err = autorest.NewErrorWithError(err, "dtl.PoliciesClient", "Update", resp, "Failure responding to request") 463 } 464 465 return 466} 467 468// UpdatePreparer prepares the Update request. 469func (client PoliciesClient) UpdatePreparer(ctx context.Context, resourceGroupName string, labName string, policySetName string, name string, policy PolicyFragment) (*http.Request, error) { 470 pathParameters := map[string]interface{}{ 471 "labName": autorest.Encode("path", labName), 472 "name": autorest.Encode("path", name), 473 "policySetName": autorest.Encode("path", policySetName), 474 "resourceGroupName": autorest.Encode("path", resourceGroupName), 475 "subscriptionId": autorest.Encode("path", client.SubscriptionID), 476 } 477 478 const APIVersion = "2016-05-15" 479 queryParameters := map[string]interface{}{ 480 "api-version": APIVersion, 481 } 482 483 preparer := autorest.CreatePreparer( 484 autorest.AsContentType("application/json; charset=utf-8"), 485 autorest.AsPatch(), 486 autorest.WithBaseURL(client.BaseURI), 487 autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/policysets/{policySetName}/policies/{name}", pathParameters), 488 autorest.WithJSON(policy), 489 autorest.WithQueryParameters(queryParameters)) 490 return preparer.Prepare((&http.Request{}).WithContext(ctx)) 491} 492 493// UpdateSender sends the Update request. The method will close the 494// http.Response Body if it receives an error. 495func (client PoliciesClient) UpdateSender(req *http.Request) (*http.Response, error) { 496 return client.Send(req, azure.DoRetryWithRegistration(client.Client)) 497} 498 499// UpdateResponder handles the response to the Update request. The method always 500// closes the http.Response Body. 501func (client PoliciesClient) UpdateResponder(resp *http.Response) (result Policy, err error) { 502 err = autorest.Respond( 503 resp, 504 client.ByInspecting(), 505 azure.WithErrorUnlessStatusCode(http.StatusOK), 506 autorest.ByUnmarshallingJSON(&result), 507 autorest.ByClosing()) 508 result.Response = autorest.Response{Response: resp} 509 return 510} 511