Debian Codegraph @

storage/2020-08-04/file/directories/create.go

raw
1 package directories
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "strings"
8 "time"
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 CreateDirectoryInput struct {
18 // The time at which this file was created at - if omitted, this'll be set to "now"
19 // This maps to the `x-ms-file-creation-time` field.
20 // ... Yes I know it says File not Directory, I didn't design the API.
21 CreatedAt *time.Time
22
23 // The time at which this file was last modified - if omitted, this'll be set to "now"
24 // This maps to the `x-ms-file-last-write-time` field.
25 // ... Yes I know it says File not Directory, I didn't design the API.
26 LastModified *time.Time
27
28 // MetaData is a mapping of key value pairs which should be assigned to this directory
29 MetaData map[string]string
30 }
31
32 // Create creates a new directory under the specified share or parent directory.
33 func (client Client) Create(ctx context.Context, accountName, shareName, path string, input CreateDirectoryInput) (result autorest.Response, err error) {
34 if accountName == "" {
35 return result, validation.NewError("directories.Client", "Create", "`accountName` cannot be an empty string.")
36 }
37 if shareName == "" {
38 return result, validation.NewError("directories.Client", "Create", "`shareName` cannot be an empty string.")
39 }
40 if strings.ToLower(shareName) != shareName {
41 return result, validation.NewError("directories.Client", "Create", "`shareName` must be a lower-cased string.")
42 }
43 if path == "" {
44 return result, validation.NewError("directories.Client", "Create", "`path` cannot be an empty string.")
45 }
46 if err := metadata.Validate(input.MetaData); err != nil {
47 return result, validation.NewError("directories.Client", "Create", fmt.Sprintf("`metadata` is not valid: %s.", err))
48 }
49
50 req, err := client.CreatePreparer(ctx, accountName, shareName, path, input)
51 if err != nil {
52 err = autorest.NewErrorWithError(err, "directories.Client", "Create", nil, "Failure preparing request")
53 return
54 }
55
56 resp, err := client.CreateSender(req)
57 if err != nil {
58 result = autorest.Response{Response: resp}
59 err = autorest.NewErrorWithError(err, "directories.Client", "Create", resp, "Failure sending request")
60 return
61 }
62
63 result, err = client.CreateResponder(resp)
64 if err != nil {
65 err = autorest.NewErrorWithError(err, "directories.Client", "Create", resp, "Failure responding to request")
66 return
67 }
68
69 return
70 }
71
72 // CreatePreparer prepares the Create request.
73 func (client Client) CreatePreparer(ctx context.Context, accountName, shareName, path string, input CreateDirectoryInput) (*http.Request, error) {
74 pathParameters := map[string]interface{}{
75 "shareName": autorest.Encode("path", shareName),
76 "directory": autorest.Encode("path", path),
77 }
78
79 queryParameters := map[string]interface{}{
80 "restype": autorest.Encode("query", "directory"),
81 }
82
83 var coalesceDate = func(input *time.Time, defaultVal string) string {
84 if input == nil {
85 return defaultVal
86 }
87
88 return input.Format(time.RFC1123)
89 }
90
91 headers := map[string]interface{}{
92 "x-ms-version": APIVersion,
93
94 // ... Yes I know these say File not Directory, I didn't design the API.
95 "x-ms-file-permission": "inherit", // TODO: expose this in future
96 "x-ms-file-attributes": "None", // TODO: expose this in future
97 "x-ms-file-creation-time": coalesceDate(input.CreatedAt, "now"),
98 "x-ms-file-last-write-time": coalesceDate(input.LastModified, "now"),
99 }
100
101 headers = metadata.SetIntoHeaders(headers, input.MetaData)
102
103 preparer := autorest.CreatePreparer(
104 autorest.AsContentType("application/xml; charset=utf-8"),
105 autorest.AsPut(),
106 autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
107 autorest.WithPathParameters("/{shareName}/{directory}", pathParameters),
108 autorest.WithQueryParameters(queryParameters),
109 autorest.WithHeaders(headers))
110 return preparer.Prepare((&http.Request{}).WithContext(ctx))
111 }
112
113 // CreateSender sends the Create request. The method will close the
114 // http.Response Body if it receives an error.
115 func (client Client) CreateSender(req *http.Request) (*http.Response, error) {
116 return autorest.SendWithSender(client, req,
117 azure.DoRetryWithRegistration(client.Client))
118 }
119
120 // CreateResponder handles the response to the Create request. The method always
121 // closes the http.Response Body.
122 func (client Client) CreateResponder(resp *http.Response) (result autorest.Response, err error) {
123 err = autorest.Respond(
124 resp,
125 client.ByInspecting(),
126 azure.WithErrorUnlessStatusCode(http.StatusCreated),
127 autorest.ByClosing())
128 result = autorest.Response{Response: resp}
129
130 return
131 }