Debian Codegraph @

storage/2018-11-09/blob/blobs/metadata_set.go

raw
1 package blobs
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 type SetMetaDataInput struct {
17 // The ID of the Lease
18 // This must be specified if a Lease is present on the Blob, else a 403 is returned
19 LeaseID *string
20
21 // Any metadata which should be added to this blob
22 MetaData map[string]string
23 }
24
25 // SetMetaData marks the specified blob or snapshot for deletion. The blob is later deleted during garbage collection.
26 func (client Client) SetMetaData(ctx context.Context, accountName, containerName, blobName string, input SetMetaDataInput) (result autorest.Response, err error) {
27 if accountName == "" {
28 return result, validation.NewError("blobs.Client", "GetProperties", "`accountName` cannot be an empty string.")
29 }
30 if containerName == "" {
31 return result, validation.NewError("blobs.Client", "GetProperties", "`containerName` cannot be an empty string.")
32 }
33 if strings.ToLower(containerName) != containerName {
34 return result, validation.NewError("blobs.Client", "GetProperties", "`containerName` must be a lower-cased string.")
35 }
36 if blobName == "" {
37 return result, validation.NewError("blobs.Client", "GetProperties", "`blobName` cannot be an empty string.")
38 }
39 if err := metadata.Validate(input.MetaData); err != nil {
40 return result, validation.NewError("blobs.Client", "GetProperties", fmt.Sprintf("`input.MetaData` is not valid: %s.", err))
41 }
42
43 req, err := client.SetMetaDataPreparer(ctx, accountName, containerName, blobName, input)
44 if err != nil {
45 err = autorest.NewErrorWithError(err, "blobs.Client", "SetMetaData", nil, "Failure preparing request")
46 return
47 }
48
49 resp, err := client.SetMetaDataSender(req)
50 if err != nil {
51 result = autorest.Response{Response: resp}
52 err = autorest.NewErrorWithError(err, "blobs.Client", "SetMetaData", resp, "Failure sending request")
53 return
54 }
55
56 result, err = client.SetMetaDataResponder(resp)
57 if err != nil {
58 err = autorest.NewErrorWithError(err, "blobs.Client", "SetMetaData", resp, "Failure responding to request")
59 return
60 }
61
62 return
63 }
64
65 // SetMetaDataPreparer prepares the SetMetaData request.
66 func (client Client) SetMetaDataPreparer(ctx context.Context, accountName, containerName, blobName string, input SetMetaDataInput) (*http.Request, error) {
67 pathParameters := map[string]interface{}{
68 "containerName": autorest.Encode("path", containerName),
69 "blobName": autorest.Encode("path", blobName),
70 }
71
72 queryParameters := map[string]interface{}{
73 "comp": autorest.Encode("query", "metadata"),
74 }
75
76 headers := map[string]interface{}{
77 "x-ms-version": APIVersion,
78 }
79
80 if input.LeaseID != nil {
81 headers["x-ms-lease-id"] = *input.LeaseID
82 }
83
84 headers = metadata.SetIntoHeaders(headers, input.MetaData)
85
86 preparer := autorest.CreatePreparer(
87 autorest.AsPut(),
88 autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)),
89 autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters),
90 autorest.WithHeaders(headers),
91 autorest.WithQueryParameters(queryParameters))
92 return preparer.Prepare((&http.Request{}).WithContext(ctx))
93 }
94
95 // SetMetaDataSender sends the SetMetaData request. The method will close the
96 // http.Response Body if it receives an error.
97 func (client Client) SetMetaDataSender(req *http.Request) (*http.Response, error) {
98 return autorest.SendWithSender(client, req,
99 azure.DoRetryWithRegistration(client.Client))
100 }
101
102 // SetMetaDataResponder handles the response to the SetMetaData request. The method always
103 // closes the http.Response Body.
104 func (client Client) SetMetaDataResponder(resp *http.Response) (result autorest.Response, err error) {
105 err = autorest.Respond(
106 resp,
107 client.ByInspecting(),
108 azure.WithErrorUnlessStatusCode(http.StatusOK),
109 autorest.ByClosing())
110 result = autorest.Response{Response: resp}
111
112 return
113 }