| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/Azure/go-autorest/autorest" |
| 10 | "github.com/Azure/go-autorest/autorest/azure" |
| 11 | "github.com/Azure/go-autorest/autorest/validation" |
| 12 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 13 | ) |
| 14 | |
| 15 | type PutPageUpdateInput struct { |
| 16 | StartByte int64 |
| 17 | EndByte int64 |
| 18 | Content []byte |
| 19 | |
| 20 | IfSequenceNumberEQ *string |
| 21 | IfSequenceNumberLE *string |
| 22 | IfSequenceNumberLT *string |
| 23 | IfModifiedSince *string |
| 24 | IfUnmodifiedSince *string |
| 25 | IfMatch *string |
| 26 | IfNoneMatch *string |
| 27 | LeaseID *string |
| 28 | } |
| 29 | |
| 30 | type PutPageUpdateResult struct { |
| 31 | autorest.Response |
| 32 | |
| 33 | BlobSequenceNumber string |
| 34 | ContentMD5 string |
| 35 | LastModified string |
| 36 | } |
| 37 | |
| 38 | // PutPageUpdate writes a range of pages to a page blob. |
| 39 | func (client Client) PutPageUpdate(ctx context.Context, accountName, containerName, blobName string, input PutPageUpdateInput) (result PutPageUpdateResult, err error) { |
| 40 | if accountName == "" { |
| 41 | return result, validation.NewError("blobs.Client", "PutPageUpdate", "`accountName` cannot be an empty string.") |
| 42 | } |
| 43 | if containerName == "" { |
| 44 | return result, validation.NewError("blobs.Client", "PutPageUpdate", "`containerName` cannot be an empty string.") |
| 45 | } |
| 46 | if strings.ToLower(containerName) != containerName { |
| 47 | return result, validation.NewError("blobs.Client", "PutPageUpdate", "`containerName` must be a lower-cased string.") |
| 48 | } |
| 49 | if blobName == "" { |
| 50 | return result, validation.NewError("blobs.Client", "PutPageUpdate", "`blobName` cannot be an empty string.") |
| 51 | } |
| 52 | if input.StartByte < 0 { |
| 53 | return result, validation.NewError("blobs.Client", "PutPageUpdate", "`input.StartByte` must be greater than or equal to 0.") |
| 54 | } |
| 55 | if input.EndByte <= 0 { |
| 56 | return result, validation.NewError("blobs.Client", "PutPageUpdate", "`input.EndByte` must be greater than 0.") |
| 57 | } |
| 58 | |
| 59 | expectedSize := (input.EndByte - input.StartByte) + 1 |
| 60 | actualSize := int64(len(input.Content)) |
| 61 | if expectedSize != actualSize { |
| 62 | return result, validation.NewError("blobs.Client", "PutPageUpdate", fmt.Sprintf("Content Size was defined as %d but got %d.", expectedSize, actualSize)) |
| 63 | } |
| 64 | |
| 65 | req, err := client.PutPageUpdatePreparer(ctx, accountName, containerName, blobName, input) |
| 66 | if err != nil { |
| 67 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutPageUpdate", nil, "Failure preparing request") |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | resp, err := client.PutPageUpdateSender(req) |
| 72 | if err != nil { |
| 73 | result.Response = autorest.Response{Response: resp} |
| 74 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutPageUpdate", resp, "Failure sending request") |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | result, err = client.PutPageUpdateResponder(resp) |
| 79 | if err != nil { |
| 80 | err = autorest.NewErrorWithError(err, "blobs.Client", "PutPageUpdate", resp, "Failure responding to request") |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | // PutPageUpdatePreparer prepares the PutPageUpdate request. |
| 88 | func (client Client) PutPageUpdatePreparer(ctx context.Context, accountName, containerName, blobName string, input PutPageUpdateInput) (*http.Request, error) { |
| 89 | pathParameters := map[string]interface{}{ |
| 90 | "containerName": autorest.Encode("path", containerName), |
| 91 | "blobName": autorest.Encode("path", blobName), |
| 92 | } |
| 93 | |
| 94 | queryParameters := map[string]interface{}{ |
| 95 | "comp": autorest.Encode("query", "page"), |
| 96 | } |
| 97 | |
| 98 | headers := map[string]interface{}{ |
| 99 | "x-ms-version": APIVersion, |
| 100 | "x-ms-page-write": "update", |
| 101 | "x-ms-range": fmt.Sprintf("bytes=%d-%d", input.StartByte, input.EndByte), |
| 102 | "Content-Length": int(len(input.Content)), |
| 103 | } |
| 104 | |
| 105 | if input.LeaseID != nil { |
| 106 | headers["x-ms-lease-id"] = *input.LeaseID |
| 107 | } |
| 108 | if input.IfSequenceNumberEQ != nil { |
| 109 | headers["x-ms-if-sequence-number-eq"] = *input.IfSequenceNumberEQ |
| 110 | } |
| 111 | if input.IfSequenceNumberLE != nil { |
| 112 | headers["x-ms-if-sequence-number-le"] = *input.IfSequenceNumberLE |
| 113 | } |
| 114 | if input.IfSequenceNumberLT != nil { |
| 115 | headers["x-ms-if-sequence-number-lt"] = *input.IfSequenceNumberLT |
| 116 | } |
| 117 | if input.IfModifiedSince != nil { |
| 118 | headers["If-Modified-Since"] = *input.IfModifiedSince |
| 119 | } |
| 120 | if input.IfUnmodifiedSince != nil { |
| 121 | headers["If-Unmodified-Since"] = *input.IfUnmodifiedSince |
| 122 | } |
| 123 | if input.IfMatch != nil { |
| 124 | headers["If-Match"] = *input.IfMatch |
| 125 | } |
| 126 | if input.IfNoneMatch != nil { |
| 127 | headers["If-None-Match"] = *input.IfNoneMatch |
| 128 | } |
| 129 | |
| 130 | preparer := autorest.CreatePreparer( |
| 131 | autorest.AsPut(), |
| 132 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 133 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 134 | autorest.WithQueryParameters(queryParameters), |
| 135 | autorest.WithHeaders(headers), |
| 136 | autorest.WithBytes(&input.Content)) |
| 137 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 138 | } |
| 139 | |
| 140 | // PutPageUpdateSender sends the PutPageUpdate request. The method will close the |
| 141 | // http.Response Body if it receives an error. |
| 142 | func (client Client) PutPageUpdateSender(req *http.Request) (*http.Response, error) { |
| 143 | return autorest.SendWithSender(client, req, |
| 144 | azure.DoRetryWithRegistration(client.Client)) |
| 145 | } |
| 146 | |
| 147 | // PutPageUpdateResponder handles the response to the PutPageUpdate request. The method always |
| 148 | // closes the http.Response Body. |
| 149 | func (client Client) PutPageUpdateResponder(resp *http.Response) (result PutPageUpdateResult, err error) { |
| 150 | if resp != nil && resp.Header != nil { |
| 151 | result.BlobSequenceNumber = resp.Header.Get("x-ms-blob-sequence-number") |
| 152 | result.ContentMD5 = resp.Header.Get("Content-MD5") |
| 153 | result.LastModified = resp.Header.Get("Last-Modified") |
| 154 | } |
| 155 | |
| 156 | err = autorest.Respond( |
| 157 | resp, |
| 158 | client.ByInspecting(), |
| 159 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 160 | autorest.ByClosing()) |
| 161 | result.Response = autorest.Response{Response: resp} |
| 162 | |
| 163 | return |
| 164 | } |