aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-storage-go/fileserviceclient.go
blob: d68bd7f64e98b8ee9ec1d0f34460586775128aef (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package storage

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

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

// 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
}

// 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"`
}

type compType string

const (
    compNone       compType = ""
    compList       compType = "list"
    compMetadata   compType = "metadata"
    compProperties compType = "properties"
    compRangeList  compType = "rangelist"
)

func (ct compType) String() string {
    return string(ct)
}

type resourceType string

const (
    resourceDirectory resourceType = "directory"
    resourceFile      resourceType = ""
    resourceShare     resourceType = "share"
)

func (rt resourceType) String() string {
    return string(rt)
}

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
}

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

    if p.Marker != "" {
        out.Set("marker", p.Marker)
    }
    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
}

// returns url.Values for the specified types
func getURLInitValues(comp compType, res resourceType) url.Values {
    values := url.Values{}
    if comp != compNone {
        values.Set("comp", comp.String())
    }
    if res != resourceFile {
        values.Set("restype", res.String())
    }
    return values
}

// GetShareReference returns a Share object for the specified share name.
func (f FileServiceClient) GetShareReference(name string) Share {
    return Share{
        fsc:  &f,
        Name: name,
        Properties: ShareProperties{
            Quota: -1,
        },
    }
}

// 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"}})

    var out ShareListResponse
    resp, err := f.listContent("", q, nil)
    if err != nil {
        return nil, err
    }
    defer resp.body.Close()
    err = xmlUnmarshal(resp.body, &out)

    // assign our client to the newly created Share objects
    for i := range out.Shares {
        out.Shares[i].fsc = &f
    }
    return &out, err
}

// GetServiceProperties gets the properties of your storage account's file service.
// File service does not support logging
// See: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-file-service-properties
func (f *FileServiceClient) GetServiceProperties() (*ServiceProperties, error) {
    return f.client.getServiceProperties(fileServiceName, f.auth)
}

// SetServiceProperties sets the properties of your storage account's file service.
// File service does not support logging
// See: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-file-service-properties
func (f *FileServiceClient) SetServiceProperties(props ServiceProperties) error {
    return f.client.setServiceProperties(props, fileServiceName, f.auth)
}

// retrieves directory or share content
func (f FileServiceClient) listContent(path string, params url.Values, extraHeaders map[string]string) (*storageResponse, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }

    uri := f.client.getEndpoint(fileServiceName, path, params)
    extraHeaders = f.client.protectUserAgent(extraHeaders)
    headers := mergeHeaders(f.client.getStandardHeaders(), extraHeaders)

    resp, err := f.client.exec(http.MethodGet, uri, headers, nil, f.auth)
    if err != nil {
        return nil, err
    }

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

    return resp, nil
}

// returns true if the specified resource exists
func (f FileServiceClient) resourceExists(path string, res resourceType) (bool, http.Header, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return false, nil, err
    }

    uri := f.client.getEndpoint(fileServiceName, path, getURLInitValues(compNone, res))
    headers := f.client.getStandardHeaders()

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

// creates a resource depending on the specified resource type
func (f FileServiceClient) createResource(path string, res resourceType, urlParams url.Values, extraHeaders map[string]string, expectedResponseCodes []int) (http.Header, error) {
    resp, err := f.createResourceNoClose(path, res, urlParams, extraHeaders)
    if err != nil {
        return nil, err
    }
    defer readAndCloseBody(resp.body)
    return resp.headers, checkRespCode(resp.statusCode, expectedResponseCodes)
}

// creates a resource depending on the specified resource type, doesn't close the response body
func (f FileServiceClient) createResourceNoClose(path string, res resourceType, urlParams url.Values, extraHeaders map[string]string) (*storageResponse, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }

    values := getURLInitValues(compNone, res)
    combinedParams := mergeParams(values, urlParams)
    uri := f.client.getEndpoint(fileServiceName, path, combinedParams)
    extraHeaders = f.client.protectUserAgent(extraHeaders)
    headers := mergeHeaders(f.client.getStandardHeaders(), extraHeaders)

    return f.client.exec(http.MethodPut, uri, headers, nil, f.auth)
}

