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

provider/aws: force_destroy argument for s3 buckets with objects #2007

Merged
merged 2 commits into from
May 21, 2015
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
47 changes: 46 additions & 1 deletion builtin/providers/aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func resourceAwsS3Bucket() *schema.Resource {
},

"tags": tagsSchema(),

"force_destroy": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand Down Expand Up @@ -259,7 +265,46 @@ func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
Bucket: aws.String(d.Id()),
})
if err != nil {
return err
ec2err, ok := err.(awserr.Error)
if ok && ec2err.Code() == "BucketNotEmpty" {
if d.Get("force_destroy").(bool) {
// bucket may have things delete them
log.Printf("[DEBUG] S3 Bucket attempting to forceDestroy %+v", err)

bucket := d.Get("bucket").(string)
resp, err := s3conn.ListObjects(
&s3.ListObjectsInput{
Bucket: aws.String(bucket),
},
)

if err != nil {
return fmt.Errorf("Error S3 Bucket list Objects err: %s", err)
}

objectsToDelete := make([]*s3.ObjectIdentifier, len(resp.Contents))
for i, v := range resp.Contents {
objectsToDelete[i] = &s3.ObjectIdentifier{
Key: v.Key,
}
}
_, err = s3conn.DeleteObjects(
&s3.DeleteObjectsInput{
Bucket: aws.String(bucket),
Delete: &s3.Delete{
Objects: objectsToDelete,
},
},
)
if err != nil {
return fmt.Errorf("Error S3 Bucket force_destroy error deleting: %s", err)
}

// this line recurses until all objects are deleted or an error is returned
return resourceAwsS3BucketDelete(d, meta)
}
}
return fmt.Errorf("Error deleting S3 Bucket: %s", err)
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The following arguments are supported:
* `acl` - (Optional) The [canned ACL](http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Defaults to "private".
* `policy` - (Optional) A valid [bucket policy](http://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html) JSON document.
* `tags` - (Optional) A mapping of tags to assign to the bucket.
* `force_destroy` - (Optional, Default:false ) A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error. These objects are *not* recoverable.
* `website` - (Optional) A website object (documented below).

The website object supports the following:
Expand Down