Skip to content

Commit

Permalink
Simplify bucket emptying mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2b3bfa0 authored Mar 30, 2022
1 parent cd41ef3 commit ad66fcd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 63 deletions.
47 changes: 5 additions & 42 deletions task/aws/resources/resource_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,54 +78,17 @@ func (b *Bucket) Update(ctx context.Context) error {
}

func (b *Bucket) Delete(ctx context.Context) error {
listInput := s3.ListObjectsV2Input{
input := s3.DeleteBucketInput{
Bucket: aws.String(b.Identifier),
}

for paginator := s3.NewListObjectsV2Paginator(b.Client.Services.S3, &listInput); paginator.HasMorePages(); {
page, err := paginator.NextPage(ctx)

if err != nil {
var e smithy.APIError
if errors.As(err, &e) && e.ErrorCode() == "NoSuchBucket" {
b.Resource = nil
return nil
}
return err
}

if len(page.Contents) == 0 {
break
}

var objects []types.ObjectIdentifier
for _, object := range page.Contents {
objects = append(objects, types.ObjectIdentifier{
Key: object.Key,
})
}

input := s3.DeleteObjectsInput{
Bucket: aws.String(b.Identifier),
Delete: &types.Delete{
Objects: objects,
},
}

if _, err = b.Client.Services.S3.DeleteObjects(ctx, &input); err != nil {

if _, err := b.Client.Services.S3.DeleteBucket(ctx, &input); err != nil {
var e smithy.APIError
if errors.As(err, &e) && e.ErrorCode() != "NoSuchBucket" {
return err
}
}

deleteInput := s3.DeleteBucketInput{
Bucket: aws.String(b.Identifier),
}

_, err := b.Client.Services.S3.DeleteBucket(ctx, &deleteInput)
if err != nil {
return err
}

b.Resource = nil
return nil
}
2 changes: 1 addition & 1 deletion task/aws/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (t *Task) Read(ctx context.Context) error {

func (t *Task) Delete(ctx context.Context) error {
logrus.Debug("Downloading Directory...")
if t.Read(ctx) == nil {
if t.Resources.Bucket.Read(ctx) == nil {
if t.Attributes.Environment.DirectoryOut != "" {
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
return err
Expand Down
2 changes: 1 addition & 1 deletion task/az/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (t *Task) Read(ctx context.Context) error {

func (t *Task) Delete(ctx context.Context) error {
logrus.Debug("Downloading Directory...")
if t.Read(ctx) == nil {
if t.Resources.BlobContainer.Read(ctx) == nil {
if t.Attributes.Environment.DirectoryOut != "" {
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
return err
Expand Down
22 changes: 4 additions & 18 deletions task/gcp/resources/resource_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,11 @@ func (b *Bucket) Update(ctx context.Context) error {
}

func (b *Bucket) Delete(ctx context.Context) error {
if b.Read(ctx) == common.NotFoundError {
return nil
}

deletePage := func(objects *storage.Objects) error {
for _, object := range objects.Items {
if err := b.Client.Services.Storage.Objects.Delete(b.Identifier, object.Name).Do(); err != nil {
return err
}
}
return nil
}

if err := b.Client.Services.Storage.Objects.List(b.Identifier).Pages(ctx, deletePage); err != nil {
return err
}

if err := b.Client.Services.Storage.Buckets.Delete(b.Identifier).Do(); err != nil {
return err
var e *googleapi.Error
if errors.As(err, &e) && e.Code != 404 {
return err
}
}

b.Resource = nil
Expand Down
2 changes: 1 addition & 1 deletion task/gcp/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (t *Task) Read(ctx context.Context) error {

func (t *Task) Delete(ctx context.Context) error {
logrus.Debug("Downloading Directory...")
if t.Read(ctx) == nil {
if t.Resources.Bucket.Read(ctx) == nil {
if t.Attributes.Environment.DirectoryOut != "" {
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
return err
Expand Down

0 comments on commit ad66fcd

Please sign in to comment.