Skip to content

Commit

Permalink
Merge pull request #33586 from k4rtikp4til/f-aws_lightsail_bucket-add…
Browse files Browse the repository at this point in the history
…_force_delete

[enhancement]: r/aws_lightsail_bucket: add force_delete attr for lightsail s3 bucket
  • Loading branch information
ewbankkit authored Sep 25, 2023
2 parents d0fd362 + 85ca6e8 commit 2d2f461
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/33586.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_lightsail_bucket: Add `force_delete` argument
```
19 changes: 15 additions & 4 deletions internal/service/lightsail/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package lightsail

import (
"context"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -30,6 +31,7 @@ func ResourceBucket() *schema.Resource {
ReadWithoutTimeout: resourceBucketRead,
UpdateWithoutTimeout: resourceBucketUpdate,
DeleteWithoutTimeout: resourceBucketDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -43,13 +45,18 @@ func ResourceBucket() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"bundle_id": {
Type: schema.TypeString,
Required: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"bundle_id": {
Type: schema.TypeString,
Required: true,
"force_delete": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"name": {
Type: schema.TypeString,
Expand All @@ -71,6 +78,7 @@ func ResourceBucket() *schema.Resource {
Computed: true,
},
},

CustomizeDiff: verify.SetTagsDiff,
}
}
Expand Down Expand Up @@ -157,8 +165,11 @@ func resourceBucketUpdate(ctx context.Context, d *schema.ResourceData, meta inte

func resourceBucketDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).LightsailClient(ctx)

log.Printf("[DEBUG] Deleting Lightsail Bucket: %s", d.Id())
out, err := conn.DeleteBucket(ctx, &lightsail.DeleteBucketInput{
BucketName: aws.String(d.Id()),
BucketName: aws.String(d.Id()),
ForceDelete: aws.Bool(d.Get("force_delete").(bool)),
})

if err != nil && errs.IsA[*types.NotFoundException](err) {
Expand Down
55 changes: 55 additions & 0 deletions internal/service/lightsail/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ func TestAccLightsailBucket_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "support_code"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttrSet(resourceName, "url"),
resource.TestCheckResourceAttrSet(resourceName, "force_delete"),
resource.TestCheckResourceAttr(resourceName, "force_delete", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"force_delete",
},
},
},
})
Expand Down Expand Up @@ -90,6 +95,9 @@ func TestAccLightsailBucket_BundleId(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"force_delete",
},
},
{
Config: testAccBucketConfig_bundleId(rName, bundle2),
Expand Down Expand Up @@ -156,6 +164,9 @@ func TestAccLightsailBucket_tags(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"force_delete",
},
},
{
Config: testAccBucketConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Expand Down Expand Up @@ -231,6 +242,40 @@ func testAccCheckBucketDestroy(ctx context.Context) resource.TestCheckFunc {
}
}

func TestAccLightsailBucket_forceDelete(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lightsail_bucket.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, strings.ToLower(lightsail.ServiceID))
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, strings.ToLower(lightsail.ServiceID)),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckBucketDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccBucketConfig_forceDelete(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBucketExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "force_delete", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"force_delete",
},
},
},
})
}

func testAccBucketConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_lightsail_bucket" "test" {
Expand Down Expand Up @@ -273,3 +318,13 @@ resource "aws_lightsail_bucket" "test" {
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccBucketConfig_forceDelete(rName string) string {
return fmt.Sprintf(`
resource "aws_lightsail_bucket" "test" {
name = %[1]q
bundle_id = "small_1_0"
force_delete = true
}
`, rName)
}
1 change: 1 addition & 0 deletions website/docs/r/lightsail_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This resource supports the following arguments:

* `name` - (Required) The name for the bucket.
* `bundle_id` - (Required) - The ID of the bundle to use for the bucket. A bucket bundle specifies the monthly cost, storage space, and data transfer quota for a bucket. Use the [get-bucket-bundles](https://docs.aws.amazon.com/cli/latest/reference/lightsail/get-bucket-bundles.html) cli command to get a list of bundle IDs that you can specify.
* `force_delete` - (Optional) - Force Delete non-empty buckets using `terraform destroy`. AWS by default will not delete an s3 bucket which is not empty, to prevent losing bucket data and affecting other resources in lightsail. If `force_delete` is set to `true` the bucket will be deleted even when not empty.
* `tags` - (Optional) A map of tags to assign to the resource. To create a key-only tag, use an empty string as the value. If configured with a provider `default_tags` configuration block present, tags with matching keys will overwrite those defined at the provider-level.

## Attribute Reference
Expand Down

0 comments on commit 2d2f461

Please sign in to comment.