Skip to content

Commit

Permalink
Improve unmarshaling for binary payloads, implement retries when crea…
Browse files Browse the repository at this point in the history
…ting file shares
  • Loading branch information
manicminer committed Feb 20, 2024
1 parent e03fa0e commit c1e9de0
Show file tree
Hide file tree
Showing 36 changed files with 149 additions and 83 deletions.
4 changes: 2 additions & 2 deletions storage/2020-08-04/blob/accounts/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Blob Storage Blobs.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/blob/blobs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Blob Storage Blobs.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/blob/containers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Blob Storage Containers.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/datalakestore/filesystems/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Data Lake Store Filesystems.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/datalakestore/paths/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

// Client is the base client for Data Lake Storage Path
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/file/directories/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for File Storage Shares.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/file/files/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for File Storage Shares.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/file/shares/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for File Storage Shares.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/queue/messages/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Messages.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/queue/queues/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

// Client is the base client for Messages.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/table/entities/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Table Storage Shares.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2020-08-04/table/tables/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Table Storage Shares.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/2023-11-03/blob/accounts/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Blob Storage Blobs.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
10 changes: 4 additions & 6 deletions storage/2023-11-03/blob/blobs/append_block.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package blobs

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -96,12 +94,12 @@ func (c Client) AppendBlock(ctx context.Context, containerName, blobName string,
return
}

if input.Content != nil {
req.Body = io.NopCloser(bytes.NewReader(*input.Content))
err = req.Marshal(input.Content)
if err != nil {
err = fmt.Errorf("marshalling request: %+v", err)
return
}

req.ContentLength = int64(len(*input.Content))

var resp *client.Response
resp, err = req.Execute(ctx)
if resp != nil {
Expand Down
4 changes: 2 additions & 2 deletions storage/2023-11-03/blob/blobs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Blob Storage Blobs.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions storage/2023-11-03/blob/blobs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type GetInput struct {
type GetResponse struct {
HttpResponse *http.Response

Contents []byte
Contents *[]byte
}

// Get reads or downloads a blob from the system, including its metadata and properties.
Expand Down Expand Up @@ -65,9 +65,10 @@ func (c Client) Get(ctx context.Context, containerName, blobName string, input G
var resp *client.Response
resp, err = req.Execute(ctx)
if resp != nil {
result.Contents = &[]byte{}
result.HttpResponse = resp.Response

err = resp.Unmarshal(&result.Contents)
err = resp.Unmarshal(result.Contents)
if err != nil {
err = fmt.Errorf("unmarshalling response: %+v", err)
return
Expand Down
12 changes: 5 additions & 7 deletions storage/2023-11-03/blob/blobs/put_block.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package blobs

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
Expand All @@ -21,8 +23,7 @@ type PutBlockInput struct {

type PutBlockResponse struct {
HttpResponse *http.Response

ContentMD5 string
ContentMD5 string
}

// PutBlock creates a new block to be committed as part of a blob.
Expand Down Expand Up @@ -69,11 +70,8 @@ func (c Client) PutBlock(ctx context.Context, containerName, blobName string, in
return
}

err = req.Marshal(&input.Content)
if err != nil {
err = fmt.Errorf("marshalling request: %+v", err)
return
}
req.ContentLength = int64(len(input.Content))
req.Body = io.NopCloser(bytes.NewReader(input.Content))

var resp *client.Response
resp, err = req.Execute(ctx)
Expand Down
6 changes: 5 additions & 1 deletion storage/2023-11-03/blob/blobs/put_block_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ func (c Client) PutBlockBlob(ctx context.Context, containerName, blobName string
Path: fmt.Sprintf("/%s/%s", containerName, blobName),
}

if input.ContentType != nil {
opts.ContentType = *input.ContentType
}

req, err := c.Client.NewRequest(ctx, opts)
if err != nil {
err = fmt.Errorf("building request: %+v", err)
return
}

err = req.Marshal(&input.Content)
err = req.Marshal(input.Content)
if err != nil {
err = fmt.Errorf("marshalling request: %+v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions storage/2023-11-03/blob/containers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Client is the base client for Blob Storage Containers.
type Client struct {
Client *storage.BaseClient
Client *storage.Client
}

func NewWithBaseUri(baseUri string) (*Client, error) {
baseClient, err := storage.NewBaseClient(baseUri, componentName, apiVersion)
baseClient, err := storage.NewStorageClient(baseUri, componentName, apiVersion)
if err != nil {
return nil, fmt.Errorf("building base client: %+v", err)
}
Expand Down
32 changes: 30 additions & 2 deletions storage/2023-11-03/blob/containers/create.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package containers

import (
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"strings"

"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
Expand All @@ -26,7 +30,6 @@ type CreateInput struct {

type CreateResponse struct {
HttpResponse *http.Response
Error *ErrorResponse `xml:"Error"`
}

// Create creates a new container under the specified account.
Expand All @@ -41,6 +44,30 @@ func (c Client) Create(ctx context.Context, containerName string, input CreateIn
return
}

// Retry the container creation if a conflicting container is still in the process of being deleted
retryFunc := func(resp *http.Response, _ *odata.OData) (bool, error) {
if resp != nil {
if resp.StatusCode == http.StatusConflict {
// TODO: move this error response parsing to a common helper function
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("could not parse response body")
}
resp.Body.Close()
respBody = bytes.TrimPrefix(respBody, []byte("\xef\xbb\xbf"))
res := ErrorResponse{}
if err = xml.Unmarshal(respBody, &res); err != nil {
return false, err
}
resp.Body = io.NopCloser(bytes.NewBuffer(respBody))
if res.Code != nil {
return strings.Contains(*res.Code, "ContainerBeingDeleted"), nil
}
}
}
return false, nil
}

opts := client.RequestOptions{
ContentType: "application/xml; charset=utf-8",
ExpectedStatusCodes: []int{
Expand All @@ -50,7 +77,8 @@ func (c Client) Create(ctx context.Context, containerName string, input CreateIn
OptionsObject: createOptions{
input: input,
},
Path: fmt.Sprintf("/%s", containerName),
Path: fmt.Sprintf("/%s", containerName),
RetryFunc: retryFunc,
}

req, err := c.Client.NewRequest(ctx, opts)
Expand Down
7 changes: 5 additions & 2 deletions storage/2023-11-03/blob/containers/models.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package containers

import "encoding/xml"

type AccessLevel string

var (
Expand Down Expand Up @@ -41,8 +43,9 @@ var (
)

type ErrorResponse struct {
Code *string `xml:"Code"`
Message *string `xml:"Message"`
XMLName xml.Name `xml:"Error"`
Code *string `xml:"Code"`
Message *string `xml:"Message"`
}

type LeaseDuration string
Expand Down
Loading

0 comments on commit c1e9de0

Please sign in to comment.