| 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 DeleteSnapshotsInput struct { |
| 15 |
// The ID of the Lease |
| 16 |
// This must be specified if a Lease is present on the Blob, else a 403 is returned |
| 17 |
LeaseID *string |
| 18 |
} |
| 19 |
|
| 20 |
// DeleteSnapshots marks all Snapshots of a Blob for Deletion, which will be deleted during the next Garbage Collection Cycle. |
| 21 |
func (client Client) DeleteSnapshots(ctx context.Context, accountName, containerName, blobName string, input DeleteSnapshotsInput) (result autorest.Response, err error) { |
| 22 |
if accountName == "" { |
| 23 |
return result, validation.NewError("blobs.Client", "DeleteSnapshots", "`accountName` cannot be an empty string.") |
| 24 |
} |
| 25 |
if containerName == "" { |
| 26 |
return result, validation.NewError("blobs.Client", "DeleteSnapshots", "`containerName` cannot be an empty string.") |
| 27 |
} |
| 28 |
if strings.ToLower(containerName) != containerName { |
| 29 |
return result, validation.NewError("blobs.Client", "DeleteSnapshots", "`containerName` must be a lower-cased string.") |
| 30 |
} |
| 31 |
if blobName == "" { |
| 32 |
return result, validation.NewError("blobs.Client", "DeleteSnapshots", "`blobName` cannot be an empty string.") |
| 33 |
} |
| 34 |
|
| 35 |
req, err := client.DeleteSnapshotsPreparer(ctx, accountName, containerName, blobName, input) |
| 36 |
if err != nil { |
| 37 |
err = autorest.NewErrorWithError(err, "blobs.Client", "DeleteSnapshots", nil, "Failure preparing request") |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
resp, err := client.DeleteSnapshotsSender(req) |
| 42 |
if err != nil { |
| 43 |
result = autorest.Response{Response: resp} |
| 44 |
err = autorest.NewErrorWithError(err, "blobs.Client", "DeleteSnapshots", resp, "Failure sending request") |
| 45 |
return |
| 46 |
} |
| 47 |
|
| 48 |
result, err = client.DeleteSnapshotsResponder(resp) |
| 49 |
if err != nil { |
| 50 |
err = autorest.NewErrorWithError(err, "blobs.Client", "DeleteSnapshots", resp, "Failure responding to request") |
| 51 |
return |
| 52 |
} |
| 53 |
|
| 54 |
return |
| 55 |
} |
| 56 |
|
| 57 |
// DeleteSnapshotsPreparer prepares the DeleteSnapshots request. |
| 58 |
func (client Client) DeleteSnapshotsPreparer(ctx context.Context, accountName, containerName, blobName string, input DeleteSnapshotsInput) (*http.Request, error) { |
| 59 |
pathParameters := map[string]interface{}{ |
| 60 |
"containerName": autorest.Encode("path", containerName), |
| 61 |
"blobName": autorest.Encode("path", blobName), |
| 62 |
} |
| 63 |
|
| 64 |
headers := map[string]interface{}{ |
| 65 |
"x-ms-version": APIVersion, |
| 66 |
// only delete the snapshots but leave the blob as-is |
| 67 |
"x-ms-delete-snapshots": "only", |
| 68 |
} |
| 69 |
|
| 70 |
if input.LeaseID != nil { |
| 71 |
headers["x-ms-lease-id"] = *input.LeaseID |
| 72 |
} |
| 73 |
|
| 74 |
preparer := autorest.CreatePreparer( |
| 75 |
autorest.AsDelete(), |
| 76 |
autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 77 |
autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 78 |
autorest.WithHeaders(headers)) |
| 79 |
return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 80 |
} |
| 81 |
|
| 82 |
// DeleteSnapshotsSender sends the DeleteSnapshots request. The method will close the |
| 83 |
// http.Response Body if it receives an error. |
| 84 |
func (client Client) DeleteSnapshotsSender(req *http.Request) (*http.Response, error) { |
| 85 |
return autorest.SendWithSender(client, req, |
| 86 |
azure.DoRetryWithRegistration(client.Client)) |
| 87 |
} |
| 88 |
|
| 89 |
// DeleteSnapshotsResponder handles the response to the DeleteSnapshots request. The method always |
| 90 |
// closes the http.Response Body. |
| 91 |
func (client Client) DeleteSnapshotsResponder(resp *http.Response) (result autorest.Response, err error) { |
| 92 |
err = autorest.Respond( |
| 93 |
resp, |
| 94 |
client.ByInspecting(), |
| 95 |
azure.WithErrorUnlessStatusCode(http.StatusAccepted), |
| 96 |
autorest.ByClosing()) |
| 97 |
result = autorest.Response{Response: resp} |
| 98 |
return |
| 99 |
} |