Debian Codegraph @

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

raw
1 package blobs
2
3 import (
4 "context"
5 "net/http"
6 "strings"
7
8 "github.com/Azure/go-autorest/autorest"
9 "github.com/Azure/go-autorest/autorest/azure"
10 "github.com/Azure/go-autorest/autorest/validation"
11 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
12 )
13
14 type ChangeLeaseInput struct {
15 ExistingLeaseID string
16 ProposedLeaseID string
17 }
18
19 type ChangeLeaseResponse struct {
20 autorest.Response
21
22 LeaseID string
23 }
24
25 // ChangeLease changes an existing lock on a blob for another lock.
26 func (client Client) ChangeLease(ctx context.Context, accountName, containerName, blobName string, input ChangeLeaseInput) (result ChangeLeaseResponse, err error) {
27 if accountName == "" {
28 return result, validation.NewError("blobs.Client", "ChangeLease", "`accountName` cannot be an empty string.")
29 }
30 if containerName == "" {
31 return result, validation.NewError("blobs.Client", "ChangeLease", "`containerName` cannot be an empty string.")
32 }
33 if strings.ToLower(containerName) != containerName {
34 return result, validation.NewError("blobs.Client", "ChangeLease", "`containerName` must be a lower-cased string.")
35 }
36 if blobName == "" {
37 return result, validation.NewError("blobs.Client", "ChangeLease", "`blobName` cannot be an empty string.")
38 }
39 if input.ExistingLeaseID == "" {
40 return result, validation.NewError("blobs.Client", "ChangeLease", "`input.ExistingLeaseID` cannot be an empty string.")
41 }
42 if input.ProposedLeaseID == "" {
43 return result, validation.NewError("blobs.Client", "ChangeLease", "`input.ProposedLeaseID` cannot be an empty string.")
44 }
45
46 req, err := client.ChangeLeasePreparer(ctx, accountName, containerName, blobName, input)
47 if err != nil {
48 err = autorest.NewErrorWithError(err, "blobs.Client", "ChangeLease", nil, "Failure preparing request")
49 return
50 }
51
52 resp, err := client.ChangeLeaseSender(req)
53 if err != nil {
54 result.Response = autorest.Response{Response: resp}
55 err = autorest.NewErrorWithError(err, "blobs.Client", "ChangeLease", resp, "Failure sending request")
56 return
57 }
58
59 result, err = client.ChangeLeaseResponder(resp)
60 if err != nil {
61 err = autorest.NewErrorWithError(err, "blobs.Client", "ChangeLease", resp, "Failure responding to request")
62 return
63 }
64
65 return
66 }
67
68 // ChangeLeasePreparer prepares the ChangeLease request.
69 func (client Client) ChangeLeasePreparer(ctx context.Context, accountName, containerName, blobName string, input ChangeLeaseInput) (*http.Request, error) {
70 pathParameters := map[string]interface{}{
71 "containerName": autorest.Encode("path", containerName),
72 "blobName": autorest.Encode("path", blobName),
73 }
74
75 queryParameters := map[string]interface{}{
76 "comp": autorest.Encode("query", "lease"),
77 }
78
79 headers := map[string]interface{}{
80 "x-ms-version": APIVersion,
81 "x-ms-lease-action": "change",
82 "x-ms-lease-id": input.ExistingLeaseID,
83 "x-ms-proposed-lease-id": input.ProposedLeaseID,
84 }
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 // ChangeLeaseSender sends the ChangeLease request. The method will close the
96 // http.Response Body if it receives an error.
97 func (client Client) ChangeLeaseSender(req *http.Request) (*http.Response, error) {
98 return autorest.SendWithSender(client, req,
99 azure.DoRetryWithRegistration(client.Client))
100 }
101
102 // ChangeLeaseResponder handles the response to the ChangeLease request. The method always
103 // closes the http.Response Body.
104 func (client Client) ChangeLeaseResponder(resp *http.Response) (result ChangeLeaseResponse, err error) {
105 if resp != nil && resp.Header != nil {
106 result.LeaseID = resp.Header.Get("x-ms-lease-id")
107 }
108
109 err = autorest.Respond(
110 resp,
111 client.ByInspecting(),
112 azure.WithErrorUnlessStatusCode(http.StatusOK),
113 autorest.ByClosing())
114 result.Response = autorest.Response{Response: resp}
115
116 return
117 }