Skip to content

Commit b47f801

Browse files
Empty buckets during Delete() (#420)
* Empty buckets during Delete() * Restyled by gofmt * Restyled by gofmt * Add bucket deletion waiting mechanism * Revert Google Cloud Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 9fc440f commit b47f801

File tree

9 files changed

+61
-49
lines changed

9 files changed

+61
-49
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ terraform
4141
# Ignore the Go vendor directory, superseded by proxy.golang.org and go.{mod,sum}
4242
vendor
4343

44-
./terraform-provider-iterative
44+
terraform-provider-iterative

task/aws/resources/resource_auto_scaling_group.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"context"
55
"errors"
6+
"log"
67
"net"
78
"strconv"
89
"time"
@@ -133,6 +134,7 @@ func (a *AutoScalingGroup) Read(ctx context.Context) error {
133134
if instance.StateReason != nil {
134135
status += " " + aws.ToString(instance.StateReason.Message)
135136
}
137+
log.Println("[DEBUG] AutoScaling Group State:", status)
136138
if status == "running" {
137139
a.Attributes.Status[common.StatusCodeActive]++
138140
}

task/aws/resources/resource_bucket.go

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -76,54 +76,17 @@ func (b *Bucket) Update(ctx context.Context) error {
7676
}
7777

7878
func (b *Bucket) Delete(ctx context.Context) error {
79-
listInput := s3.ListObjectsV2Input{
79+
input := s3.DeleteBucketInput{
8080
Bucket: aws.String(b.Identifier),
8181
}
8282

83-
for paginator := s3.NewListObjectsV2Paginator(b.Client.Services.S3, &listInput); paginator.HasMorePages(); {
84-
page, err := paginator.NextPage(ctx)
85-
86-
if err != nil {
87-
var e smithy.APIError
88-
if errors.As(err, &e) && e.ErrorCode() == "NoSuchBucket" {
89-
b.Resource = nil
90-
return nil
91-
}
92-
return err
93-
}
94-
95-
if len(page.Contents) == 0 {
96-
break
97-
}
98-
99-
var objects []types.ObjectIdentifier
100-
for _, object := range page.Contents {
101-
objects = append(objects, types.ObjectIdentifier{
102-
Key: object.Key,
103-
})
104-
}
105-
106-
input := s3.DeleteObjectsInput{
107-
Bucket: aws.String(b.Identifier),
108-
Delete: &types.Delete{
109-
Objects: objects,
110-
},
111-
}
112-
113-
if _, err = b.Client.Services.S3.DeleteObjects(ctx, &input); err != nil {
83+
if _, err := b.Client.Services.S3.DeleteBucket(ctx, &input); err != nil {
84+
var e smithy.APIError
85+
if errors.As(err, &e) && e.ErrorCode() != "NoSuchBucket" {
11486
return err
11587
}
11688
}
11789

118-
deleteInput := s3.DeleteBucketInput{
119-
Bucket: aws.String(b.Identifier),
120-
}
121-
122-
_, err := b.Client.Services.S3.DeleteBucket(ctx, &deleteInput)
123-
if err != nil {
124-
return err
125-
}
126-
12790
b.Resource = nil
12891
return nil
12992
}

task/aws/task.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,14 @@ func (t *Task) Read(ctx context.Context) error {
196196

197197
func (t *Task) Delete(ctx context.Context) error {
198198
logrus.Debug("Downloading Directory...")
199-
if t.Attributes.Environment.DirectoryOut != "" && t.Read(ctx) == nil {
200-
if err := t.Pull(ctx, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
199+
if t.Read(ctx) == nil {
200+
if t.Attributes.Environment.DirectoryOut != "" {
201+
if err := t.Pull(ctx, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
202+
return err
203+
}
204+
}
205+
logrus.Debug("Emptying Bucket...")
206+
if err := machine.Delete(ctx, (*t.DataSources.Credentials.Resource)["RCLONE_REMOTE"]); err != nil && err != common.NotFoundError {
201207
return err
202208
}
203209
}

task/az/resources/resource_virtual_machine_scale_set.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"errors"
77
"fmt"
8+
"log"
89
"net"
910
"regexp"
1011
"time"
@@ -243,6 +244,7 @@ func (v *VirtualMachineScaleSet) Read(ctx context.Context) error {
243244
if scaleSetView.VirtualMachine.StatusesSummary != nil {
244245
for _, status := range *scaleSetView.VirtualMachine.StatusesSummary {
245246
code := to.String(status.Code)
247+
log.Println("[DEBUG] ScaleSet Status Summary:", code, int(to.Int32(status.Count)))
246248
if code == "ProvisioningState/succeeded" {
247249
v.Attributes.Status[common.StatusCodeActive] = int(to.Int32(status.Count))
248250
}

task/az/task.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,14 @@ func (t *Task) Read(ctx context.Context) error {
185185

186186
func (t *Task) Delete(ctx context.Context) error {
187187
logrus.Debug("Downloading Directory...")
188-
if t.Attributes.Environment.DirectoryOut != "" && t.Read(ctx) == nil {
189-
if err := t.Pull(ctx, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
188+
if t.Read(ctx) == nil {
189+
if t.Attributes.Environment.DirectoryOut != "" {
190+
if err := t.Pull(ctx, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
191+
return err
192+
}
193+
}
194+
logrus.Debug("Emptying Bucket...")
195+
if err := machine.Delete(ctx, (*t.DataSources.Credentials.Resource)["RCLONE_REMOTE"]); err != nil && err != common.NotFoundError {
190196
return err
191197
}
192198
}

task/common/machine/storage.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"io"
89
"path/filepath"
910
"strings"
@@ -14,6 +15,7 @@ import (
1415
_ "github.com/rclone/rclone/backend/s3"
1516

1617
"github.com/rclone/rclone/fs"
18+
"github.com/rclone/rclone/fs/operations"
1719
"github.com/rclone/rclone/fs/sync"
1820

1921
"terraform-provider-iterative/task/common"
@@ -99,9 +101,32 @@ func Transfer(ctx context.Context, source, destination string) error {
99101
return err
100102
}
101103

102-
if err := sync.CopyDir(ctx, destinationFileSystem, sourceFileSystem, true); err != nil {
104+
return sync.CopyDir(ctx, destinationFileSystem, sourceFileSystem, true)
105+
}
106+
107+
func Delete(ctx context.Context, destination string) error {
108+
destinationFileSystem, err := fs.NewFs(ctx, destination)
109+
if err != nil {
103110
return err
104111
}
105112

113+
actions := []func(context.Context) error{
114+
func(ctx context.Context) error {
115+
return operations.Delete(ctx, destinationFileSystem)
116+
},
117+
func(ctx context.Context) error {
118+
return operations.Rmdirs(ctx, destinationFileSystem, "", true)
119+
},
120+
}
121+
122+
for _, action := range actions {
123+
if err := action(ctx); err != nil {
124+
if !errors.Is(err, fs.ErrorDirNotFound) && !strings.Contains(err.Error(), "no such host") {
125+
return common.NotFoundError
126+
}
127+
return err
128+
}
129+
}
130+
106131
return nil
107132
}

task/gcp/resources/resource_instance_group_manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"context"
55
"errors"
6+
"log"
67
"net"
78
"path/filepath"
89
"strings"
@@ -77,6 +78,7 @@ func (i *InstanceGroupManager) Read(ctx context.Context) error {
7778
i.Attributes.Addresses = []net.IP{}
7879
i.Attributes.Status = common.Status{common.StatusCodeActive: 0}
7980
for _, groupInstance := range groupInstances.Items {
81+
log.Println("[DEBUG] Instance Group Manager Status:", groupInstance.Status)
8082
if groupInstance.Status == "RUNNING" {
8183
instance, err := i.Client.Services.Compute.Instances.Get(i.Client.Credentials.ProjectID, i.Client.Region, filepath.Base(groupInstance.Instance)).Do()
8284
if err != nil {

task/gcp/task.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,14 @@ func (t *Task) Read(ctx context.Context) error {
264264

265265
func (t *Task) Delete(ctx context.Context) error {
266266
logrus.Debug("Downloading Directory...")
267-
if t.Attributes.Environment.DirectoryOut != "" && t.Read(ctx) == nil {
268-
if err := t.Pull(ctx, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
267+
if t.Read(ctx) == nil {
268+
if t.Attributes.Environment.DirectoryOut != "" {
269+
if err := t.Pull(ctx, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
270+
return err
271+
}
272+
}
273+
logrus.Debug("Emptying Bucket...")
274+
if err := machine.Delete(ctx, (*t.DataSources.Credentials.Resource)["RCLONE_REMOTE"]); err != nil && err != common.NotFoundError {
269275
return err
270276
}
271277
}

0 commit comments

Comments
 (0)