Debian Codegraph @

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

raw
1 package containers
2
3 import (
4 "context"
5 "net/http"
6 "strconv"
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 BreakLeaseInput struct {
15 // For a break operation, proposed duration the lease should continue
16 // before it is broken, in seconds, between 0 and 60.
17 // This break period is only used if it is shorter than the time remaining on the lease.
18 // If longer, the time remaining on the lease is used.
19 // A new lease will not be available before the break period has expired,
20 // but the lease may be held for longer than the break period.
21 // If this header does not appear with a break operation, a fixed-duration lease breaks
22 // after the remaining lease period elapses, and an infinite lease breaks immediately.
23 BreakPeriod *int
24
25 LeaseID string
26 }
27
28 type BreakLeaseResponse struct {
29 autorest.Response
30
31 // Approximate time remaining in the lease period, in seconds.
32 // If the break is immediate, 0 is returned.
33 LeaseTime int
34 }
35
36 // BreakLease breaks a lock based on it's Lease ID
37 func (client Client) BreakLease(ctx context.Context, accountName, containerName string, input BreakLeaseInput) (result BreakLeaseResponse, err error) {
38 if accountName == "" {
39 return result, validation.NewError("containers.Client", "BreakLease", "`accountName` cannot be an empty string.")
40 }
41 if containerName == "" {
42 return result, validation.NewError("containers.Client", "BreakLease", "`containerName` cannot be an empty string.")
43 }
44 if input.LeaseID == "" {
45 return result, validation.NewError("containers.Client", "BreakLease", "`input.LeaseID` cannot be an empty string.")
46 }
47
48 req, err := client.BreakLeasePreparer(ctx, accountName, containerName, input)
49 if err != nil {
50 err = autorest.NewErrorWithError(err, "containers.Client", "BreakLease", nil, "Failure preparing request")
51 return
52 }
53
54 resp, err := client.BreakLeaseSender(req)
55 if err != nil {
56 result.Response = autorest.Response{Response: resp}
57 err = autorest.NewErrorWithError(err, "containers.Client", "BreakLease", resp, "Failure sending request")
58 return
59 }
60
61 result, err = client.BreakLeaseResponder(resp)
62 if err != nil {
63 err = autorest.NewErrorWithError(err, "containers.Client", "BreakLease", resp, "Failure responding to request")
64 return
65 }
66
67 return
68 }
69
70 // BreakLeasePreparer prepares the BreakLease request.
71 func (client Client) BreakLeasePreparer(ctx context.Context, accountName string, containerName string, input BreakLeaseInput) (*http.Request, error) {
72 pathParameters := map[string]interface{}{
73 "containerName": autorest.Encode("path", containerName),
74 }
75
76 queryParameters := map[string]interface{}{
77 "restype": autorest.Encode("path", "container"),
78 "comp": autorest.Encode("path", "lease"),
79 }
80
81 headers := map[string]interface{}{
82 "x-ms-version": APIVersion,
83 "x-ms-lease-action": "break",
84 "x-ms-lease-id": input.LeaseID,
85 }
86
87 if input.BreakPeriod != nil {
88 headers["x-ms-lease-break-period"] = *input.BreakPeriod
89 }
90
91 preparer := autorest.CreatePreparer(
92 autorest.AsContentType("application/xml; charset=utf-8"),
93 autorest.AsPut(),
94 autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)),
95 autorest.WithPathParameters("/{containerName}", pathParameters),
96 autorest.WithQueryParameters(queryParameters),
97 autorest.WithHeaders(headers))
98 return preparer.Prepare((&http.Request{}).WithContext(ctx))
99 }
100
101 // BreakLeaseSender sends the BreakLease request. The method will close the
102 // http.Response Body if it receives an error.
103 func (client Client) BreakLeaseSender(req *http.Request) (*http.Response, error) {
104 return autorest.SendWithSender(client, req,
105 azure.DoRetryWithRegistration(client.Client))
106 }
107
108 // BreakLeaseResponder handles the response to the BreakLease request. The method always
109 // closes the http.Response Body.
110 func (client Client) BreakLeaseResponder(resp *http.Response) (result BreakLeaseResponse, err error) {
111 if resp != nil {
112 leaseRaw := resp.Header.Get("x-ms-lease-time")
113 if leaseRaw != "" {
114 i, err := strconv.Atoi(leaseRaw)
115 if err == nil {
116 result.LeaseTime = i
117 }
118 }
119 }
120
121 err = autorest.Respond(
122 resp,
123 client.ByInspecting(),
124 azure.WithErrorUnlessStatusCode(http.StatusAccepted),
125 autorest.ByClosing())
126 result.Response = autorest.Response{Response: resp}
127
128 return
129 }