// returns HTTP header data for the specified directory or share
func (f FileServiceClient) getResourceHeaders(path string, comp compType, res resourceType, verb string) (http.Header, error) {
    resp, err := f.getResourceNoClose(path, comp, res, verb, nil)
    if err != nil {
        return nil, err
    }
    defer readAndCloseBody(resp.body)

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

    return resp.headers, nil
}

// gets the specified resource, doesn't close the response body
func (f FileServiceClient) getResourceNoClose(path string, comp compType, res resourceType, verb string, extraHeaders map[string]string) (*storageResponse, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }

    params := getURLInitValues(comp, res)
    uri := f.client.getEndpoint(fileServiceName, path, params)
    extraHeaders = f.client.protectUserAgent(extraHeaders)
    headers := mergeHeaders(f.client.getStandardHeaders(), extraHeaders)

    return f.client.exec(verb, uri, headers, nil, f.auth)
}

// deletes the resource and returns the response
func (f FileServiceClient) deleteResource(path string, res resourceType) error {
    resp, err := f.deleteResourceNoClose(path, res)
    if err != nil {
        return err
    }
    defer readAndCloseBody(resp.body)
    return checkRespCode(resp.statusCode, []int{http.StatusAccepted})
}

// deletes the resource and returns the response, doesn't close the response body
func (f FileServiceClient) deleteResourceNoClose(path string, res resourceType) (*storageResponse, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }

    values := getURLInitValues(compNone, res)
    uri := f.client.getEndpoint(fileServiceName, path, values)
    return f.client.exec(http.MethodDelete, uri, f.client.getStandardHeaders(), nil, f.auth)
}

// merges metadata into extraHeaders and returns extraHeaders
func mergeMDIntoExtraHeaders(metadata, extraHeaders map[string]string) map[string]string {
    if metadata == nil && extraHeaders == nil {
        return nil
    }
    if extraHeaders == nil {
        extraHeaders = make(map[string]string)
    }
    for k, v := range metadata {
        extraHeaders[userDefinedMetadataHeaderPrefix+k] = v
    }
    return extraHeaders
}

// merges extraHeaders into headers and returns headers
func mergeHeaders(headers, extraHeaders map[string]string) map[string]string {
    for k, v := range extraHeaders {
        headers[k] = v
    }
    return headers
}

// sets extra header data for the specified resource
func (f FileServiceClient) setResourceHeaders(path string, comp compType, res resourceType, extraHeaders map[string]string) (http.Header, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }

    params := getURLInitValues(comp, res)
    uri := f.client.getEndpoint(fileServiceName, path, params)
    extraHeaders = f.client.protectUserAgent(extraHeaders)
    headers := mergeHeaders(f.client.getStandardHeaders(), extraHeaders)

    resp, err := f.client.exec(http.MethodPut, uri, headers, nil, f.auth)
    if err != nil {
        return nil, err
    }
    defer readAndCloseBody(resp.body)

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

// gets metadata for the specified resource
func (f FileServiceClient) getMetadata(path string, res resourceType) (map[string]string, error) {
    if err := f.checkForStorageEmulator(); err != nil {
        return nil, err
    }

    headers, err := f.getResourceHeaders(path, compMetadata, res, http.MethodGet)
    if err != nil {
        return nil, err
    }

    return getMetadataFromHeaders(headers), nil
}

// returns a map of custom metadata values from the specified HTTP header
func getMetadataFromHeaders(header http.Header) map[string]string {
    metadata := make(map[string]string)
    for k, v := range header {
        // 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]
    }

    if len(metadata) == 0 {
        return nil
    }

    return metadata
}

//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
}