Skip to content

Commit

Permalink
Merge pull request #92 from tombuildsstuff/pollers
Browse files Browse the repository at this point in the history
Add pollers for `CopyAndWait` in `file/files` and `blobs/blob`
  • Loading branch information
catriona-m authored Nov 17, 2023
2 parents 4e57cee + 249506f commit 55cac93
Show file tree
Hide file tree
Showing 23 changed files with 250 additions and 136 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
Expand Down Expand Up @@ -85,7 +84,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand Down
3 changes: 1 addition & 2 deletions storage/2020-08-04/blob/blobs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package blobs
import (
"context"
"os"
"time"
)

type StorageBlob interface {
AppendBlock(ctx context.Context, containerName string, blobName string, input AppendBlockInput) (AppendBlockResponse, error)
Copy(ctx context.Context, containerName string, blobName string, input CopyInput) (CopyResponse, error)
AbortCopy(ctx context.Context, containerName string, blobName string, input AbortCopyInput) (CopyAbortResponse, error)
CopyAndWait(ctx context.Context, containerName string, blobName string, input CopyInput, pollingInterval time.Duration) error
CopyAndWait(ctx context.Context, containerName string, blobName string, input CopyInput) error
Delete(ctx context.Context, containerName string, blobName string, input DeleteInput) (DeleteResponse, error)
DeleteSnapshot(ctx context.Context, containerName string, blobName string, input DeleteSnapshotInput) (DeleteSnapshotResponse, error)
DeleteSnapshots(ctx context.Context, containerName string, blobName string, input DeleteSnapshotsInput) (DeleteSnapshotsResponse, error)
Expand Down
35 changes: 11 additions & 24 deletions storage/2020-08-04/blob/blobs/copy_and_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,25 @@ import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

// CopyAndWait copies a blob to a destination within the storage account and waits for it to finish copying.
func (c Client) CopyAndWait(ctx context.Context, containerName, blobName string, input CopyInput, pollingInterval time.Duration) error {
func (c Client) CopyAndWait(ctx context.Context, containerName, blobName string, input CopyInput) error {
if _, err := c.Copy(ctx, containerName, blobName, input); err != nil {
return fmt.Errorf("error copying: %s", err)
}

for true {
getInput := GetPropertiesInput{
LeaseID: input.LeaseID,
}
getResult, err := c.GetProperties(ctx, containerName, blobName, getInput)
if err != nil {
return fmt.Errorf("")
}

switch getResult.CopyStatus {
case Aborted:
return fmt.Errorf("Copy was aborted: %s", getResult.CopyStatusDescription)

case Failed:
return fmt.Errorf("Copy failed: %s", getResult.CopyStatusDescription)

case Success:
return nil
getInput := GetPropertiesInput{
LeaseID: input.LeaseID,
}

case Pending:
time.Sleep(pollingInterval)
continue
}
pollerType := NewCopyAndWaitPoller(&c, containerName, blobName, getInput)
poller := pollers.NewPoller(pollerType, 10*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow)
if err := poller.PollUntilDone(ctx); err != nil {
return fmt.Errorf("waiting for file to copy: %+v", err)
}

return fmt.Errorf("unexpected error waiting for the copy to complete")
return nil
}
48 changes: 48 additions & 0 deletions storage/2020-08-04/blob/blobs/copy_and_wait_poller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package blobs

import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

var _ pollers.PollerType = &copyAndWaitPoller{}

func NewCopyAndWaitPoller(client *Client, containerName, blobName string, getPropertiesInput GetPropertiesInput) *copyAndWaitPoller {
return &copyAndWaitPoller{
client: client,
containerName: containerName,
blobName: blobName,
getPropertiesInput: getPropertiesInput,
}
}

type copyAndWaitPoller struct {
client *Client
containerName string
blobName string
getPropertiesInput GetPropertiesInput
}

func (p *copyAndWaitPoller) Poll(ctx context.Context) (*pollers.PollResult, error) {
props, err := p.client.GetProperties(ctx, p.containerName, p.blobName, p.getPropertiesInput)
if err != nil {
return nil, fmt.Errorf("retrieving properties (container: %s blob: %s) : %+v", p.containerName, p.blobName, err)
}

if strings.EqualFold(string(props.CopyStatus), string(Success)) {
return &pollers.PollResult{
Status: pollers.PollingStatusSucceeded,
PollInterval: 10 * time.Second,
}, nil
}

// Processing
return &pollers.PollResult{
Status: pollers.PollingStatusInProgress,
PollInterval: 10 * time.Second,
}, nil
}
8 changes: 3 additions & 5 deletions storage/2020-08-04/blob/blobs/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ func TestCopyFromExistingFile(t *testing.T) {
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
}

refreshInterval := 5 * time.Second
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput, refreshInterval); err != nil {
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput); err != nil {
t.Fatalf("Error copying: %s", err)
}

t.Logf("[DEBUG] Duplicating that file..")
copiedInput := CopyInput{
CopySource: fmt.Sprintf("%s/%s/%s", baseUri, containerName, fileName),
}
if err := blobClient.CopyAndWait(ctx, containerName, copiedFileName, copiedInput, refreshInterval); err != nil {
if err := blobClient.CopyAndWait(ctx, containerName, copiedFileName, copiedInput); err != nil {
t.Fatalf("Error duplicating file: %s", err)
}

Expand Down Expand Up @@ -162,8 +161,7 @@ func TestCopyFromURL(t *testing.T) {
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
}

refreshInterval := 5 * time.Second
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput, refreshInterval); err != nil {
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput); err != nil {
t.Fatalf("Error copying: %s", err)
}

Expand Down
3 changes: 1 addition & 2 deletions storage/2020-08-04/blob/blobs/lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func TestLeaseLifecycle(t *testing.T) {
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
}

refreshInterval := 5 * time.Second
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput, refreshInterval); err != nil {
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput); err != nil {
t.Fatalf("Error copying: %s", err)
}
defer blobClient.Delete(ctx, containerName, fileName, DeleteInput{})
Expand Down
3 changes: 1 addition & 2 deletions storage/2020-08-04/blob/blobs/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func TestLifecycle(t *testing.T) {
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
}

refreshInterval := 5 * time.Second
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput, refreshInterval); err != nil {
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput); err != nil {
t.Fatalf("Error copying: %s", err)
}

