Debian Codegraph @

storage/2018-11-09/blob/containers/create.go

raw
1 package containers
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "github.com/Azure/go-autorest/autorest"
9 "github.com/Azure/go-autorest/autorest/azure"
10 "github.com/Azure/go-autorest/autorest/validation"
11 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
12 "github.com/tombuildsstuff/giovanni/storage/internal/metadata"
13 )
14
15 type CreateInput struct {
16 // Specifies whether data in the container may be accessed publicly and the level of access
17 AccessLevel AccessLevel
18
19 // A name-value pair to associate with the container as metadata.
20 MetaData map[string]string
21 }
22
23 type CreateResponse struct {
24 autorest.Response
25 Error *ErrorResponse `xml:"Error"`
26 }
27
28 // Create creates a new container under the specified account.
29 // If the container with the same name already exists, the operation fails.
30 func (client Client) Create(ctx context.Context, accountName, containerName string, input CreateInput) (result CreateResponse, err error) {
31 if accountName == "" {
32 return result, validation.NewError("containers.Client", "Create", "`accountName` cannot be an empty string.")
33 }
34 if containerName == "" {
35 return result, validation.NewError("containers.Client", "Create", "`containerName` cannot be an empty string.")
36 }
37 if err := metadata.Validate(input.MetaData); err != nil {
38 return result, validation.NewError("containers.Client", "Create", fmt.Sprintf("`input.MetaData` is not valid: %s.", err))
39 }
40
41 req, err := client.CreatePreparer(ctx, accountName, containerName, input)
42 if err != nil {
43 err = autorest.NewErrorWithError(err, "containers.Client", "Create", nil, "Failure preparing request")
44 return
45 }
46
47 resp, err := client.CreateSender(req)
48 if err != nil {
49 result.Response = autorest.Response{Response: resp}
50 err = autorest.NewErrorWithError(err, "containers.Client", "Create", resp, "Failure sending request")
51 return
52 }
53
54 result, err = client.CreateResponder(resp)
55 if err != nil {
56 err = autorest.NewErrorWithError(err, "containers.Client", "Create", resp, "Failure responding to request")
57 return
58 }
59
60 return
61 }
62
63 // CreatePreparer prepares the Create request.
64 func (client Client) CreatePreparer(ctx context.Context, accountName string, containerName string, input CreateInput) (*http.Request, error) {
65 pathParameters := map[string]interface{}{
66 "containerName": autorest.Encode("path", containerName),
67 }
68
69 queryParameters := map[string]interface{}{
70 "restype": autorest.Encode("path", "container"),
71 }
72
73 headers := map[string]interface{}{
74 "x-ms-version": APIVersion,
75 }
76
77 headers = client.setAccessLevelIntoHeaders(headers, input.AccessLevel)
78 headers = metadata.SetIntoHeaders(headers, input.MetaData)
79
80 preparer := autorest.CreatePreparer(
81 autorest.AsContentType("application/xml; charset=utf-8"),
82 autorest.AsPut(),
83 autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)),
84 autorest.WithPathParameters("/{containerName}", pathParameters),
85 autorest.WithQueryParameters(queryParameters),
86 autorest.WithHeaders(headers))
87 return preparer.Prepare((&http.Request{}).WithContext(ctx))
88 }
89
90 // CreateSender sends the Create request. The method will close the
91 // http.Response Body if it receives an error.
92 func (client Client) CreateSender(req *http.Request) (*http.Response, error) {
93 return autorest.SendWithSender(client, req,
94 azure.DoRetryWithRegistration(client.Client))
95 }
96
97 // CreateResponder handles the response to the Create request. The method always
98 // closes the http.Response Body.
99 func (client Client) CreateResponder(resp *http.Response) (result CreateResponse, err error) {
100 successfulStatusCodes := []int{
101 http.StatusCreated,
102 }
103 if autorest.ResponseHasStatusCode(resp, successfulStatusCodes...) {
104 // when successful there's no response
105 err = autorest.Respond(
106 resp,
107 client.ByInspecting(),
108 azure.WithErrorUnlessStatusCode(successfulStatusCodes...),
109 autorest.ByClosing())
110 result.Response = autorest.Response{Response: resp}
111 } else {
112 // however when there's an error the error's in the response
113 err = autorest.Respond(
114 resp,
115 client.ByInspecting(),
116 azure.WithErrorUnlessStatusCode(successfulStatusCodes...),
117 autorest.ByUnmarshallingXML(&result),
118 autorest.ByClosing())
119 result.Response = autorest.Response{Response: resp}
120 }
121
122 return
123 }