| 1 | package shares |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/Azure/go-autorest/autorest" |
| 11 | "github.com/Azure/go-autorest/autorest/azure" |
| 12 | "github.com/Azure/go-autorest/autorest/validation" |
| 13 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 14 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 15 | ) |
| 16 | |
| 17 | type GetPropertiesResult struct { |
| 18 | autorest.Response |
| 19 | |
| 20 | MetaData map[string]string |
| 21 | ShareQuota int |
| 22 | } |
| 23 | |
| 24 | // GetProperties returns the properties about the specified Storage Share |
| 25 | func (client Client) GetProperties(ctx context.Context, accountName, shareName string) (result GetPropertiesResult, err error) { |
| 26 | if accountName == "" { |
| 27 | return result, validation.NewError("shares.Client", "GetProperties", "`accountName` cannot be an empty string.") |
| 28 | } |
| 29 | if shareName == "" { |
| 30 | return result, validation.NewError("shares.Client", "GetProperties", "`shareName` cannot be an empty string.") |
| 31 | } |
| 32 | if strings.ToLower(shareName) != shareName { |
| 33 | return result, validation.NewError("shares.Client", "GetProperties", "`shareName` must be a lower-cased string.") |
| 34 | } |
| 35 | |
| 36 | req, err := client.GetPropertiesPreparer(ctx, accountName, shareName) |
| 37 | if err != nil { |
| 38 | err = autorest.NewErrorWithError(err, "shares.Client", "GetProperties", nil, "Failure preparing request") |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | resp, err := client.GetPropertiesSender(req) |
| 43 | if err != nil { |
| 44 | result.Response = autorest.Response{Response: resp} |
| 45 | err = autorest.NewErrorWithError(err, "shares.Client", "GetProperties", resp, "Failure sending request") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | result, err = client.GetPropertiesResponder(resp) |
| 50 | if err != nil { |
| 51 | err = autorest.NewErrorWithError(err, "shares.Client", "GetProperties", resp, "Failure responding to request") |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | // GetPropertiesPreparer prepares the GetProperties request. |
| 59 | func (client Client) GetPropertiesPreparer(ctx context.Context, accountName, shareName string) (*http.Request, error) { |
| 60 | pathParameters := map[string]interface{}{ |
| 61 | "shareName": autorest.Encode("path", shareName), |
| 62 | } |
| 63 | |
| 64 | queryParameters := map[string]interface{}{ |
| 65 | "restype": autorest.Encode("query", "share"), |
| 66 | } |
| 67 | |
| 68 | headers := map[string]interface{}{ |
| 69 | "x-ms-version": APIVersion, |
| 70 | } |
| 71 | |
| 72 | preparer := autorest.CreatePreparer( |
| 73 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 74 | autorest.AsGet(), |
| 75 | autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 76 | autorest.WithPathParameters("/{shareName}", pathParameters), |
| 77 | autorest.WithQueryParameters(queryParameters), |
| 78 | autorest.WithHeaders(headers)) |
| 79 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 80 | } |
| 81 | |
| 82 | // GetPropertiesSender sends the GetProperties request. The method will close the |
| 83 | // http.Response Body if it receives an error. |
| 84 | func (client Client) GetPropertiesSender(req *http.Request) (*http.Response, error) { |
| 85 | return autorest.SendWithSender(client, req, |
| 86 | azure.DoRetryWithRegistration(client.Client)) |
| 87 | } |
| 88 | |
| 89 | // GetPropertiesResponder handles the response to the GetProperties request. The method always |
| 90 | // closes the http.Response Body. |
| 91 | func (client Client) GetPropertiesResponder(resp *http.Response) (result GetPropertiesResult, err error) { |
| 92 | if resp.Header != nil { |
| 93 | result.MetaData = metadata.ParseFromHeaders(resp.Header) |
| 94 | |
| 95 | quotaRaw := resp.Header.Get("x-ms-share-quota") |
| 96 | if quotaRaw != "" { |
| 97 | quota, e := strconv.Atoi(quotaRaw) |
| 98 | if e != nil { |
| 99 | return result, fmt.Errorf("Error converting %q to an integer: %s", quotaRaw, err) |
| 100 | } |
| 101 | result.ShareQuota = quota |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | err = autorest.Respond( |
| 106 | resp, |
| 107 | client.ByInspecting(), |
| 108 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 109 | autorest.ByClosing()) |
| 110 | result.Response = autorest.Response{Response: resp} |
| 111 | |
| 112 | return |
| 113 | } |