-
Notifications
You must be signed in to change notification settings - Fork 21
/
create.go
115 lines (92 loc) · 3.08 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package directories
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/tombuildsstuff/giovanni/storage/internal/metadata"
)
type CreateDirectoryInput struct {
// The time at which this file was created at - if omitted, this'll be set to "now"
// This maps to the `x-ms-file-creation-time` field.
// ... Yes I know it says File not Directory, I didn't design the API.
CreatedAt *time.Time
// The time at which this file was last modified - if omitted, this'll be set to "now"
// This maps to the `x-ms-file-last-write-time` field.
// ... Yes I know it says File not Directory, I didn't design the API.
LastModified *time.Time
// MetaData is a mapping of key value pairs which should be assigned to this directory
MetaData map[string]string
}
type CreateDirectoryResponse struct {
HttpResponse *client.Response
}
// Create creates a new directory under the specified share or parent directory.
func (c Client) Create(ctx context.Context, shareName, path string, input CreateDirectoryInput) (resp CreateDirectoryResponse, err error) {
if shareName == "" {
return resp, fmt.Errorf("`shareName` cannot be an empty string")
}
if strings.ToLower(shareName) != shareName {
return resp, fmt.Errorf("`shareName` must be a lower-cased string")
}
if err = metadata.Validate(input.MetaData); err != nil {
return resp, fmt.Errorf("`input.MetaData` is not valid: %s", err)
}
if path == "" {
return resp, fmt.Errorf("`path` cannot be an empty string")
}
opts := client.RequestOptions{
ContentType: "application/xml; charset=utf-8",
ExpectedStatusCodes: []int{
http.StatusCreated,
},
HttpMethod: http.MethodPut,
OptionsObject: CreateOptions{
input: input,
},
Path: fmt.Sprintf("/%s/%s", shareName, path),
}
req, err := c.Client.NewRequest(ctx, opts)
if err != nil {
err = fmt.Errorf("building request: %+v", err)
return
}
resp.HttpResponse, err = req.Execute(ctx)
if err != nil {
err = fmt.Errorf("executing request: %+v", err)
return
}
return
}
type CreateOptions struct {
input CreateDirectoryInput
}
func (c CreateOptions) ToHeaders() *client.Headers {
headers := &client.Headers{}
if len(c.input.MetaData) > 0 {
headers.Merge(metadata.SetMetaDataHeaders(c.input.MetaData))
}
var coalesceDate = func(input *time.Time, defaultVal string) string {
if input == nil {
return defaultVal
}
return input.Format(time.RFC1123)
}
// ... Yes I know these say File not Directory, I didn't design the API.
headers.Append("x-ms-file-permission", "inherit") // TODO: expose this in future
headers.Append("x-ms-file-attributes", "None") // TODO: expose this in future
headers.Append("x-ms-file-creation-time", coalesceDate(c.input.CreatedAt, "now"))
headers.Append("x-ms-file-last-write-time", coalesceDate(c.input.LastModified, "now"))
return headers
}
func (c CreateOptions) ToOData() *odata.Query {
return nil
}
func (c CreateOptions) ToQuery() *client.QueryParams {
out := &client.QueryParams{}
out.Append("restype", "directory")
return out
}