| 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | |
| 8 | "github.com/Azure/go-autorest/autorest" |
| 9 | "github.com/Azure/go-autorest/autorest/azure" |
| 10 | "github.com/Azure/go-autorest/autorest/validation" |
| 11 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 12 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 13 | ) |
| 14 | |
| 15 | // SetMetaData sets the specified MetaData on the Container without a Lease ID |
| 16 | func (client Client) SetMetaData(ctx context.Context, accountName, containerName string, metaData map[string]string) (autorest.Response, error) { |
| 17 | return client.SetMetaDataWithLeaseID(ctx, accountName, containerName, "", metaData) |
| 18 | } |
| 19 | |
| 20 | // SetMetaDataWithLeaseID sets the specified MetaData on the Container using the specified Lease ID |
| 21 | func (client Client) SetMetaDataWithLeaseID(ctx context.Context, accountName, containerName, leaseID string, metaData map[string]string) (result autorest.Response, err error) { |
| 22 | if accountName == "" { |
| 23 | return result, validation.NewError("containers.Client", "SetMetaData", "`accountName` cannot be an empty string.") |
| 24 | } |
| 25 | if containerName == "" { |
| 26 | return result, validation.NewError("containers.Client", "SetMetaData", "`containerName` cannot be an empty string.") |
| 27 | } |
| 28 | if err := metadata.Validate(metaData); err != nil { |
| 29 | return result, validation.NewError("containers.Client", "SetMetaData", fmt.Sprintf("`metaData` is not valid: %s.", err)) |
| 30 | } |
| 31 | |
| 32 | req, err := client.SetMetaDataWithLeaseIDPreparer(ctx, accountName, containerName, leaseID, metaData) |
| 33 | if err != nil { |
| 34 | err = autorest.NewErrorWithError(err, "containers.Client", "SetMetaData", nil, "Failure preparing request") |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | resp, err := client.SetMetaDataWithLeaseIDSender(req) |
| 39 | if err != nil { |
| 40 | result = autorest.Response{Response: resp} |
| 41 | err = autorest.NewErrorWithError(err, "containers.Client", "SetMetaData", resp, "Failure sending request") |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | result, err = client.SetMetaDataWithLeaseIDResponder(resp) |
| 46 | if err != nil { |
| 47 | err = autorest.NewErrorWithError(err, "containers.Client", "SetMetaData", resp, "Failure responding to request") |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | // SetMetaDataWithLeaseIDPreparer prepares the SetMetaDataWithLeaseID request. |
| 55 | func (client Client) SetMetaDataWithLeaseIDPreparer(ctx context.Context, accountName, containerName, leaseID string, metaData map[string]string) (*http.Request, error) { |
| 56 | pathParameters := map[string]interface{}{ |
| 57 | "containerName": autorest.Encode("path", containerName), |
| 58 | } |
| 59 | |
| 60 | queryParameters := map[string]interface{}{ |
| 61 | "comp": autorest.Encode("path", "metadata"), |
| 62 | "restype": autorest.Encode("path", "container"), |
| 63 | } |
| 64 | |
| 65 | headers := map[string]interface{}{ |
| 66 | "x-ms-version": APIVersion, |
| 67 | } |
| 68 | |
| 69 | headers = metadata.SetIntoHeaders(headers, metaData) |
| 70 | |
| 71 | // If specified, Get Container Properties only succeeds if the container’s lease is active and matches this ID. |
| 72 | // If there is no active lease or the ID does not match, 412 (Precondition Failed) is returned. |
| 73 | if leaseID != "" { |
| 74 | headers["x-ms-lease-id"] = leaseID |
| 75 | } |
| 76 | |
| 77 | preparer := autorest.CreatePreparer( |
| 78 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 79 | autorest.AsPut(), |
| 80 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 81 | autorest.WithPathParameters("/{containerName}", pathParameters), |
| 82 | autorest.WithQueryParameters(queryParameters), |
| 83 | autorest.WithHeaders(headers)) |
| 84 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 85 | } |
| 86 | |
| 87 | // SetMetaDataWithLeaseIDSender sends the SetMetaDataWithLeaseID request. The method will close the |
| 88 | // http.Response Body if it receives an error. |
| 89 | func (client Client) SetMetaDataWithLeaseIDSender(req *http.Request) (*http.Response, error) { |
| 90 | return autorest.SendWithSender(client, req, |
| 91 | azure.DoRetryWithRegistration(client.Client)) |
| 92 | } |
| 93 | |
| 94 | // SetMetaDataWithLeaseIDResponder handles the response to the SetMetaDataWithLeaseID request. The method always |
| 95 | // closes the http.Response Body. |
| 96 | func (client Client) SetMetaDataWithLeaseIDResponder(resp *http.Response) (result autorest.Response, err error) { |
| 97 | err = autorest.Respond( |
| 98 | resp, |
| 99 | client.ByInspecting(), |
| 100 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 101 | autorest.ByClosing()) |
| 102 | result = autorest.Response{Response: resp} |
| 103 | |
| 104 | return |
| 105 | } |