Debian Codegraph @

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

raw
1 package directories
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "strings"
8
9 "github.com/Azure/go-autorest/autorest"
10 "github.com/Azure/go-autorest/autorest/azure"
11 "github.com/Azure/go-autorest/autorest/validation"
12 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
13 "github.com/tombuildsstuff/giovanni/storage/internal/metadata"
14 )
15
16 // SetMetaData updates user defined metadata for the specified directory
17 func (client Client) SetMetaData(ctx context.Context, accountName, shareName, path string, metaData map[string]string) (result autorest.Response, err error) {
18 if accountName == "" {
19 return result, validation.NewError("directories.Client", "SetMetaData", "`accountName` cannot be an empty string.")
20 }
21 if shareName == "" {
22 return result, validation.NewError("directories.Client", "SetMetaData", "`shareName` cannot be an empty string.")
23 }
24 if strings.ToLower(shareName) != shareName {
25 return result, validation.NewError("directories.Client", "SetMetaData", "`shareName` must be a lower-cased string.")
26 }
27 if path == "" {
28 return result, validation.NewError("directories.Client", "SetMetaData", "`path` cannot be an empty string.")
29 }
30 if err := metadata.Validate(metaData); err != nil {
31 return result, validation.NewError("directories.Client", "SetMetaData", fmt.Sprintf("`metaData` is not valid: %s.", err))
32 }
33
34 req, err := client.SetMetaDataPreparer(ctx, accountName, shareName, path, metaData)
35 if err != nil {
36 err = autorest.NewErrorWithError(err, "directories.Client", "SetMetaData", nil, "Failure preparing request")
37 return
38 }
39
40 resp, err := client.SetMetaDataSender(req)
41 if err != nil {
42 result = autorest.Response{Response: resp}
43 err = autorest.NewErrorWithError(err, "directories.Client", "SetMetaData", resp, "Failure sending request")
44 return
45 }
46
47 result, err = client.SetMetaDataResponder(resp)
48 if err != nil {
49 err = autorest.NewErrorWithError(err, "directories.Client", "SetMetaData", resp, "Failure responding to request")
50 return
51 }
52
53 return
54 }
55
56 // SetMetaDataPreparer prepares the SetMetaData request.
57 func (client Client) SetMetaDataPreparer(ctx context.Context, accountName, shareName, path string, metaData map[string]string) (*http.Request, error) {
58 pathParameters := map[string]interface{}{
59 "shareName": autorest.Encode("path", shareName),
60 "directory": autorest.Encode("path", path),
61 }
62
63 queryParameters := map[string]interface{}{
64 "restype": autorest.Encode("query", "directory"),
65 "comp": autorest.Encode("query", "metadata"),
66 }
67
68 headers := map[string]interface{}{
69 "x-ms-version": APIVersion,
70 }
71
72 headers = metadata.SetIntoHeaders(headers, metaData)
73
74 preparer := autorest.CreatePreparer(
75 autorest.AsContentType("application/xml; charset=utf-8"),
76 autorest.AsPut(),
77 autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
78 autorest.WithPathParameters("/{shareName}/{directory}", pathParameters),
79 autorest.WithQueryParameters(queryParameters),
80 autorest.WithHeaders(headers))
81 return preparer.Prepare((&http.Request{}).WithContext(ctx))
82 }
83
84 // SetMetaDataSender sends the SetMetaData request. The method will close the
85 // http.Response Body if it receives an error.
86 func (client Client) SetMetaDataSender(req *http.Request) (*http.Response, error) {
87 return autorest.SendWithSender(client, req,
88 azure.DoRetryWithRegistration(client.Client))
89 }
90
91 // SetMetaDataResponder handles the response to the SetMetaData request. The method always
92 // closes the http.Response Body.
93 func (client Client) SetMetaDataResponder(resp *http.Response) (result autorest.Response, err error) {
94 err = autorest.Respond(
95 resp,
96 client.ByInspecting(),
97 azure.WithErrorUnlessStatusCode(http.StatusOK),
98 autorest.ByClosing())
99 result = autorest.Response{Response: resp}
100
101 return
102 }