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