Skip to content

Commit

Permalink
Destroy object versions on bucket force_destroy (#524)
Browse files Browse the repository at this point in the history
(cherry picked from commit a107f61)
  • Loading branch information
pjsier authored and felladrin committed Oct 9, 2023
1 parent 2185c6c commit 9f77e2b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion minio/resource_minio_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions minio/resource_minio_s3_bucket_versioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down

0 comments on commit 9f77e2b

Please sign in to comment.