| 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 AcquireLeaseInput struct { |
| 14 | // Specifies the duration of the lease, in seconds, or negative one (-1) for a lease that never expires. |
| 15 | // A non-infinite lease can be between 15 and 60 seconds |
| 16 | LeaseDuration int |
| 17 | |
| 18 | ProposedLeaseID string |
| 19 | } |
| 20 | |
| 21 | type AcquireLeaseResponse struct { |
| 22 | autorest.Response |
| 23 | |
| 24 | LeaseID string |
| 25 | } |
| 26 | |
| 27 | // AcquireLease establishes and manages a lock on a container for delete operations. |
| 28 | func (client Client) AcquireLease(ctx context.Context, accountName, containerName string, input AcquireLeaseInput) (result AcquireLeaseResponse, err error) { |
| 29 | if accountName == "" { |
| 30 | return result, validation.NewError("containers.Client", "AcquireLease", "`accountName` cannot be an empty string.") |
| 31 | } |
| 32 | if containerName == "" { |
| 33 | return result, validation.NewError("containers.Client", "AcquireLease", "`containerName` cannot be an empty string.") |
| 34 | } |
| 35 | // An infinite lease duration is -1 seconds. A non-infinite lease can be between 15 and 60 seconds |
| 36 | if input.LeaseDuration != -1 && (input.LeaseDuration <= 15 || input.LeaseDuration >= 60) { |
| 37 | return result, validation.NewError("containers.Client", "AcquireLease", "`input.LeaseDuration` must be -1 (infinite), or between 15 and 60 seconds.") |
| 38 | } |
| 39 | |
| 40 | req, err := client.AcquireLeasePreparer(ctx, accountName, containerName, input) |
| 41 | if err != nil { |
| 42 | err = autorest.NewErrorWithError(err, "containers.Client", "AcquireLease", nil, "Failure preparing request") |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | resp, err := client.AcquireLeaseSender(req) |
| 47 | if err != nil { |
| 48 | result.Response = autorest.Response{Response: resp} |
| 49 | err = autorest.NewErrorWithError(err, "containers.Client", "AcquireLease", resp, "Failure sending request") |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | result, err = client.AcquireLeaseResponder(resp) |
| 54 | if err != nil { |
| 55 | err = autorest.NewErrorWithError(err, "containers.Client", "AcquireLease", resp, "Failure responding to request") |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | // AcquireLeasePreparer prepares the AcquireLease request. |
| 63 | func (client Client) AcquireLeasePreparer(ctx context.Context, accountName string, containerName string, input AcquireLeaseInput) (*http.Request, error) { |
| 64 | pathParameters := map[string]interface{}{ |
| 65 | "containerName": autorest.Encode("path", containerName), |
| 66 | } |
| 67 | |
| 68 | queryParameters := map[string]interface{}{ |
| 69 | "restype": autorest.Encode("path", "container"), |
| 70 | "comp": autorest.Encode("path", "lease"), |
| 71 | } |
| 72 | |
| 73 | headers := map[string]interface{}{ |
| 74 | "x-ms-version": APIVersion, |
| 75 | "x-ms-lease-action": "acquire", |
| 76 | "x-ms-lease-duration": input.LeaseDuration, |
| 77 | } |
| 78 | |
| 79 | if input.ProposedLeaseID != "" { |
| 80 | headers["x-ms-proposed-lease-id"] = input.ProposedLeaseID |
| 81 | } |
| 82 | |
| 83 | preparer := autorest.CreatePreparer( |
| 84 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 85 | autorest.AsPut(), |
| 86 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 87 | autorest.WithPathParameters("/{containerName}", pathParameters), |
| 88 | autorest.WithQueryParameters(queryParameters), |
| 89 | autorest.WithHeaders(headers)) |
| 90 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 91 | } |
| 92 | |
| 93 | // AcquireLeaseSender sends the AcquireLease request. The method will close the |
| 94 | // http.Response Body if it receives an error. |
| 95 | func (client Client) AcquireLeaseSender(req *http.Request) (*http.Response, error) { |
| 96 | return autorest.SendWithSender(client, req, |
| 97 | azure.DoRetryWithRegistration(client.Client)) |
| 98 | } |
| 99 | |
| 100 | // AcquireLeaseResponder handles the response to the AcquireLease request. The method always |
| 101 | // closes the http.Response Body. |
| 102 | func (client Client) AcquireLeaseResponder(resp *http.Response) (result AcquireLeaseResponse, err error) { |
| 103 | if resp != nil { |
| 104 | result.LeaseID = resp.Header.Get("x-ms-lease-id") |
| 105 | } |
| 106 | |
| 107 | err = autorest.Respond( |
| 108 | resp, |
| 109 | client.ByInspecting(), |
| 110 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 111 | autorest.ByClosing()) |
| 112 | result.Response = autorest.Response{Response: resp} |
| 113 | |
| 114 | return |
| 115 | } |