Debian Codegraph @

storage/2020-08-04/file/shares/snapshot_create.go

raw
1 package shares
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 CreateSnapshotInput struct {
17 MetaData map[string]string
18 }
19
20 type CreateSnapshotResult struct {
21 autorest.Response
22
23 // This header is a DateTime value that uniquely identifies the share snapshot.
24 // The value of this header may be used in subsequent requests to access the share snapshot.
25 // This value is opaque.
26 SnapshotDateTime string
27 }
28
29 // CreateSnapshot creates a read-only snapshot of the share
30 // A share can support creation of 200 share snapshots. Attempting to create more than 200 share snapshots fails with 409 (Conflict).
31 // Attempting to create a share snapshot while a previous Snapshot Share operation is in progress fails with 409 (Conflict).
32 func (client Client) CreateSnapshot(ctx context.Context, accountName, shareName string, input CreateSnapshotInput) (result CreateSnapshotResult, err error) {
33 if accountName == "" {
34 return result, validation.NewError("shares.Client", "CreateSnapshot", "`accountName` cannot be an empty string.")
35 }
36 if shareName == "" {
37 return result, validation.NewError("shares.Client", "CreateSnapshot", "`shareName` cannot be an empty string.")
38 }
39 if strings.ToLower(shareName) != shareName {
40 return result, validation.NewError("shares.Client", "CreateSnapshot", "`shareName` must be a lower-cased string.")
41 }
42 if err := metadata.Validate(input.MetaData); err != nil {
43 return result, validation.NewError("shares.Client", "CreateSnapshot", fmt.Sprintf("`input.MetaData` is not valid: %s.", err))
44 }
45
46 req, err := client.CreateSnapshotPreparer(ctx, accountName, shareName, input)
47 if err != nil {
48 err = autorest.NewErrorWithError(err, "shares.Client", "CreateSnapshot", nil, "Failure preparing request")
49 return
50 }
51
52 resp, err := client.CreateSnapshotSender(req)
53 if err != nil {
54 result.Response = autorest.Response{Response: resp}
55 err = autorest.NewErrorWithError(err, "shares.Client", "CreateSnapshot", resp, "Failure sending request")
56 return
57 }
58
59 result, err = client.CreateSnapshotResponder(resp)
60 if err != nil {
61 err = autorest.NewErrorWithError(err, "shares.Client", "CreateSnapshot", resp, "Failure responding to request")
62 return
63 }
64
65 return
66 }
67
68 // CreateSnapshotPreparer prepares the CreateSnapshot request.
69 func (client Client) CreateSnapshotPreparer(ctx context.Context, accountName, shareName string, input CreateSnapshotInput) (*http.Request, error) {
70 pathParameters := map[string]interface{}{
71 "shareName": autorest.Encode("path", shareName),
72 }
73
74 queryParameters := map[string]interface{}{
75 "comp": autorest.Encode("query", "snapshot"),
76 "restype": autorest.Encode("query", "share"),
77 }
78
79 headers := map[string]interface{}{
80 "x-ms-version": APIVersion,
81 }
82
83 headers = metadata.SetIntoHeaders(headers, input.MetaData)
84
85 preparer := autorest.CreatePreparer(
86 autorest.AsContentType("application/xml; charset=utf-8"),
87 autorest.AsPut(),
88 autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
89 autorest.WithPathParameters("/{shareName}", pathParameters),
90 autorest.WithQueryParameters(queryParameters),
91 autorest.WithHeaders(headers))
92 return preparer.Prepare((&http.Request{}).WithContext(ctx))
93 }
94
95 // CreateSnapshotSender sends the CreateSnapshot request. The method will close the
96 // http.Response Body if it receives an error.
97 func (client Client) CreateSnapshotSender(req *http.Request) (*http.Response, error) {
98 return autorest.SendWithSender(client, req,
99 azure.DoRetryWithRegistration(client.Client))
100 }
101
102 // CreateSnapshotResponder handles the response to the CreateSnapshot request. The method always
103 // closes the http.Response Body.
104 func (client Client) CreateSnapshotResponder(resp *http.Response) (result CreateSnapshotResult, err error) {
105 result.SnapshotDateTime = resp.Header.Get("x-ms-snapshot")
106
107 err = autorest.Respond(
108 resp,
109 client.ByInspecting(),
110 azure.WithErrorUnlessStatusCode(http.StatusCreated),
111 autorest.ByClosing())
112 result.Response = autorest.Response{Response: resp}
113
114 return
115 }