Debian Codegraph @

storage/2018-03-28/file/directories/create.go

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