Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: path fixes for files/file, retryfunc for directory deletion #113

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions storage/2023-11-03/file/directories/delete.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package directories

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

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)

type DeleteResponse struct {
Expand All @@ -32,6 +37,30 @@ func (c Client) Delete(ctx context.Context, shareName, path string) (result Dele
return
}

// Retry the directory deletion if the directory is not empty (deleted files take a little while to disappear)
retryFunc := func(resp *http.Response, _ *odata.OData) (bool, error) {
if resp != nil {
if response.WasStatusCode(resp, 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, "DirectoryNotEmpty"), nil
}
}
}
return false, nil
}

opts := client.RequestOptions{
ContentType: "application/xml; charset=utf-8",
ExpectedStatusCodes: []int{
Expand All @@ -40,6 +69,7 @@ func (c Client) Delete(ctx context.Context, shareName, path string) (result Dele
HttpMethod: http.MethodDelete,
OptionsObject: directoriesOptions{},
Path: fmt.Sprintf("/%s/%s", shareName, path),
RetryFunc: retryFunc,
}

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

import "encoding/xml"

type ErrorResponse struct {
XMLName xml.Name `xml:"Error"`
Code *string `xml:"Code"`
Message *string `xml:"Message"`
}
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c Client) Delete(ctx context.Context, shareName, path, fileName string) (r
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/metadata_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c Client) SetMetaData(ctx context.Context, shareName, path, fileName strin
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/properties_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c Client) GetProperties(ctx context.Context, shareName, path, fileName str
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/properties_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c Client) SetProperties(ctx context.Context, shareName, path, fileName str
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/files/range_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c Client) PutByteRange(ctx context.Context, shareName, path, fileName stri
}

if path != "" {
path = fmt.Sprintf("/%s/", path)
path = fmt.Sprintf("%s/", path)
}

opts := client.RequestOptions{
Expand Down
10 changes: 7 additions & 3 deletions storage/2023-11-03/file/files/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func NewFileID(accountId accounts.AccountId, shareName, directoryPath, fileName
}

func (b FileId) ID() string {
return fmt.Sprintf("%s/%s/%s/%s", b.AccountId.ID(), b.ShareName, b.DirectoryPath, b.FileName)
path := ""
if b.DirectoryPath != "" {
path = fmt.Sprintf("%s/", b.DirectoryPath)
}
return fmt.Sprintf("%s/%s/%s%s", b.AccountId.ID(), b.ShareName, path, b.FileName)
}

func (b FileId) String() string {
Expand Down Expand Up @@ -72,8 +76,8 @@ func ParseFileID(input, domainSuffix string) (*FileId, error) {

path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) < 3 {
return nil, fmt.Errorf("expected the path to contain at least 3 segments but got %d", len(segments))
if len(segments) < 2 {
return nil, fmt.Errorf("expected the path to contain at least 2 segments but got %d", len(segments))
}
shareName := segments[0]
directoryPath := strings.Join(segments[1:len(segments)-1], "/")
Expand Down
Loading