| 1 | package tables |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | |
| 7 | "github.com/Azure/go-autorest/autorest" |
| 8 | "github.com/Azure/go-autorest/autorest/azure" |
| 9 | "github.com/Azure/go-autorest/autorest/validation" |
| 10 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 11 | ) |
| 12 | |
| 13 | type createTableRequest struct { |
| 14 | TableName string `json:"TableName"` |
| 15 | } |
| 16 | |
| 17 | // Create creates a new table in the storage account. |
| 18 | func (client Client) Create(ctx context.Context, accountName, tableName string) (result autorest.Response, err error) { |
| 19 | if accountName == "" { |
| 20 | return result, validation.NewError("tables.Client", "Create", "`accountName` cannot be an empty string.") |
| 21 | } |
| 22 | if tableName == "" { |
| 23 | return result, validation.NewError("tables.Client", "Create", "`tableName` cannot be an empty string.") |
| 24 | } |
| 25 | |
| 26 | req, err := client.CreatePreparer(ctx, accountName, tableName) |
| 27 | if err != nil { |
| 28 | err = autorest.NewErrorWithError(err, "tables.Client", "Create", nil, "Failure preparing request") |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | resp, err := client.CreateSender(req) |
| 33 | if err != nil { |
| 34 | result = autorest.Response{Response: resp} |
| 35 | err = autorest.NewErrorWithError(err, "tables.Client", "Create", resp, "Failure sending request") |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | result, err = client.CreateResponder(resp) |
| 40 | if err != nil { |
| 41 | err = autorest.NewErrorWithError(err, "tables.Client", "Create", resp, "Failure responding to request") |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | // CreatePreparer prepares the Create request. |
| 49 | func (client Client) CreatePreparer(ctx context.Context, accountName, tableName string) (*http.Request, error) { |
| 50 | headers := map[string]interface{}{ |
| 51 | "x-ms-version": APIVersion, |
| 52 | // NOTE: we could support returning metadata here, but it doesn't appear to be directly useful |
| 53 | // vs making a request using the Get methods as-necessary? |
| 54 | "Accept": "application/json;odata=nometadata", |
| 55 | "Prefer": "return-no-content", |
| 56 | } |
| 57 | |
| 58 | body := createTableRequest{ |
| 59 | TableName: tableName, |
| 60 | } |
| 61 | |
| 62 | preparer := autorest.CreatePreparer( |
| 63 | autorest.AsContentType("application/json"), |
| 64 | autorest.AsPost(), |
| 65 | autorest.WithBaseURL(endpoints.GetTableEndpoint(client.BaseURI, accountName)), |
| 66 | autorest.WithPath("/Tables"), |
| 67 | autorest.WithJSON(body), |
| 68 | autorest.WithHeaders(headers)) |
| 69 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 70 | } |
| 71 | |
| 72 | // CreateSender sends the Create request. The method will close the |
| 73 | // http.Response Body if it receives an error. |
| 74 | func (client Client) CreateSender(req *http.Request) (*http.Response, error) { |
| 75 | return autorest.SendWithSender(client, req, |
| 76 | azure.DoRetryWithRegistration(client.Client)) |
| 77 | } |
| 78 | |
| 79 | // CreateResponder handles the response to the Create request. The method always |
| 80 | // closes the http.Response Body. |
| 81 | func (client Client) CreateResponder(resp *http.Response) (result autorest.Response, err error) { |
| 82 | err = autorest.Respond( |
| 83 | resp, |
| 84 | client.ByInspecting(), |
| 85 | azure.WithErrorUnlessStatusCode(http.StatusNoContent), |
| 86 | autorest.ByClosing()) |
| 87 | result = autorest.Response{Response: resp} |
| 88 | |
| 89 | return |
| 90 | } |