Debian Codegraph @

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

raw
1 package containers
2
3 import (
4 "context"
5 "net/http"
6
7 "github.com/Azure/go-autorest/autorest"
8 "github.com/Azure/go-autorest/autorest/azure"
9 "github.com/Azure/go-autorest/autorest/validation"
10 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
11 )
12
13 type ChangeLeaseInput struct {
14 ExistingLeaseID string
15 ProposedLeaseID string
16 }
17
18 type ChangeLeaseResponse struct {
19 autorest.Response
20
21 LeaseID string
22 }
23
24 // ChangeLease changes the lock from one Lease ID to another Lease ID
25 func (client Client) ChangeLease(ctx context.Context, accountName, containerName string, input ChangeLeaseInput) (result ChangeLeaseResponse, err error) {
26 if accountName == "" {
27 return result, validation.NewError("containers.Client", "ChangeLease", "`accountName` cannot be an empty string.")
28 }
29 if containerName == "" {
30 return result, validation.NewError("containers.Client", "ChangeLease", "`containerName` cannot be an empty string.")
31 }
32 if input.ExistingLeaseID == "" {
33 return result, validation.NewError("containers.Client", "ChangeLease", "`input.ExistingLeaseID` cannot be an empty string.")
34 }
35 if input.ProposedLeaseID == "" {
36 return result, validation.NewError("containers.Client", "ChangeLease", "`input.ProposedLeaseID` cannot be an empty string.")
37 }
38
39 req, err := client.ChangeLeasePreparer(ctx, accountName, containerName, input)
40 if err != nil {
41 err = autorest.NewErrorWithError(err, "containers.Client", "ChangeLease", nil, "Failure preparing request")
42 return
43 }
44
45 resp, err := client.ChangeLeaseSender(req)
46 if err != nil {
47 result.Response = autorest.Response{Response: resp}
48 err = autorest.NewErrorWithError(err, "containers.Client", "ChangeLease", resp, "Failure sending request")
49 return
50 }
51
52 result, err = client.ChangeLeaseResponder(resp)
53 if err != nil {
54 err = autorest.NewErrorWithError(err, "containers.Client", "ChangeLease", resp, "Failure responding to request")
55 return
56 }
57
58 return
59 }
60
61 // ChangeLeasePreparer prepares the ChangeLease request.
62 func (client Client) ChangeLeasePreparer(ctx context.Context, accountName string, containerName string, input ChangeLeaseInput) (*http.Request, error) {
63 pathParameters := map[string]interface{}{
64 "containerName": autorest.Encode("path", containerName),
65 }
66
67 queryParameters := map[string]interface{}{
68 "restype": autorest.Encode("path", "container"),
69 "comp": autorest.Encode("path", "lease"),
70 }
71
72 headers := map[string]interface{}{
73 "x-ms-version": APIVersion,
74 "x-ms-lease-action": "change",
75 "x-ms-lease-id": input.ExistingLeaseID,
76 "x-ms-proposed-lease-id": input.ProposedLeaseID,
77 }
78
79 preparer := autorest.CreatePreparer(
80 autorest.AsContentType("application/xml; charset=utf-8"),
81 autorest.AsPut(),
82 autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)),
83 autorest.WithPathParameters("/{containerName}", pathParameters),
84 autorest.WithQueryParameters(queryParameters),
85 autorest.WithHeaders(headers))
86 return preparer.Prepare((&http.Request{}).WithContext(ctx))
87 }
88
89 // ChangeLeaseSender sends the ChangeLease request. The method will close the
90 // http.Response Body if it receives an error.
91 func (client Client) ChangeLeaseSender(req *http.Request) (*http.Response, error) {
92 return autorest.SendWithSender(client, req,
93 azure.DoRetryWithRegistration(client.Client))
94 }
95
96 // ChangeLeaseResponder handles the response to the ChangeLease request. The method always
97 // closes the http.Response Body.
98 func (client Client) ChangeLeaseResponder(resp *http.Response) (result ChangeLeaseResponse, err error) {
99 if resp != nil {
100 result.LeaseID = resp.Header.Get("x-ms-lease-id")
101 }
102
103 err = autorest.Respond(
104 resp,
105 client.ByInspecting(),
106 azure.WithErrorUnlessStatusCode(http.StatusOK),
107 autorest.ByClosing())
108 result.Response = autorest.Response{Response: resp}
109
110 return
111 }