From dc698e3b67b9bf757cebfadcd640adae5e41fcfd Mon Sep 17 00:00:00 2001 From: Michael Austin Date: Wed, 20 May 2015 19:06:27 -0400 Subject: [PATCH] added force_destroy argument to s3 bucket provider commit a92fe29b909af033c4c57257ddcb6793bfb694aa Author: Michael Austin Date: Wed May 20 16:35:38 2015 -0400 updated to new style of awserr commit 428271c9b9ca01ed2add1ffa608ab354f520bfa0 Merge: b3bae0e 883e284 Author: Michael Austin Date: Wed May 20 16:29:00 2015 -0400 Merge branch 'master' into 2544-terraform-s3-forceDelete commit b3bae0efdac81adf8bb448d11cc1ca62eae75d94 Author: Michael Austin Date: Wed May 20 12:06:36 2015 -0400 removed extra line commit 85eb40fc7ce24f5eb01af10eadde35ebac3c8223 Author: Michael Austin Date: Tue May 19 14:27:19 2015 -0400 stray [ commit d8a405f7d6880c350ab9fccb70b833d2239d9915 Author: Michael Austin Date: Tue May 19 14:24:01 2015 -0400 addressed feedback concerning parsing of aws error in a more standard way commit 5b9a5ee613af78e466d89ba772959bb38566f50e Author: Michael Austin Date: Tue May 19 10:55:22 2015 -0400 clarify comment to highlight recursion commit 91043781f4ba08b075673cd4c7c01792975c2402 Author: Michael Austin Date: Tue May 19 10:51:13 2015 -0400 addressed feedback about reusing err variable and unneeded parens commit 95e9c3afbd34d4d09a6355b0aaeb52606917b6dc Merge: 2637edf db095e2 Author: Michael Austin Date: Mon May 18 19:15:36 2015 -0400 Merge branch 'master' into 2544-terraform-s3-forceDelete commit 2637edfc48a23b2951032b1e974d7097602c4715 Author: Michael Austin Date: Fri May 15 15:12:41 2015 -0400 optimize delete to delete up to 1000 at once instead of one at a time commit 1441eb2ccf13fa34f4d8c43257c2e471108738e4 Author: Michael Austin Date: Fri May 15 12:34:53 2015 -0400 Revert "hook new resource provider into configuration" This reverts commit e14a1ade5315e3276e039b745a40ce69a64518b5. commit b532fa22022e34e4a8ea09024874bb0e8265f3ac Author: Michael Austin Date: Fri May 15 12:34:49 2015 -0400 this file should not be in this branch commit 645c0b66c6f000a6da50ebeca1d867a63e5fd9f1 Author: Michael Austin Date: Thu May 14 21:15:29 2015 -0400 buckets tagged force_destroy will delete all files and then delete buckets commit ac50cae214ce88e22bb1184386c56b8ba8c057f7 Author: Michael Austin Date: Thu May 14 12:41:40 2015 -0400 added code to delete policy from s3 bucket commit cd45e45d6d04a3956fe35c178d5e816ba18d1051 Author: Michael Austin Date: Thu May 14 12:27:13 2015 -0400 added code to read bucket policy from bucket, however, it's not working as expected currently commit 0d3d51abfddec9c39c60d8f7b81e8fcd88e117b9 Merge: 31ffdea 8a3b75d Author: Michael Austin Date: Thu May 14 08:38:06 2015 -0400 Merge remote-tracking branch 'hashi_origin/master' into 2544-terraform-s3-policy commit 31ffdea96ba3d5ddf5d42f862e68c1c133e49925 Author: Michael Austin Date: Wed May 13 16:01:52 2015 -0400 add name for use with resouce id commit b41c7375dbd9ae43ee0d421cf2432c1eb174b5b0 Author: Michael Austin Date: Wed May 13 14:48:24 2015 -0400 Revert "working policy assignment" This reverts commit 0975a70c37eaa310d2bdfe6f77009253c5e450c7. commit b926b11521878f1527bdcaba3c1b7c0b973e89e5 Author: Michael Austin Date: Wed May 13 14:35:02 2015 -0400 moved policy to it's own provider commit 233a5f443c13d71f3ddc06cf034d07cb8231b4dd Merge: e14a1ad c003e96 Author: Michael Austin Date: Wed May 13 12:39:14 2015 -0400 merged origin/master commit e14a1ade5315e3276e039b745a40ce69a64518b5 Author: Michael Austin Date: Wed May 13 12:26:51 2015 -0400 hook new resource provider into configuration commit 455b409cb853faae3e45a0a3d4e2859ffc4ed865 Author: Michael Austin Date: Wed May 13 12:26:15 2015 -0400 dummy resource provider commit 0975a70c37eaa310d2bdfe6f77009253c5e450c7 Author: Michael Austin Date: Wed May 13 09:42:31 2015 -0400 working policy assignment commit 3ab901d6b3ab605adc0a8cb703aa047a513b68d4 Author: Michael Austin Date: Tue May 12 10:39:56 2015 -0400 added policy string to schema --- .../providers/aws/resource_aws_s3_bucket.go | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 142b800694..1c1de96ed9 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -85,6 +85,12 @@ func resourceAwsS3Bucket() *schema.Resource { }, "tags": tagsSchema(), + + "force_destroy": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: false, + }, }, } } @@ -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 }