| 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 | // ReleaseLease releases a lock based on the Lease ID. |
| 15 | func (client Client) ReleaseLease(ctx context.Context, accountName, containerName, blobName, leaseID string) (result autorest.Response, err error) { |
| 16 | if accountName == "" { |
| 17 | return result, validation.NewError("blobs.Client", "ReleaseLease", "`accountName` cannot be an empty string.") |
| 18 | } |
| 19 | if containerName == "" { |
| 20 | return result, validation.NewError("blobs.Client", "ReleaseLease", "`containerName` cannot be an empty string.") |
| 21 | } |
| 22 | if strings.ToLower(containerName) != containerName { |
| 23 | return result, validation.NewError("blobs.Client", "ReleaseLease", "`containerName` must be a lower-cased string.") |
| 24 | } |
| 25 | if blobName == "" { |
| 26 | return result, validation.NewError("blobs.Client", "ReleaseLease", "`blobName` cannot be an empty string.") |
| 27 | } |
| 28 | if leaseID == "" { |
| 29 | return result, validation.NewError("blobs.Client", "ReleaseLease", "`leaseID` cannot be an empty string.") |
| 30 | } |
| 31 | |
| 32 | req, err := client.ReleaseLeasePreparer(ctx, accountName, containerName, blobName, leaseID) |
| 33 | if err != nil { |
| 34 | err = autorest.NewErrorWithError(err, "blobs.Client", "ReleaseLease", nil, "Failure preparing request") |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | resp, err := client.ReleaseLeaseSender(req) |
| 39 | if err != nil { |
| 40 | result = autorest.Response{Response: resp} |
| 41 | err = autorest.NewErrorWithError(err, "blobs.Client", "ReleaseLease", resp, "Failure sending request") |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | result, err = client.ReleaseLeaseResponder(resp) |
| 46 | if err != nil { |
| 47 | err = autorest.NewErrorWithError(err, "blobs.Client", "ReleaseLease", resp, "Failure responding to request") |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | // ReleaseLeasePreparer prepares the ReleaseLease request. |
| 55 | func (client Client) ReleaseLeasePreparer(ctx context.Context, accountName, containerName, blobName, leaseID string) (*http.Request, error) { |
| 56 | pathParameters := map[string]interface{}{ |
| 57 | "containerName": autorest.Encode("path", containerName), |
| 58 | "blobName": autorest.Encode("path", blobName), |
| 59 | } |
| 60 | |
| 61 | queryParameters := map[string]interface{}{ |
| 62 | "comp": autorest.Encode("query", "lease"), |
| 63 | } |
| 64 | |
| 65 | headers := map[string]interface{}{ |
| 66 | "x-ms-version": APIVersion, |
| 67 | "x-ms-lease-action": "release", |
| 68 | "x-ms-lease-id": leaseID, |
| 69 | } |
| 70 | |
| 71 | preparer := autorest.CreatePreparer( |
| 72 | autorest.AsPut(), |
| 73 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 74 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 75 | autorest.WithHeaders(headers), |
| 76 | autorest.WithQueryParameters(queryParameters)) |
| 77 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 78 | } |
| 79 | |
| 80 | // ReleaseLeaseSender sends the ReleaseLease request. The method will close the |
| 81 | // http.Response Body if it receives an error. |
| 82 | func (client Client) ReleaseLeaseSender(req *http.Request) (*http.Response, error) { |
| 83 | return autorest.SendWithSender(client, req, |
| 84 | azure.DoRetryWithRegistration(client.Client)) |
| 85 | } |
| 86 | |
| 87 | // ReleaseLeaseResponder handles the response to the ReleaseLease request. The method always |
| 88 | // closes the http.Response Body. |
| 89 | func (client Client) ReleaseLeaseResponder(resp *http.Response) (result autorest.Response, err error) { |
| 90 | err = autorest.Respond( |
| 91 | resp, |
| 92 | client.ByInspecting(), |
| 93 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 94 | autorest.ByClosing()) |
| 95 | result = autorest.Response{Response: resp} |
| 96 | |
| 97 | return |
| 98 | } |