Skip to content

Commit ad66fcd

Browse files
authored
Simplify bucket emptying mechanism
1 parent cd41ef3 commit ad66fcd

File tree

5 files changed

+12
-63
lines changed

5 files changed

+12
-63
lines changed

task/aws/resources/resource_bucket.go

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -78,54 +78,17 @@ func (b *Bucket) Update(ctx context.Context) error {
7878
}
7979

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

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

task/aws/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ 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.Read(ctx) == nil {
199+
if t.Resources.Bucket.Read(ctx) == nil {
200200
if t.Attributes.Environment.DirectoryOut != "" {
201201
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
202202
return err

task/az/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ 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.Read(ctx) == nil {
188+
if t.Resources.BlobContainer.Read(ctx) == nil {
189189
if t.Attributes.Environment.DirectoryOut != "" {
190190
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
191191
return err

task/gcp/resources/resource_bucket.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,11 @@ func (b *Bucket) Update(ctx context.Context) error {
6161
}
6262

6363
func (b *Bucket) Delete(ctx context.Context) error {
64-
if b.Read(ctx) == common.NotFoundError {
65-
return nil
66-
}
67-
68-
deletePage := func(objects *storage.Objects) error {
69-
for _, object := range objects.Items {
70-
if err := b.Client.Services.Storage.Objects.Delete(b.Identifier, object.Name).Do(); err != nil {
71-
return err
72-
}
73-
}
74-
return nil
75-
}
76-
77-
if err := b.Client.Services.Storage.Objects.List(b.Identifier).Pages(ctx, deletePage); err != nil {
78-
return err
79-
}
80-
8164
if err := b.Client.Services.Storage.Buckets.Delete(b.Identifier).Do(); err != nil {
82-
return err
65+
var e *googleapi.Error
66+
if errors.As(err, &e) && e.Code != 404 {
67+
return err
68+
}
8369
}
8470

8571
b.Resource = nil

task/gcp/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ 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.Read(ctx) == nil {
267+
if t.Resources.Bucket.Read(ctx) == nil {
268268
if t.Attributes.Environment.DirectoryOut != "" {
269269
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
270270
return err

0 commit comments

Comments
 (0)