github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/azure/internal/azureresources/resources.go (about) 1 // This file is based on code from Azure/azure-sdk-for-go, 2 // which is Copyright Microsoft Corporation. See the LICENSE 3 // file in this directory for details. 4 package resources 5 6 import ( 7 "context" 8 "net/http" 9 10 "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" 11 "github.com/Azure/go-autorest/autorest" 12 "github.com/Azure/go-autorest/autorest/validation" 13 "github.com/Azure/go-autorest/tracing" 14 ) 15 16 // The package's fully qualified name. 17 const fqdn = "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" 18 19 // ResourcesClient wraps resources.Client, providing methods for 20 // dealing with generic resources. 21 // 22 // NOTE(axw) this wrapper is necessary only to work around an issue in 23 // the generated Client, which hard-codes the API version: 24 // https://github.com/Azure/azure-sdk-for-go/issues/741 25 // When this issue has been resolved, we should drop this code and use 26 // the SDK's client directly. 27 type ResourcesClient struct { 28 *resources.Client 29 } 30 31 // GetByID gets a resource by ID. 32 // 33 // See: resources.Client.GetByID. 34 func (client ResourcesClient) GetByID(ctx context.Context, resourceID, apiVersion string) (result resources.GenericResource, err error) { 35 if tracing.IsEnabled() { 36 ctx = tracing.StartSpan(ctx, fqdn+"/Client.GetByID") 37 defer func() { 38 sc := -1 39 if result.Response.Response != nil { 40 sc = result.Response.Response.StatusCode 41 } 42 tracing.EndSpan(ctx, sc, err) 43 }() 44 } 45 req, err := client.GetByIDPreparer(ctx, resourceID) 46 if err != nil { 47 err = autorest.NewErrorWithError(err, "resources.Client", "GetByID", nil, "Failure preparing request") 48 return 49 } 50 setAPIVersion(req, apiVersion) 51 52 resp, err := client.GetByIDSender(req) 53 if err != nil { 54 result.Response = autorest.Response{Response: resp} 55 err = autorest.NewErrorWithError(err, "resources.Client", "GetByID", resp, "Failure sending request") 56 return 57 } 58 59 result, err = client.GetByIDResponder(resp) 60 if err != nil { 61 err = autorest.NewErrorWithError(err, "resources.Client", "GetByID", resp, "Failure responding to request") 62 } 63 64 return 65 } 66 67 // CreateOrUpdateByID creates a resource. 68 // 69 // See: resources.Client.CreateOrUpdateByID. 70 func (client ResourcesClient) CreateOrUpdateByID(ctx context.Context, resourceID string, parameters resources.GenericResource, apiVersion string) (result resources.CreateOrUpdateByIDFuture, err error) { 71 if tracing.IsEnabled() { 72 ctx = tracing.StartSpan(ctx, fqdn+"/Client.CreateOrUpdateByID") 73 defer func() { 74 sc := -1 75 if result.Response() != nil { 76 sc = result.Response().StatusCode 77 } 78 tracing.EndSpan(ctx, sc, err) 79 }() 80 } 81 if err := validation.Validate([]validation.Validation{ 82 {TargetValue: parameters, 83 Constraints: []validation.Constraint{{Target: "parameters.Kind", Name: validation.Null, Rule: false, 84 Chain: []validation.Constraint{{Target: "parameters.Kind", Name: validation.Pattern, Rule: `^[-\w\._,\(\)]+$`, Chain: nil}}}}}}); err != nil { 85 return result, validation.NewError("resources.Client", "CreateOrUpdateByID", err.Error()) 86 } 87 88 req, err := client.CreateOrUpdateByIDPreparer(ctx, resourceID, parameters) 89 if err != nil { 90 err = autorest.NewErrorWithError(err, "resources.Client", "CreateOrUpdateByID", nil, "Failure preparing request") 91 return 92 } 93 setAPIVersion(req, apiVersion) 94 95 result, err = client.CreateOrUpdateByIDSender(req) 96 if err != nil { 97 err = autorest.NewErrorWithError(err, "resources.Client", "CreateOrUpdateByID", result.Response(), "Failure sending request") 98 return 99 } 100 101 return 102 } 103 104 func setAPIVersion(req *http.Request, apiVersion string) { 105 // Replace the API version. 106 query := req.URL.Query() 107 query.Set("api-version", apiVersion) 108 req.URL.RawQuery = query.Encode() 109 }