Skip to content

Commit

Permalink
Allow change status of a minio_ilm_policy (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
felladrin authored Feb 14, 2025
1 parent ed80fb9 commit 626ab1f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 8 deletions.
10 changes: 7 additions & 3 deletions docs/resources/ilm_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ resource "minio_ilm_policy" "bucket-lifecycle-rules" {
rule {
id = "expire-7d"
status = "Enabled"
expiration = "7d"
}
}
Expand All @@ -33,7 +34,8 @@ resource "minio_ilm_policy" "comprehensive-rules" {
# Rule with transition and expiration
rule {
id = "documents"
id = "documents"
status = "Enabled"
transition {
days = "30d"
storage_class = "STANDARD_IA"
Expand All @@ -47,7 +49,8 @@ resource "minio_ilm_policy" "comprehensive-rules" {
# Rule with noncurrent version management
rule {
id = "versioning"
id = "versioning"
status = "Enabled"
noncurrent_expiration {
days = "60d"
newer_versions = 5
Expand Down Expand Up @@ -80,6 +83,7 @@ resource "minio_ilm_policy" "comprehensive-rules" {

#### Optional

- `status` (String) Status of the rule. Can be either "Enabled" or "Disabled". Defaults to "Enabled".
- `expiration` (String) When objects should expire. Value must be a duration (e.g., "7d"), date (e.g., "2024-12-31"), or "DeleteMarker".
- `filter` (String) Prefix path filter for the rule.
- `tags` (Map of String) Key-value pairs of tags to filter objects.
Expand Down Expand Up @@ -129,4 +133,4 @@ MinIO lifecycle policies can be imported using the bucket name:

```shell
terraform import minio_ilm_policy.example bucket-name
```
```
2 changes: 2 additions & 0 deletions examples/resources/minio_ilm_policy/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ resource "minio_ilm_policy" "test_policy" {

rule {
id = "rule1"
status = "Enabled"
# Delete objects after 90 days
expiration = "90d"
filter = "documents/"
Expand All @@ -18,6 +19,7 @@ resource "minio_ilm_policy" "test_policy" {

rule {
id = "rule2"
status = "Disabled"
# Move objects to GLACIER after 30 days
transition {
days = "30d"
Expand Down
18 changes: 13 additions & 5 deletions minio/resource_minio_ilm_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func resourceMinioILMPolicy() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
Default: "Enabled",
ValidateFunc: validation.StringInSlice([]string{"Enabled", "Disabled"}, false),
Description: "Status of the rule. Can be either 'Enabled' or 'Disabled'. Defaults to 'Enabled'.",
},
"expiration": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -111,10 +118,6 @@ func resourceMinioILMPolicy() *schema.Resource {
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"filter": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -222,6 +225,11 @@ func createLifecycleRule(ruleData map[string]interface{}) (lifecycle.Rule, error
return lifecycle.Rule{}, fmt.Errorf("rule id is required")
}

status, ok := getStringValue(ruleData, "status")
if !ok {
status = "Enabled"
}

if transition, exists := ruleData["transition"].([]interface{}); exists && len(transition) > 0 {
t := transition[0].(map[string]interface{})
if _, ok := t["storage_class"].(string); !ok {
Expand Down Expand Up @@ -267,7 +275,7 @@ func createLifecycleRule(ruleData map[string]interface{}) (lifecycle.Rule, error
Transition: parseILMTransition(ruleData["transition"]),
NoncurrentVersionExpiration: parseILMNoncurrentExpiration(ruleData["noncurrent_expiration"]),
NoncurrentVersionTransition: noncurrentTransition,
Status: "Enabled",
Status: status,
RuleFilter: filter,
}, nil
}
Expand Down
92 changes: 92 additions & 0 deletions minio/resource_minio_ilm_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,45 @@ func TestAccILMPolicy_transition(t *testing.T) {
})
}

func TestAccILMPolicy_ruleStatus(t *testing.T) {
var lifecycleConfig lifecycle.Configuration
name := fmt.Sprintf("test-ilm-rule-status-%d", acctest.RandInt())
resourceName := "minio_ilm_policy.rule_status"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckMinioS3BucketDestroy,
Steps: []resource.TestStep{
{
// Test default status (should be "Enabled")
Config: testAccMinioILMPolicyConfigDefaultStatus(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioS3BucketExists("minio_s3_bucket.bucket"),
testAccCheckMinioILMPolicyExists(resourceName, &lifecycleConfig),
resource.TestCheckResourceAttr(resourceName, "rule.0.status", "Enabled"),
),
},
{
// Test explicitly setting status to "Disabled"
Config: testAccMinioILMPolicyConfigDisabledStatus(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioILMPolicyExists(resourceName, &lifecycleConfig),
resource.TestCheckResourceAttr(resourceName, "rule.0.status", "Disabled"),
),
},
{
// Test updating back to "Enabled"
Config: testAccMinioILMPolicyConfigEnabledStatus(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioILMPolicyExists(resourceName, &lifecycleConfig),
resource.TestCheckResourceAttr(resourceName, "rule.0.status", "Enabled"),
),
},
},
})
}

func testAccCheckMinioLifecycleConfigurationValid(config *lifecycle.Configuration) resource.TestCheckFunc {
return func(s *terraform.State) error {
if config.Empty() || len(config.Rules) == 0 {
Expand Down Expand Up @@ -388,3 +427,56 @@ resource "minio_s3_bucket" %q {
bucket = %q
}`, resourceName, provider, bucketName)
}

func testAccMinioILMPolicyConfigDefaultStatus(randInt string) string {
return fmt.Sprintf(`
resource "minio_s3_bucket" "bucket" {
bucket = "%s"
acl = "public"
}
resource "minio_ilm_policy" "rule_status" {
bucket = minio_s3_bucket.bucket.bucket
rule {
id = "rule-default-status"
expiration = "7d"
}
}
`, randInt)
}

func testAccMinioILMPolicyConfigDisabledStatus(randInt string) string {
return fmt.Sprintf(`
resource "minio_s3_bucket" "bucket" {
bucket = "%s"
acl = "public"
}
resource "minio_ilm_policy" "rule_status" {
bucket = minio_s3_bucket.bucket.bucket
rule {
id = "rule-disabled-status"
status = "Disabled"
expiration = "7d"
}
}
`, randInt)
}

func testAccMinioILMPolicyConfigEnabledStatus(randInt string) string {
return fmt.Sprintf(`
resource "minio_s3_bucket" "bucket" {
bucket = "%s"
acl = "public"
}
resource "minio_ilm_policy" "rule_status" {
bucket = minio_s3_bucket.bucket.bucket
rule {
id = "rule-enabled-status"
status = "Enabled"
expiration = "7d"
}
}
`, randInt)
}

0 comments on commit 626ab1f

Please sign in to comment.