From a4c77718697c4d1277001551dffecf0650ea9260 Mon Sep 17 00:00:00 2001 From: pjsier Date: Fri, 6 Oct 2023 19:54:45 -0400 Subject: [PATCH] Destroy object versions on bucket force_destroy --- minio/resource_minio_s3_bucket.go | 3 +- ...esource_minio_s3_bucket_versioning_test.go | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/minio/resource_minio_s3_bucket.go b/minio/resource_minio_s3_bucket.go index 2b1ea7b8..a754a0b6 100644 --- a/minio/resource_minio_s3_bucket.go +++ b/minio/resource_minio_s3_bucket.go @@ -220,7 +220,8 @@ func minioDeleteBucket(ctx context.Context, d *schema.ResourceData, meta interfa // List all objects from a bucket-name with a matching prefix. for object := range bucketConfig.MinioClient.ListObjects(ctx, d.Id(), minio.ListObjectsOptions{ - Recursive: true, + Recursive: true, + WithVersions: true, }) { if object.Err != nil { log.Fatalln(object.Err) diff --git a/minio/resource_minio_s3_bucket_versioning_test.go b/minio/resource_minio_s3_bucket_versioning_test.go index 7014c1d9..6a3adb6d 100644 --- a/minio/resource_minio_s3_bucket_versioning_test.go +++ b/minio/resource_minio_s3_bucket_versioning_test.go @@ -88,6 +88,30 @@ func TestAccS3BucketVersioning_update(t *testing.T) { }) } +func TestAccS3BucketVersioning_forceDestroy(t *testing.T) { + name := acctest.RandomWithPrefix("tf-version-force-destroy") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviders, + CheckDestroy: testAccCheckMinioS3BucketDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBucketVersioningObjectConfig(name, "Enabled"), + Check: resource.ComposeTestCheckFunc( + testAccCheckMinioS3BucketExists("minio_s3_bucket.bucket"), + testAccCheckBucketHasVersioning( + "minio_s3_bucket_versioning.bucket", + S3MinioBucketVersioningConfiguration{ + Status: "Enabled", + }, + ), + ), + }, + }, + }) +} + func testAccBucketVersioningConfig(bucketName string, status string, prefixes []string, excludeFolders bool) string { prefixSlice := []string{} for _, v := range prefixes { @@ -111,6 +135,22 @@ resource "minio_s3_bucket_versioning" "bucket" { `, bucketName, status, strings.Join(prefixSlice, ", "), excludeFolders) } +func testAccBucketVersioningObjectConfig(bucketName string, status string) string { + return fmt.Sprintf(` +resource "minio_s3_bucket" "bucket" { + bucket = "%s" + force_destroy = true +} + +resource "minio_s3_bucket_versioning" "bucket" { + bucket = minio_s3_bucket.bucket.bucket + versioning_configuration { + status = "%s" + } +} +`, bucketName, status) +} + func testAccCheckBucketHasVersioning(n string, config S3MinioBucketVersioningConfiguration) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n]