aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-sdk-for-go/storage/file.go
blob: 2397587c886d0db5819c762ff8f28a0560a484a8 (plain) (blame)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package storage

import (
    "encoding/xml"
    "fmt"
    "net/http"
    "net/url"
    "strings"
)

// FileServiceClient contains operations for Microsoft Azure File Service.
type FileServiceClient struct {
    client Client
}

// A Share is an entry in ShareListResponse.
type Share struct {
    Name       string          `xml:"Name"`
    Properties ShareProperties `xml:"Properties"`
}

// ShareProperties contains various properties of a share returned from
// various endpoints like ListShares.
type ShareProperties struct {
    LastModified string `xml:"Last-Modified"`
    Etag         string `xml:"Etag"`
    Quota        string `xml:"Quota"`
}

// ShareListResponse contains the response fields from
// ListShares call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn167009.aspx
type ShareListResponse struct {
    XMLName    xml.Name `xml:"EnumerationResults"`
    Xmlns      string   `xml:"xmlns,attr"`
    Prefix     string   `xml:"Prefix"`
    Marker     string   `xml:"Marker"`
    NextMarker string   `xml:"NextMarker"`
    MaxResults int64    `xml:"MaxResults"`
    Shares     []Share  `xml:"Shares>Share"`
}

// ListSharesParameters defines the set of customizable parameters to make a
// List Shares call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn167009.aspx
type ListSharesParameters struct {
    Prefix     string
    Marker     string
    Include    string
    MaxResults uint
    Timeout    uint
}

// ShareHeaders contains various properties of a file and is an entry
// in SetShareProperties
type ShareHeaders struct {
    Quota string `header:"x-ms-share-quota"`
}

func (p ListSharesParameters) getParameters() url.Values {
    out := url.Values{}

    if p.Prefix != "" {
        out.Set("prefix", p.Prefix)
    }
    if p.Marker != "" {
        out.Set("marker", p.Marker)
    }
    if p.Include != "" {
        out.Set("include", p.Include)
    }
    if p.MaxResults != 0 {
        out.Set("maxresults", fmt.Sprintf("%v", p.MaxResults))
    }
    if p.Timeout != 0 {
        out.Set("timeout", fmt.Sprintf("%v", p.Timeout))
    }

    return out
}

// pathForFileShare returns the URL path segment for a File Share resource
func pathForFileShare(name string) string {
    return fmt.Sprintf("/%s", name)
}

// ListShares returns the list of shares in a storage account along with
// pagination token and other response details.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179352.aspx
func (f FileServiceClient) ListShares(params ListSharesParameters) (ShareListResponse, error) {
    q := mergeParams(params.getParameters(), url.Values{"comp": {"list"}})
    uri := f.client.getEndpoint(fileServiceName, "", q)
    headers := f.client.getStandardHeaders()

    var out ShareListResponse
    resp, err := f.client.exec("GET", uri, headers, nil)
    if err != nil {
        return out, err
    }
    defer resp.body.Close()

    err = xmlUnmarshal(resp.body, &out)
    return out, err
}

// CreateShare operation creates a new share under the specified account. If the
// share with the same name already exists, the operation fails.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn167008.aspx
func (f FileServiceClient) CreateShare(name string) error {
    resp, err := f.createShare(name)
    if err != nil {
        return err
    }
    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusCreated})
}

// ShareExists returns true if a share with given name exists
// on the storage account, otherwise returns false.
func (f FileServiceClient) ShareExists(name string) (bool, error) {
    uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), url.Values{"restype": {"share"}})
    headers := f.client.getStandardHeaders()

    resp, err := f.client.exec("HEAD", uri, headers, nil)
    if resp != nil {
        defer resp.body.Close()
        if resp.statusCode == http.StatusOK || resp.statusCode == http.StatusNotFound {
            return resp.statusCode == http.StatusOK, nil
        }
    }
    return false, err
}

// GetShareURL gets the canonical URL to the share with the specified name in the
// specified container. This method does not create a publicly accessible URL if
// the file is private and this method does not check if the file
// exists.
func (f FileServiceClient) GetShareURL(name string) string {
    return f.client.getEndpoint(fileServiceName, pathForFileShare(name), url.Values{})
}

// CreateShareIfNotExists creates a new share under the specified account if
// it does not exist. Returns true if container is newly created or false if
// container already exists.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn167008.aspx
func (f FileServiceClient) CreateShareIfNotExists(name string) (bool, error) {
    resp, err := f.createShare(name)
    if resp != nil {
        defer resp.body.Close()
        if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict {
            return resp.statusCode == http.StatusCreated, nil
        }
    }
    return false, err
}

// CreateShare creates a Azure File Share and returns its response
func (f FileServiceClient) createShare(name string) (*storageResponse, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }
    uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), url.Values{"restype": {"share"}})
    headers := f.client.getStandardHeaders()
    return f.client.exec("PUT", uri, headers, nil)
}

