Skip to content

Commit

Permalink
Add support for Tags in minio_ilm_policy resource (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-ruth authored Jan 30, 2023
1 parent 14f204b commit 7603594
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
10 changes: 8 additions & 2 deletions docs/resources/ilm_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ resource "minio_ilm_policy" "bucket-lifecycle-rules" {
rule {
id = "expire-7d"
expiration = 7
expiration = "7d"
filter = "prefix/"
tags = {
"app" = "myapp"
}
}
}
```
Expand All @@ -47,7 +52,8 @@ Required:
Optional:

- **expiration** (String) The expiration as a duration (5d), date (1970-01-01), or "DeleteMarker"
- **filter** (String)
- **filter** (String) Correspond to "prefix" value
- **tags** (Map of String) List of tags to use in filter

Read-Only:

Expand Down
37 changes: 35 additions & 2 deletions minio/resource_minio_ilm_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func resourceMinioILMPolicy() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -79,11 +83,28 @@ func minioCreateILMPolicy(ctx context.Context, d *schema.ResourceData, meta inte
rules := d.Get("rule").([]interface{})
for _, ruleI := range rules {
rule := ruleI.(map[string]interface{})

var filter lifecycle.Filter

tags := map[string]string{}
for k, v := range rule["tags"].(map[string]interface{}) {
tags[k] = v.(string)
}

if len(tags) > 0 {
filter.And.Prefix = rule["filter"].(string)
for k, v := range tags {
filter.And.Tags = append(filter.And.Tags, lifecycle.Tag{Key: k, Value: v})
}
} else {
filter.Prefix = rule["filter"].(string)
}

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

var prefix string
tags := map[string]string{}
if len(r.RuleFilter.And.Tags) > 0 {
prefix = r.RuleFilter.And.Prefix
for _, tag := range r.RuleFilter.And.Tags {
tags[tag.Key] = tag.Value
}
} else {
prefix = r.RuleFilter.Prefix
}

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckMinioS3BucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccMinioILMPolicyFilterWithPrefix(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioILMPolicyExists(resourceName, &lifecycleConfig),
testAccCheckMinioLifecycleConfigurationValid(&lifecycleConfig),
),
},
{
Config: testAccMinioILMPolicyFilterWithPrefixAndTags(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioILMPolicyExists(resourceName, &lifecycleConfig),
testAccCheckMinioLifecycleConfigurationValid(&lifecycleConfig),
),
},
},
})
}

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

func testAccMinioILMPolicyFilterWithPrefix(randInt string) string {
return fmt.Sprintf(`
resource "minio_s3_bucket" "bucket3" {
bucket = "%s"
acl = "public-read"
}
resource "minio_ilm_policy" "rule3" {
bucket = "${minio_s3_bucket.bucket3.id}"
rule {
id = "withPrefix"
expiration = "5d"
filter = "temp/"
}
}
`, randInt)
}

func testAccMinioILMPolicyFilterWithPrefixAndTags(randInt string) string {
return fmt.Sprintf(`
resource "minio_s3_bucket" "bucket3" {
bucket = "%s"
acl = "public-read"
}
resource "minio_ilm_policy" "rule3" {
bucket = "${minio_s3_bucket.bucket3.id}"
rule {
id = "withPrefixAndTags"
expiration = "5d"
filter = "temp/"
tags = {
key1 = "value1"
key2 = "value2"
}
}
}
`, randInt)
}

0 comments on commit 7603594

Please sign in to comment.