| 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 DeleteSnapshotInput 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 | // The DateTime of the Snapshot which should be marked for Deletion |
| 20 | SnapshotDateTime string |
| 21 | } |
| 22 | |
| 23 | // DeleteSnapshot marks a single Snapshot of a Blob for Deletion based on it's DateTime, which will be deleted during the next Garbage Collection cycle. |
| 24 | func (client Client) DeleteSnapshot(ctx context.Context, accountName, containerName, blobName string, input DeleteSnapshotInput) (result autorest.Response, err error) { |
| 25 | if accountName == "" { |
| 26 | return result, validation.NewError("blobs.Client", "DeleteSnapshot", "`accountName` cannot be an empty string.") |
| 27 | } |
| 28 | if containerName == "" { |
| 29 | return result, validation.NewError("blobs.Client", "DeleteSnapshot", "`containerName` cannot be an empty string.") |
| 30 | } |
| 31 | if strings.ToLower(containerName) != containerName { |
| 32 | return result, validation.NewError("blobs.Client", "DeleteSnapshot", "`containerName` must be a lower-cased string.") |
| 33 | } |
| 34 | if blobName == "" { |
| 35 | return result, validation.NewError("blobs.Client", "DeleteSnapshot", "`blobName` cannot be an empty string.") |
| 36 | } |
| 37 | if input.SnapshotDateTime == "" { |
| 38 | return result, validation.NewError("blobs.Client", "DeleteSnapshot", "`input.SnapshotDateTime` cannot be an empty string.") |
| 39 | } |
| 40 | |
| 41 | req, err := client.DeleteSnapshotPreparer(ctx, accountName, containerName, blobName, input) |
| 42 | if err != nil { |
| 43 | err = autorest.NewErrorWithError(err, "blobs.Client", "DeleteSnapshot", nil, "Failure preparing request") |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | resp, err := client.DeleteSnapshotSender(req) |
| 48 | if err != nil { |
| 49 | result = autorest.Response{Response: resp} |
| 50 | err = autorest.NewErrorWithError(err, "blobs.Client", "DeleteSnapshot", resp, "Failure sending request") |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | result, err = client.DeleteSnapshotResponder(resp) |
| 55 | if err != nil { |
| 56 | err = autorest.NewErrorWithError(err, "blobs.Client", "DeleteSnapshot", resp, "Failure responding to request") |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | // DeleteSnapshotPreparer prepares the DeleteSnapshot request. |
| 64 | func (client Client) DeleteSnapshotPreparer(ctx context.Context, accountName, containerName, blobName string, input DeleteSnapshotInput) (*http.Request, error) { |
| 65 | pathParameters := map[string]interface{}{ |
| 66 | "containerName": autorest.Encode("path", containerName), |
| 67 | "blobName": autorest.Encode("path", blobName), |
| 68 | } |
| 69 | |
| 70 | queryParameters := map[string]interface{}{ |
| 71 | "snapshot": autorest.Encode("query", input.SnapshotDateTime), |
| 72 | } |
| 73 | |
| 74 | headers := map[string]interface{}{ |
| 75 | "x-ms-version": APIVersion, |
| 76 | } |
| 77 | |
| 78 | if input.LeaseID != nil { |
| 79 | headers["x-ms-lease-id"] = *input.LeaseID |
| 80 | } |
| 81 | |
| 82 | preparer := autorest.CreatePreparer( |
| 83 | autorest.AsDelete(), |
| 84 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 85 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 86 | autorest.WithQueryParameters(queryParameters), |
| 87 | autorest.WithHeaders(headers)) |
| 88 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 89 | } |
| 90 | |
| 91 | // DeleteSnapshotSender sends the DeleteSnapshot request. The method will close the |
| 92 | // http.Response Body if it receives an error. |
| 93 | func (client Client) DeleteSnapshotSender(req *http.Request) (*http.Response, error) { |
| 94 | return autorest.SendWithSender(client, req, |
| 95 | azure.DoRetryWithRegistration(client.Client)) |
| 96 | } |
| 97 | |
| 98 | // DeleteSnapshotResponder handles the response to the DeleteSnapshot request. The method always |
| 99 | // closes the http.Response Body. |
| 100 | func (client Client) DeleteSnapshotResponder(resp *http.Response) (result autorest.Response, err error) { |
| 101 | err = autorest.Respond( |
| 102 | resp, |
| 103 | client.ByInspecting(), |
| 104 | azure.WithErrorUnlessStatusCode(http.StatusAccepted), |
| 105 | autorest.ByClosing()) |
| 106 | result = autorest.Response{Response: resp} |
| 107 | return |
| 108 | } |