Expand Down
3 changes: 1 addition & 2 deletions storage/2020-08-04/blob/blobs/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func TestSnapshotLifecycle(t *testing.T) {
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
}

refreshInterval := 5 * time.Second
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput, refreshInterval); err != nil {
if err := blobClient.CopyAndWait(ctx, containerName, fileName, copyInput); err != nil {
t.Fatalf("Error copying: %s", err)
}

Expand Down
3 changes: 1 addition & 2 deletions storage/2020-08-04/file/files/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package files
import (
"context"
"os"
"time"
)

type StorageFile interface {
Expand All @@ -21,5 +20,5 @@ type StorageFile interface {
GetProperties(ctx context.Context, shareName string, path string, fileName string) (GetResponse, error)
Delete(ctx context.Context, shareName string, path string, fileName string) (DeleteResponse, error)
Create(ctx context.Context, shareName string, path string, fileName string, input CreateInput) (CreateResponse, error)
CopyAndWait(ctx context.Context, shareName, path, fileName string, input CopyInput, pollDuration time.Duration) (CopyResponse, error)
CopyAndWait(ctx context.Context, shareName, path, fileName string, input CopyInput) (CopyResponse, error)
}
48 changes: 48 additions & 0 deletions storage/2020-08-04/file/files/copy_and_wait_poller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package files

import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

var _ pollers.PollerType = &copyAndWaitPoller{}

func NewCopyAndWaitPoller(client *Client, shareName, path, fileName string) *copyAndWaitPoller {
return &copyAndWaitPoller{
client: client,
shareName: shareName,
path: path,
fileName: fileName,
}
}

type copyAndWaitPoller struct {
client *Client
shareName string
path string
fileName string
}

func (p *copyAndWaitPoller) Poll(ctx context.Context) (*pollers.PollResult, error) {
props, err := p.client.GetProperties(ctx, p.shareName, p.path, p.fileName)
if err != nil {
return nil, fmt.Errorf("retrieving copy (shareName: %s path: %s fileName: %s) : %+v", p.shareName, p.path, p.fileName, err)
}

if strings.EqualFold(props.CopyStatus, "success") {
return &pollers.PollResult{
Status: pollers.PollingStatusSucceeded,
PollInterval: 10 * time.Second,
}, nil
}

// Processing
return &pollers.PollResult{
Status: pollers.PollingStatusInProgress,
PollInterval: 10 * time.Second,
}, nil
}
32 changes: 7 additions & 25 deletions storage/2020-08-04/file/files/copy_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package files
import (
"context"
"fmt"
"strings"
"time"
)

const DefaultCopyPollDuration = 15 * time.Second
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

// CopyAndWait is a convenience method which doesn't exist in the API, which copies the file and then waits for the copy to complete
func (c Client) CopyAndWait(ctx context.Context, shareName, path, fileName string, input CopyInput, pollDuration time.Duration) (resp CopyResponse, err error) {
func (c Client) CopyAndWait(ctx context.Context, shareName, path, fileName string, input CopyInput) (resp CopyResponse, err error) {
copy, e := c.Copy(ctx, shareName, path, fileName, input)
if err != nil {
resp.HttpResponse = copy.HttpResponse
Expand All @@ -20,27 +19,10 @@ func (c Client) CopyAndWait(ctx context.Context, shareName, path, fileName strin

resp.CopyID = copy.CopyID

// since the API doesn't return a LRO, this is a hack which also polls every 10s, but should be sufficient
for true {
props, e := c.GetProperties(ctx, shareName, path, fileName)
if e != nil {
resp.HttpResponse = copy.HttpResponse
err = fmt.Errorf("error waiting for copy: %s", e)
return
}

switch strings.ToLower(props.CopyStatus) {
case "pending":
time.Sleep(pollDuration)
continue

case "success":
return

default:
err = fmt.Errorf("Unexpected CopyState %q", e)
return
}
pollerType := NewCopyAndWaitPoller(&c, shareName, path, fileName)
poller := pollers.NewPoller(pollerType, 10*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow)
if err := poller.PollUntilDone(ctx); err != nil {
return resp, fmt.Errorf("waiting for file to copy: %+v", err)
}

return
Expand Down
6 changes: 3 additions & 3 deletions storage/2020-08-04/file/files/copy_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestFilesCopyAndWaitFromURL(t *testing.T) {
}

t.Logf("[DEBUG] Copy And Waiting..")
if _, err := filesClient.CopyAndWait(ctx, shareName, "", copiedFileName, copyInput, DefaultCopyPollDuration); err != nil {
if _, err := filesClient.CopyAndWait(ctx, shareName, "", copiedFileName, copyInput); err != nil {
t.Fatalf("Error copy & waiting: %s", err)
}

Expand Down Expand Up @@ -126,15 +126,15 @@ func TestFilesCopyAndWaitFromBlob(t *testing.T) {
CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
}
t.Logf("[DEBUG] Copy And Waiting the original file..")
if _, err := filesClient.CopyAndWait(ctx, shareName, "", originalFileName, copyInput, DefaultCopyPollDuration); err != nil {
if _, err := filesClient.CopyAndWait(ctx, shareName, "", originalFileName, copyInput); err != nil {
t.Fatalf("Error copy & waiting: %s", err)
}

t.Logf("[DEBUG] Now copying that blob..")
duplicateInput := CopyInput{
CopySource: fmt.Sprintf("%s/%s/%s", endpoints.GetFileEndpoint(*domainSuffix, accountName), shareName, originalFileName),
}
if _, err := filesClient.CopyAndWait(ctx, shareName, "", copiedFileName, duplicateInput, DefaultCopyPollDuration); err != nil {
if _, err := filesClient.CopyAndWait(ctx, shareName, "", copiedFileName, duplicateInput); err != nil {
t.Fatalf("Error copying duplicate: %s", err)
}

Expand Down
3 changes: 1 addition & 2 deletions storage/2023-11-03/blob/blobs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package blobs
import (
"context"
"os"
"time"
)

type StorageBlob interface {
AppendBlock(ctx context.Context, containerName string, blobName string, input AppendBlockInput) (AppendBlockResponse, error)
Copy(ctx context.Context, containerName string, blobName string, input CopyInput) (CopyResponse, error)
AbortCopy(ctx context.Context, containerName string, blobName string, input AbortCopyInput) (CopyAbortResponse, error)
CopyAndWait(ctx context.Context, containerName string, blobName string, input CopyInput, pollingInterval time.Duration) error
CopyAndWait(ctx context.Context, containerName string, blobName string, input CopyInput) error
Delete(ctx context.Context, containerName string, blobName string, input DeleteInput) (DeleteResponse, error)
DeleteSnapshot(ctx context.Context, containerName string, blobName string, input DeleteSnapshotInput) (DeleteSnapshotResponse, error)
DeleteSnapshots(ctx context.Context, containerName string, blobName string, input DeleteSnapshotsInput) (DeleteSnapshotsResponse, error)
Expand Down
35 changes: 11 additions & 24 deletions storage/2023-11-03/blob/blobs/copy_and_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,25 @@ import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

// CopyAndWait copies a blob to a destination within the storage account and waits for it to finish copying.
func (c Client) CopyAndWait(ctx context.Context, containerName, blobName string, input CopyInput, pollingInterval time.Duration) error {
func (c Client) CopyAndWait(ctx context.Context, containerName, blobName string, input CopyInput) error {
if _, err := c.Copy(ctx, containerName, blobName, input); err != nil {
return fmt.Errorf("error copying: %s", err)
}

for true {
getInput := GetPropertiesInput{
LeaseID: input.LeaseID,
}
getResult, err := c.GetProperties(ctx, containerName, blobName, getInput)
if err != nil {
return fmt.Errorf("")
}

switch getResult.CopyStatus {
case Aborted:
return fmt.Errorf("Copy was aborted: %s", getResult.CopyStatusDescription)

case Failed:
return fmt.Errorf("Copy failed: %s", getResult.CopyStatusDescription)

case Success:
return nil
getInput := GetPropertiesInput{
LeaseID: input.LeaseID,
}

case Pending:
time.Sleep(pollingInterval)
continue
}
pollerType := NewCopyAndWaitPoller(&c, containerName, blobName, getInput)
poller := pollers.NewPoller(pollerType, 10*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow)
if err := poller.PollUntilDone(ctx); err != nil {
return fmt.Errorf("waiting for file to copy: %+v", err)
}

return fmt.Errorf("unexpected error waiting for the copy to complete")
return nil
}
Loading

0 comments on commit 55cac93

Please sign in to comment.