-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcreate.go
119 lines (95 loc) · 2.89 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
116
117
118
119
package shares
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/tombuildsstuff/giovanni/storage/internal/metadata"
)
type AccessTier string
const (
TransactionOptimizedAccessTier AccessTier = "TransactionOptimized"
HotAccessTier AccessTier = "Hot"
CoolAccessTier AccessTier = "Cool"
PremiumAccessTier AccessTier = "Premium"
)
type CreateInput struct {
// Specifies the maximum size of the share, in gigabytes.
// Must be greater than 0, and less than or equal to 5TB (5120).
QuotaInGB int
// Specifies the enabled protocols on the share. If not specified, the default is SMB.
EnabledProtocol ShareProtocol
MetaData map[string]string
// Specifies the access tier of the share.
AccessTier *AccessTier
}
type CreateResponse struct {
HttpResponse *client.Response
}
// Create creates the specified Storage Share within the specified Storage Account
func (c Client) Create(ctx context.Context, shareName string, input CreateInput) (resp CreateResponse, 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 input.QuotaInGB <= 0 || input.QuotaInGB > 102400 {
return resp, fmt.Errorf("`input.QuotaInGB` must be greater than 0, and less than/equal to 100TB (102400 GB)")
}
if err = metadata.Validate(input.MetaData); err != nil {
return resp, fmt.Errorf("`input.MetaData` is not valid: %s", err)
}
opts := client.RequestOptions{
ContentType: "application/xml; charset=utf-8",
ExpectedStatusCodes: []int{
http.StatusCreated,
},
HttpMethod: http.MethodPut,
OptionsObject: CreateOptions{
input: input,
},
Path: fmt.Sprintf("/%s", shareName),
}
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 CreateInput
}
func (c CreateOptions) ToHeaders() *client.Headers {
headers := &client.Headers{}
if len(c.input.MetaData) > 0 {
headers.Merge(metadata.SetMetaDataHeaders(c.input.MetaData))
}
protocol := SMB
if c.input.EnabledProtocol != "" {
protocol = c.input.EnabledProtocol
}
headers.Append("x-ms-enabled-protocols", string(protocol))
if c.input.AccessTier != nil {
headers.Append("x-ms-access-tier", string(*c.input.AccessTier))
}
headers.Append("x-ms-share-quota", strconv.Itoa(c.input.QuotaInGB))
return headers
}
func (c CreateOptions) ToOData() *odata.Query {
return nil
}
func (c CreateOptions) ToQuery() *client.QueryParams {
out := &client.QueryParams{}
out.Append("restype", "share")
return out
}