1package entities
2
3import (
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
13type DeleteEntityInput struct {
14	// When inserting an entity into a table, you must specify values for the PartitionKey and RowKey system properties.
15	// Together, these properties form the primary key and must be unique within the table.
16	// Both the PartitionKey and RowKey values must be string values; each key value may be up to 64 KB in size.
17	// If you are using an integer value for the key value, you should convert the integer to a fixed-width string,
18	// because they are canonically sorted. For example, you should convert the value 1 to 0000001 to ensure proper sorting.
19	RowKey       string
20	PartitionKey string
21}
22
23// Delete deletes an existing entity in a table.
24func (client Client) Delete(ctx context.Context, accountName, tableName string, input DeleteEntityInput) (result autorest.Response, err error) {
25	if accountName == "" {
26		return result, validation.NewError("entities.Client", "Delete", "`accountName` cannot be an empty string.")
27	}
28	if tableName == "" {
29		return result, validation.NewError("entities.Client", "Delete", "`tableName` cannot be an empty string.")
30	}
31	if input.PartitionKey == "" {
32		return result, validation.NewError("entities.Client", "Delete", "`input.PartitionKey` cannot be an empty string.")
33	}
34	if input.RowKey == "" {
35		return result, validation.NewError("entities.Client", "Delete", "`input.RowKey` cannot be an empty string.")
36	}
37
38	req, err := client.DeletePreparer(ctx, accountName, tableName, input)
39	if err != nil {
40		err = autorest.NewErrorWithError(err, "entities.Client", "Delete", nil, "Failure preparing request")
41		return
42	}
43
44	resp, err := client.DeleteSender(req)
45	if err != nil {
46		result = autorest.Response{Response: resp}
47		err = autorest.NewErrorWithError(err, "entities.Client", "Delete", resp, "Failure sending request")
48		return
49	}
50
51	result, err = client.DeleteResponder(resp)
52	if err != nil {
53		err = autorest.NewErrorWithError(err, "entities.Client", "Delete", resp, "Failure responding to request")
54		return
55	}
56
57	return
58}
59
60// DeletePreparer prepares the Delete request.
61func (client Client) DeletePreparer(ctx context.Context, accountName, tableName string, input DeleteEntityInput) (*http.Request, error) {
62	pathParameters := map[string]interface{}{
63		"tableName":    autorest.Encode("path", tableName),
64		"partitionKey": autorest.Encode("path", input.PartitionKey),
65		"rowKey":       autorest.Encode("path", input.RowKey),
66	}
67
68	headers := map[string]interface{}{
69		// TODO: support for eTags
70		"If-Match": "*",
71	}
72
73	preparer := autorest.CreatePreparer(
74		autorest.AsDelete(),
75		autorest.WithBaseURL(endpoints.GetTableEndpoint(client.BaseURI, accountName)),
76		autorest.WithPathParameters("/{tableName}(PartitionKey='{partitionKey}', RowKey='{rowKey}')", pathParameters),
77		autorest.WithHeaders(headers))
78	return preparer.Prepare((&http.Request{}).WithContext(ctx))
79}
80
81// DeleteSender sends the Delete request. The method will close the
82// http.Response Body if it receives an error.
83func (client Client) DeleteSender(req *http.Request) (*http.Response, error) {
84	return autorest.SendWithSender(client, req,
85		azure.DoRetryWithRegistration(client.Client))
86}
87
88// DeleteResponder handles the response to the Delete request. The method always
89// closes the http.Response Body.
90func (client Client) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
91	err = autorest.Respond(
92		resp,
93		client.ByInspecting(),
94		azure.WithErrorUnlessStatusCode(http.StatusNoContent),
95		autorest.ByClosing())
96	result = autorest.Response{Response: resp}
97
98	return
99}
100