// GetShareProperties provides various information about the specified
// file. See https://msdn.microsoft.com/en-us/library/azure/dn689099.aspx
func (f FileServiceClient) GetShareProperties(name string) (*ShareProperties, error) {
    uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), url.Values{"restype": {"share"}})

    headers := f.client.getStandardHeaders()
    resp, err := f.client.exec("HEAD", uri, headers, nil)
    if err != nil {
        return nil, err
    }
    defer resp.body.Close()

    if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
        return nil, err
    }

    return &ShareProperties{
        LastModified: resp.headers.Get("Last-Modified"),
        Etag:         resp.headers.Get("Etag"),
        Quota:        resp.headers.Get("x-ms-share-quota"),
    }, nil
}

// SetShareProperties replaces the ShareHeaders for the specified file.
//
// Some keys may be converted to Camel-Case before sending. All keys
// are returned in lower case by SetShareProperties. HTTP header names
// are case-insensitive so case munging should not matter to other
// applications either.
//
// See https://msdn.microsoft.com/en-us/library/azure/mt427368.aspx
func (f FileServiceClient) SetShareProperties(name string, shareHeaders ShareHeaders) error {
    params := url.Values{}
    params.Set("restype", "share")
    params.Set("comp", "properties")

    uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), params)
    headers := f.client.getStandardHeaders()

    extraHeaders := headersFromStruct(shareHeaders)

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := f.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusOK})
}

// DeleteShare operation marks the specified share for deletion. The share
// and any files contained within it are later deleted during garbage
// collection.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn689090.aspx
func (f FileServiceClient) DeleteShare(name string) error {
    resp, err := f.deleteShare(name)
    if err != nil {
        return err
    }
    defer resp.body.Close()
    return checkRespCode(resp.statusCode, []int{http.StatusAccepted})
}

// DeleteShareIfExists operation marks the specified share for deletion if it
// exists. The share and any files contained within it are later deleted during
// garbage collection. Returns true if share existed and deleted with this call,
// false otherwise.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn689090.aspx
func (f FileServiceClient) DeleteShareIfExists(name string) (bool, error) {
    resp, err := f.deleteShare(name)
    if resp != nil {
        defer resp.body.Close()
        if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound {
            return resp.statusCode == http.StatusAccepted, nil
        }
    }
    return false, err
}

// deleteShare makes the call to Delete Share operation endpoint and returns
// the response
func (f FileServiceClient) deleteShare(name string) (*storageResponse, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }
    uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), url.Values{"restype": {"share"}})
    return f.client.exec("DELETE", uri, f.client.getStandardHeaders(), nil)
}

// SetShareMetadata replaces the metadata for the specified Share.
//
// Some keys may be converted to Camel-Case before sending. All keys
// are returned in lower case by GetShareMetadata. HTTP header names
// are case-insensitive so case munging should not matter to other
// applications either.
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
func (f FileServiceClient) SetShareMetadata(name string, metadata map[string]string, extraHeaders map[string]string) error {
    params := url.Values{}
    params.Set("restype", "share")
    params.Set("comp", "metadata")

    uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), params)
    headers := f.client.getStandardHeaders()
    for k, v := range metadata {
        headers[userDefinedMetadataHeaderPrefix+k] = v
    }

    for k, v := range extraHeaders {
        headers[k] = v
    }

    resp, err := f.client.exec("PUT", uri, headers, nil)
    if err != nil {
        return err
    }
    defer resp.body.Close()

    return checkRespCode(resp.statusCode, []int{http.StatusOK})
}

// GetShareMetadata returns all user-defined metadata for the specified share.
//
// All metadata keys will be returned in lower case. (HTTP header
// names are case-insensitive.)
//
// See https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
func (f FileServiceClient) GetShareMetadata(name string) (map[string]string, error) {
    params := url.Values{}
    params.Set("restype", "share")
    params.Set("comp", "metadata")

    uri := f.client.getEndpoint(fileServiceName, pathForFileShare(name), params)
    headers := f.client.getStandardHeaders()

    resp, err := f.client.exec("GET", uri, headers, nil)
    if err != nil {
        return nil, err
    }
    defer resp.body.Close()

    if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
        return nil, err
    }

    metadata := make(map[string]string)
    for k, v := range resp.headers {
        // Can't trust CanonicalHeaderKey() to munge case
        // reliably. "_" is allowed in identifiers:
        // https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
        // https://msdn.microsoft.com/library/aa664670(VS.71).aspx
        // http://tools.ietf.org/html/rfc7230#section-3.2
        // ...but "_" is considered invalid by
        // CanonicalMIMEHeaderKey in
        // https://golang.org/src/net/textproto/reader.go?s=14615:14659#L542
        // so k can be "X-Ms-Meta-Foo" or "x-ms-meta-foo_bar".
        k = strings.ToLower(k)
        if len(v) == 0 || !strings.HasPrefix(k, strings.ToLower(userDefinedMetadataHeaderPrefix)) {
            continue
        }
        // metadata["foo"] = content of the last X-Ms-Meta-Foo header
        k = k[len(userDefinedMetadataHeaderPrefix):]
        metadata[k] = v[len(v)-1]
    }
    return metadata, nil
}

//checkForStorageEmulator determines if the client is setup for use with
//Azure Storage Emulator, and returns a relevant error
func (f FileServiceClient) checkForStorageEmulator() error {
    if f.client.accountName == StorageEmulatorAccountName {
        return fmt.Errorf("Error: File service is not currently supported by Azure Storage Emulator")
    }
    return nil
}