| 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 | // Create creates the specified Queue within the specified Storage Account |
| 17 | func (client Client) Create(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", "Create", "`accountName` cannot be an empty string.") |
| 20 | } |
| 21 | if queueName == "" { |
| 22 | return result, validation.NewError("queues.Client", "Create", "`queueName` cannot be an empty string.") |
| 23 | } |
| 24 | if strings.ToLower(queueName) != queueName { |
| 25 | return result, validation.NewError("queues.Client", "Create", "`queueName` must be a lower-cased string.") |
| 26 | } |
| 27 | if err := metadata.Validate(metaData); err != nil { |
| 28 | return result, validation.NewError("queues.Client", "Create", fmt.Sprintf("`metadata` is not valid: %s.", err)) |
| 29 | } |
| 30 | |
| 31 | req, err := client.CreatePreparer(ctx, accountName, queueName, metaData) |
| 32 | if err != nil { |
| 33 | err = autorest.NewErrorWithError(err, "queues.Client", "Create", nil, "Failure preparing request") |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | resp, err := client.CreateSender(req) |
| 38 | if err != nil { |
| 39 | result = autorest.Response{Response: resp} |
| 40 | err = autorest.NewErrorWithError(err, "queues.Client", "Create", resp, "Failure sending request") |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | result, err = client.CreateResponder(resp) |
| 45 | if err != nil { |
| 46 | err = autorest.NewErrorWithError(err, "queues.Client", "Create", resp, "Failure responding to request") |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | // CreatePreparer prepares the Create request. |
| 54 | func (client Client) CreatePreparer(ctx context.Context, accountName string, queueName string, metaData map[string]string) (*http.Request, error) { |
| 55 | pathParameters := map[string]interface{}{ |
| 56 | "queueName": autorest.Encode("path", queueName), |
| 57 | } |
| 58 | |
| 59 | headers := map[string]interface{}{ |
| 60 | "x-ms-version": APIVersion, |
| 61 | } |
| 62 | |
| 63 | headers = metadata.SetIntoHeaders(headers, metaData) |
| 64 | |
| 65 | preparer := autorest.CreatePreparer( |
| 66 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 67 | autorest.AsPut(), |
| 68 | autorest.WithBaseURL(endpoints.GetQueueEndpoint(client.BaseURI, accountName)), |
| 69 | autorest.WithPathParameters("/{queueName}", pathParameters), |
| 70 | autorest.WithHeaders(headers)) |
| 71 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 72 | } |
| 73 | |
| 74 | // CreateSender sends the Create request. The method will close the |
| 75 | // http.Response Body if it receives an error. |
| 76 | func (client Client) CreateSender(req *http.Request) (*http.Response, error) { |
| 77 | return autorest.SendWithSender(client, req, |
| 78 | azure.DoRetryWithRegistration(client.Client)) |
| 79 | } |
| 80 | |
| 81 | // CreateResponder handles the response to the Create request. The method always |
| 82 | // closes the http.Response Body. |
| 83 | func (client Client) CreateResponder(resp *http.Response) (result autorest.Response, err error) { |
| 84 | err = autorest.Respond( |
| 85 | resp, |
| 86 | client.ByInspecting(), |
| 87 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 88 | autorest.ByClosing()) |
| 89 | result = autorest.Response{Response: resp} |
| 90 | |
| 91 | return |
| 92 | } |