| 1 | package shares |
| 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 | type CreateInput struct { |
| 17 | // Specifies the maximum size of the share, in gigabytes. |
| 18 | // Must be greater than 0, and less than or equal to 5TB (5120). |
| 19 | QuotaInGB int |
| 20 | |
| 21 | MetaData map[string]string |
| 22 | } |
| 23 | |
| 24 | // Create creates the specified Storage Share within the specified Storage Account |
| 25 | func (client Client) Create(ctx context.Context, accountName, shareName string, input CreateInput) (result autorest.Response, err error) { |
| 26 | if accountName == "" { |
| 27 | return result, validation.NewError("shares.Client", "Create", "`accountName` cannot be an empty string.") |
| 28 | } |
| 29 | if shareName == "" { |
| 30 | return result, validation.NewError("shares.Client", "Create", "`shareName` cannot be an empty string.") |
| 31 | } |
| 32 | if strings.ToLower(shareName) != shareName { |
| 33 | return result, validation.NewError("shares.Client", "Create", "`shareName` must be a lower-cased string.") |
| 34 | } |
| 35 | if input.QuotaInGB <= 0 || input.QuotaInGB > 102400 { |
| 36 | return result, validation.NewError("shares.Client", "Create", "`input.QuotaInGB` must be greater than 0, and less than/equal to 100TB (102400 GB)") |
| 37 | } |
| 38 | if err := metadata.Validate(input.MetaData); err != nil { |
| 39 | return result, validation.NewError("shares.Client", "Create", fmt.Sprintf("`input.MetaData` is not valid: %s.", err)) |
| 40 | } |
| 41 | |
| 42 | req, err := client.CreatePreparer(ctx, accountName, shareName, input) |
| 43 | if err != nil { |
| 44 | err = autorest.NewErrorWithError(err, "shares.Client", "Create", nil, "Failure preparing request") |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | resp, err := client.CreateSender(req) |
| 49 | if err != nil { |
| 50 | result = autorest.Response{Response: resp} |
| 51 | err = autorest.NewErrorWithError(err, "shares.Client", "Create", resp, "Failure sending request") |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | result, err = client.CreateResponder(resp) |
| 56 | if err != nil { |
| 57 | err = autorest.NewErrorWithError(err, "shares.Client", "Create", resp, "Failure responding to request") |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | // CreatePreparer prepares the Create request. |
| 65 | func (client Client) CreatePreparer(ctx context.Context, accountName, shareName string, input CreateInput) (*http.Request, error) { |
| 66 | pathParameters := map[string]interface{}{ |
| 67 | "shareName": autorest.Encode("path", shareName), |
| 68 | } |
| 69 | |
| 70 | queryParameters := map[string]interface{}{ |
| 71 | "restype": autorest.Encode("path", "share"), |
| 72 | } |
| 73 | |
| 74 | headers := map[string]interface{}{ |
| 75 | "x-ms-version": APIVersion, |
| 76 | "x-ms-share-quota": input.QuotaInGB, |
| 77 | } |
| 78 | |
| 79 | headers = metadata.SetIntoHeaders(headers, input.MetaData) |
| 80 | |
| 81 | preparer := autorest.CreatePreparer( |
| 82 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 83 | autorest.AsPut(), |
| 84 | autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 85 | autorest.WithPathParameters("/{shareName}", pathParameters), |
| 86 | autorest.WithQueryParameters(queryParameters), |
| 87 | autorest.WithHeaders(headers)) |
| 88 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 89 | } |
| 90 | |
| 91 | // CreateSender sends the Create request. The method will close the |
| 92 | // http.Response Body if it receives an error. |
| 93 | func (client Client) CreateSender(req *http.Request) (*http.Response, error) { |
| 94 | return autorest.SendWithSender(client, req, |
| 95 | azure.DoRetryWithRegistration(client.Client)) |
| 96 | } |
| 97 | |
| 98 | // CreateResponder handles the response to the Create request. The method always |
| 99 | // closes the http.Response Body. |
| 100 | func (client Client) CreateResponder(resp *http.Response) (result autorest.Response, err error) { |
| 101 | err = autorest.Respond( |
| 102 | resp, |
| 103 | client.ByInspecting(), |
| 104 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 105 | autorest.ByClosing()) |
| 106 | result = autorest.Response{Response: resp} |
| 107 | |
| 108 | return |
| 109 | } |