| 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 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 an existing lock on a blob using the LeaseID. |
| 37 |
func (client Client) BreakLease(ctx context.Context, accountName, containerName, blobName string, input BreakLeaseInput) (result autorest.Response, err error) { |
| 38 |
if accountName == "" { |
| 39 |
return result, validation.NewError("blobs.Client", "BreakLease", "`accountName` cannot be an empty string.") |
| 40 |
} |
| 41 |
if containerName == "" { |
| 42 |
return result, validation.NewError("blobs.Client", "BreakLease", "`containerName` cannot be an empty string.") |
| 43 |
} |
| 44 |
if strings.ToLower(containerName) != containerName { |
| 45 |
return result, validation.NewError("blobs.Client", "BreakLease", "`containerName` must be a lower-cased string.") |
| 46 |
} |
| 47 |
if blobName == "" { |
| 48 |
return result, validation.NewError("blobs.Client", "BreakLease", "`blobName` cannot be an empty string.") |
| 49 |
} |
| 50 |
if input.LeaseID == "" { |
| 51 |
return result, validation.NewError("blobs.Client", "BreakLease", "`input.LeaseID` cannot be an empty string.") |
| 52 |
} |
| 53 |
|
| 54 |
req, err := client.BreakLeasePreparer(ctx, accountName, containerName, blobName, input) |
| 55 |
if err != nil { |
| 56 |
err = autorest.NewErrorWithError(err, "blobs.Client", "BreakLease", nil, "Failure preparing request") |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
resp, err := client.BreakLeaseSender(req) |
| 61 |
if err != nil { |
| 62 |
result = autorest.Response{Response: resp} |
| 63 |
err = autorest.NewErrorWithError(err, "blobs.Client", "BreakLease", resp, "Failure sending request") |
| 64 |
return |
| 65 |
} |
| 66 |
|
| 67 |
result, err = client.BreakLeaseResponder(resp) |
| 68 |
if err != nil { |
| 69 |
err = autorest.NewErrorWithError(err, "blobs.Client", "BreakLease", resp, "Failure responding to request") |
| 70 |
return |
| 71 |
} |
| 72 |
|
| 73 |
return |
| 74 |
} |
| 75 |
|
| 76 |
// BreakLeasePreparer prepares the BreakLease request. |
| 77 |
func (client Client) BreakLeasePreparer(ctx context.Context, accountName, containerName, blobName string, input BreakLeaseInput) (*http.Request, error) { |
| 78 |
pathParameters := map[string]interface{}{ |
| 79 |
"containerName": autorest.Encode("path", containerName), |
| 80 |
"blobName": autorest.Encode("path", blobName), |
| 81 |
} |
| 82 |
|
| 83 |
queryParameters := map[string]interface{}{ |
| 84 |
"comp": autorest.Encode("query", "lease"), |
| 85 |
} |
| 86 |
|
| 87 |
headers := map[string]interface{}{ |
| 88 |
"x-ms-version": APIVersion, |
| 89 |
"x-ms-lease-action": "break", |
| 90 |
"x-ms-lease-id": input.LeaseID, |
| 91 |
} |
| 92 |
|
| 93 |
if input.BreakPeriod != nil { |
| 94 |
headers["x-ms-lease-break-period"] = *input.BreakPeriod |
| 95 |
} |
| 96 |
|
| 97 |
preparer := autorest.CreatePreparer( |
| 98 |
autorest.AsPut(), |
| 99 |
autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 100 |
autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 101 |
autorest.WithHeaders(headers), |
| 102 |
autorest.WithQueryParameters(queryParameters)) |
| 103 |
return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 104 |
} |
| 105 |
|
| 106 |
// BreakLeaseSender sends the BreakLease request. The method will close the |
| 107 |
// http.Response Body if it receives an error. |
| 108 |
func (client Client) BreakLeaseSender(req *http.Request) (*http.Response, error) { |
| 109 |
return autorest.SendWithSender(client, req, |
| 110 |
azure.DoRetryWithRegistration(client.Client)) |
| 111 |
} |
| 112 |
|
| 113 |
// BreakLeaseResponder handles the response to the BreakLease request. The method always |
| 114 |
// closes the http.Response Body. |
| 115 |
func (client Client) BreakLeaseResponder(resp *http.Response) (result autorest.Response, err error) { |
| 116 |
err = autorest.Respond( |
| 117 |
resp, |
| 118 |
client.ByInspecting(), |
| 119 |
azure.WithErrorUnlessStatusCode(http.StatusAccepted), |
| 120 |
autorest.ByClosing()) |
| 121 |
result = autorest.Response{Response: resp} |
| 122 |
|
| 123 |
return |
| 124 |
} |