Skip to content

Commit

Permalink
Add Noncurrent Version expiration to ilm policy (#526)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas De Jesus <[email protected]>
  • Loading branch information
2 people authored and felladrin committed Oct 24, 2023
1 parent f42b008 commit ceec923
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/resources/ilm_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Optional:

- `expiration` (String)
- `filter` (String)
- `noncurrent_version_expiration_days` (Int)
- `tags` (Map of String)

Read-Only:
Expand Down
42 changes: 33 additions & 9 deletions minio/resource_minio_ilm_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func resourceMinioILMPolicy() *schema.Resource {
Optional: true,
ValidateDiagFunc: validateILMExpiration,
},
"noncurrent_version_expiration_days": {
Type: schema.TypeInt,
Optional: true,
ValidateDiagFunc: validateILMNoncurrentVersionExpiration,
},
"status": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -74,6 +79,16 @@ func validateILMExpiration(v interface{}, p cty.Path) (errors diag.Diagnostics)
return
}

func validateILMNoncurrentVersionExpiration(v interface{}, p cty.Path) (errors diag.Diagnostics) {
value := v.(int)

if value < 1 {
return diag.Errorf("noncurrent_version_expiration_days must be strictly positive")
}

return
}

func minioCreateILMPolicy(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*S3MinioClient).S3Client

Expand All @@ -86,6 +101,8 @@ func minioCreateILMPolicy(ctx context.Context, d *schema.ResourceData, meta inte

var filter lifecycle.Filter

noncurrentVersionExpirationDays := lifecycle.NoncurrentVersionExpiration{NoncurrentDays: lifecycle.ExpirationDays(rule["noncurrent_version_expiration_days"].(int))}

tags := map[string]string{}
for k, v := range rule["tags"].(map[string]interface{}) {
tags[k] = v.(string)
Expand All @@ -101,10 +118,11 @@ func minioCreateILMPolicy(ctx context.Context, d *schema.ResourceData, meta inte
}

r := lifecycle.Rule{
ID: rule["id"].(string),
Expiration: parseILMExpiration(rule["expiration"].(string)),
Status: "Enabled",
RuleFilter: filter,
ID: rule["id"].(string),
Expiration: parseILMExpiration(rule["expiration"].(string)),
NoncurrentVersionExpiration: noncurrentVersionExpirationDays,
Status: "Enabled",
RuleFilter: filter,
}
config.Rules = append(config.Rules, r)
}
Expand Down Expand Up @@ -144,6 +162,11 @@ func minioReadILMPolicy(ctx context.Context, d *schema.ResourceData, meta interf
expiration = r.Expiration.Date.Format("2006-01-02")
}

var noncurrentVersionExpirationDays int
if r.NoncurrentVersionExpiration.NoncurrentDays != 0 {
noncurrentVersionExpirationDays = int(r.NoncurrentVersionExpiration.NoncurrentDays)
}

var prefix string
tags := map[string]string{}
if len(r.RuleFilter.And.Tags) > 0 {
Expand All @@ -156,11 +179,12 @@ func minioReadILMPolicy(ctx context.Context, d *schema.ResourceData, meta interf
}

rule := map[string]interface{}{
"id": r.ID,
"expiration": expiration,
"status": r.Status,
"filter": prefix,
"tags": tags,
"id": r.ID,
"expiration": expiration,
"noncurrent_version_expiration_days": noncurrentVersionExpirationDays,
"status": r.Status,
"filter": prefix,
"tags": tags,
}
rules = append(rules, rule)
}
Expand Down
40 changes: 40 additions & 0 deletions minio/resource_minio_ilm_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ func TestAccILMPolicy_filterTags(t *testing.T) {
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckMinioS3BucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccMinioILMPolicyExpireNoncurrentVersion(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioILMPolicyExists(resourceName, &lifecycleConfig),
testAccCheckMinioLifecycleConfigurationValid(&lifecycleConfig),
resource.TestCheckResourceAttr(
resourceName, "rule.0.noncurrent_version_expiration_days", "5"),
),
},
},
})
}

func testAccCheckMinioLifecycleConfigurationValid(config *lifecycle.Configuration) resource.TestCheckFunc {
return func(s *terraform.State) error {
if config.Empty() || len(config.Rules) == 0 {
Expand Down Expand Up @@ -208,3 +231,20 @@ resource "minio_ilm_policy" "rule3" {
}
`, randInt)
}

func testAccMinioILMPolicyExpireNoncurrentVersion(randInt string) string {
return fmt.Sprintf(`
resource "minio_s3_bucket" "bucket4" {
bucket = "%s"
acl = "public-read"
}
resource "minio_ilm_policy" "rule4" {
bucket = "${minio_s3_bucket.bucket4.id}"
rule {
id = "expireNoncurrentVersion"
expiration = "5d"
noncurrent_version_expiration_days = 5
}
}
`, randInt)
}

0 comments on commit ceec923

Please sign in to comment.