| 1 | package queues |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/Azure/go-autorest/autorest" |
| 10 | "github.com/Azure/go-autorest/autorest/azure" |
| 11 | "github.com/Azure/go-autorest/autorest/validation" |
| 12 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 13 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 14 | ) |
| 15 | |
| 16 | // SetMetaData returns the metadata for this Queue |
| 17 | func (client Client) SetMetaData(ctx context.Context, accountName, queueName string, metaData map[string]string) (result autorest.Response, err error) { |
| 18 | if accountName == "" { |
| 19 | return result, validation.NewError("queues.Client", "SetMetaData", "`accountName` cannot be an empty string.") |
| 20 | } |
| 21 | if queueName == "" { |
| 22 | return result, validation.NewError("queues.Client", "SetMetaData", "`queueName` cannot be an empty string.") |
| 23 | } |
| 24 | if strings.ToLower(queueName) != queueName { |
| 25 | return result, validation.NewError("queues.Client", "SetMetaData", "`queueName` must be a lower-cased string.") |
| 26 | } |
| 27 | if err := metadata.Validate(metaData); err != nil { |
| 28 | return result, validation.NewError("queues.Client", "SetMetaData", fmt.Sprintf("`metadata` is not valid: %s.", err)) |
| 29 | } |
| 30 | |
| 31 | req, err := client.SetMetaDataPreparer(ctx, accountName, queueName, metaData) |
| 32 | if err != nil { |
| 33 | err = autorest.NewErrorWithError(err, "queues.Client", "SetMetaData", nil, "Failure preparing request") |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | resp, err := client.SetMetaDataSender(req) |
| 38 | if err != nil { |
| 39 | result = autorest.Response{Response: resp} |
| 40 | err = autorest.NewErrorWithError(err, "queues.Client", "SetMetaData", resp, "Failure sending request") |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | result, err = client.SetMetaDataResponder(resp) |
| 45 | if err != nil { |
| 46 | err = autorest.NewErrorWithError(err, "queues.Client", "SetMetaData", resp, "Failure responding to request") |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | // SetMetaDataPreparer prepares the SetMetaData request. |
| 54 | func (client Client) SetMetaDataPreparer(ctx context.Context, accountName, queueName string, metaData map[string]string) (*http.Request, error) { |
| 55 | pathParameters := map[string]interface{}{ |
| 56 | "queueName": autorest.Encode("path", queueName), |
| 57 | } |
| 58 | |
| 59 | queryParameters := map[string]interface{}{ |
| 60 | "comp": autorest.Encode("path", "metadata"), |
| 61 | } |
| 62 | |
| 63 | headers := map[string]interface{}{ |
| 64 | "x-ms-version": APIVersion, |
| 65 | } |
| 66 | |
| 67 | headers = metadata.SetIntoHeaders(headers, metaData) |
| 68 | |
| 69 | preparer := autorest.CreatePreparer( |
| 70 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 71 | autorest.AsPut(), |
| 72 | autorest.WithBaseURL(endpoints.GetQueueEndpoint(client.BaseURI, accountName)), |
| 73 | autorest.WithPathParameters("/{queueName}", pathParameters), |
| 74 | autorest.WithQueryParameters(queryParameters), |
| 75 | autorest.WithHeaders(headers)) |
| 76 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 77 | } |
| 78 | |
| 79 | // SetMetaDataSender sends the SetMetaData request. The method will close the |
| 80 | // http.Response Body if it receives an error. |
| 81 | func (client Client) SetMetaDataSender(req *http.Request) (*http.Response, error) { |
| 82 | return autorest.SendWithSender(client, req, |
| 83 | azure.DoRetryWithRegistration(client.Client)) |
| 84 | } |
| 85 | |
| 86 | // SetMetaDataResponder handles the response to the SetMetaData request. The method always |
| 87 | // closes the http.Response Body. |
| 88 | func (client Client) SetMetaDataResponder(resp *http.Response) (result autorest.Response, err error) { |
| 89 | err = autorest.Respond( |
| 90 | resp, |
| 91 | client.ByInspecting(), |
| 92 | azure.WithErrorUnlessStatusCode(http.StatusNoContent), |
| 93 | autorest.ByClosing()) |
| 94 | result = autorest.Response{Response: resp} |
| 95 | |
| 96 | return |
| 97 | } |