Debian Codegraph @

storage/2018-11-09/blob/blobs/delete.go

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