Debian Codegraph @

storage/2018-11-09/blob/blobs/snapshot.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 SnapshotInput 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 // MetaData is a user-defined name-value pair associated with the blob.
22 // If no name-value pairs are specified, the operation will copy the base blob metadata to the snapshot.
23 // If one or more name-value pairs are specified, the snapshot is created with the specified metadata,
24 // and metadata is not copied from the base blob.
25 MetaData map[string]string
26
27 // A DateTime value which will only snapshot the blob if it has been modified since the specified date/time
28 // If the base blob has not been modified, the Blob service returns status code 412 (Precondition Failed).
29 IfModifiedSince *string
30
31 // A DateTime value which will only snapshot the blob if it has not been modified since the specified date/time
32 // If the base blob has been modified, the Blob service returns status code 412 (Precondition Failed).
33 IfUnmodifiedSince *string
34
35 // An ETag value to snapshot the blob only if its ETag value matches the value specified.
36 // If the values do not match, the Blob service returns status code 412 (Precondition Failed).
37 IfMatch *string
38
39 // An ETag value for this conditional header to snapshot the blob only if its ETag value
40 // does not match the value specified.
41 // If the values are identical, the Blob service returns status code 412 (Precondition Failed).
42 IfNoneMatch *string
43 }
44
45 type SnapshotResult struct {
46 autorest.Response
47
48 // The ETag of the snapshot
49 ETag string
50
51 // A DateTime value that uniquely identifies the snapshot.
52 // The value of this header indicates the snapshot version,
53 // and may be used in subsequent requests to access the snapshot.
54 SnapshotDateTime string
55 }
56
57 // Snapshot captures a Snapshot of a given Blob
58 func (client Client) Snapshot(ctx context.Context, accountName, containerName, blobName string, input SnapshotInput) (result SnapshotResult, err error) {
59 if accountName == "" {
60 return result, validation.NewError("blobs.Client", "Snapshot", "`accountName` cannot be an empty string.")
61 }
62 if containerName == "" {
63 return result, validation.NewError("blobs.Client", "Snapshot", "`containerName` cannot be an empty string.")
64 }
65 if strings.ToLower(containerName) != containerName {
66 return result, validation.NewError("blobs.Client", "Snapshot", "`containerName` must be a lower-cased string.")
67 }
68 if blobName == "" {
69 return result, validation.NewError("blobs.Client", "Snapshot", "`blobName` cannot be an empty string.")
70 }
71 if err := metadata.Validate(input.MetaData); err != nil {
72 return result, validation.NewError("blobs.Client", "Snapshot", fmt.Sprintf("`input.MetaData` is not valid: %s.", err))
73 }
74
75 req, err := client.SnapshotPreparer(ctx, accountName, containerName, blobName, input)
76 if err != nil {
77 err = autorest.NewErrorWithError(err, "blobs.Client", "Snapshot", nil, "Failure preparing request")
78 return
79 }
80
81 resp, err := client.SnapshotSender(req)
82 if err != nil {
83 result.Response = autorest.Response{Response: resp}
84 err = autorest.NewErrorWithError(err, "blobs.Client", "Snapshot", resp, "Failure sending request")
85 return
86 }
87
88 result, err = client.SnapshotResponder(resp)
89 if err != nil {
90 err = autorest.NewErrorWithError(err, "blobs.Client", "Snapshot", resp, "Failure responding to request")
91 return
92 }
93
94 return
95 }
96
97 // SnapshotPreparer prepares the Snapshot request.
98 func (client Client) SnapshotPreparer(ctx context.Context, accountName, containerName, blobName string, input SnapshotInput) (*http.Request, error) {
99 pathParameters := map[string]interface{}{
100 "containerName": autorest.Encode("path", containerName),
101 "blobName": autorest.Encode("path", blobName),
102 }
103
104 queryParameters := map[string]interface{}{
105 "comp": autorest.Encode("query", "snapshot"),
106 }
107
108 headers := map[string]interface{}{
109 "x-ms-version": APIVersion,
110 }
111
112 if input.LeaseID != nil {
113 headers["x-ms-lease-id"] = *input.LeaseID
114 }
115
116 if input.IfModifiedSince != nil {
117 headers["If-Modified-Since"] = *input.IfModifiedSince
118 }
119 if input.IfUnmodifiedSince != nil {
120 headers["If-Unmodified-Since"] = *input.IfUnmodifiedSince
121 }
122 if input.IfMatch != nil {
123 headers["If-Match"] = *input.IfMatch
124 }
125 if input.IfNoneMatch != nil {
126 headers["If-None-Match"] = *input.IfNoneMatch
127 }
128
129 headers = metadata.SetIntoHeaders(headers, input.MetaData)
130
131 preparer := autorest.CreatePreparer(
132 autorest.AsPut(),
133 autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)),
134 autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters),
135 autorest.WithQueryParameters(queryParameters),
136 autorest.WithHeaders(headers))
137 return preparer.Prepare((&http.Request{}).WithContext(ctx))
138 }
139
140 // SnapshotSender sends the Snapshot request. The method will close the
141 // http.Response Body if it receives an error.
142 func (client Client) SnapshotSender(req *http.Request) (*http.Response, error) {
143 return autorest.SendWithSender(client, req,
144 azure.DoRetryWithRegistration(client.Client))
145 }
146
147 // SnapshotResponder handles the response to the Snapshot request. The method always
148 // closes the http.Response Body.
149 func (client Client) SnapshotResponder(resp *http.Response) (result SnapshotResult, err error) {
150 if resp != nil && resp.Header != nil {
151 result.ETag = resp.Header.Get("ETag")
152 result.SnapshotDateTime = resp.Header.Get("x-ms-snapshot")
153 }
154
155 err = autorest.Respond(
156 resp,
157 client.ByInspecting(),
158 azure.WithErrorUnlessStatusCode(http.StatusCreated),
159 autorest.ByClosing())
160 result.Response = autorest.Response{Response: resp}
161
162 return
163 }