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