Skip to content

Commit

Permalink
Merge pull request #899 from kjmkznr/f-aws-s3-bucket-lifecycle-tag
Browse files Browse the repository at this point in the history
resource/s3_bucket: Support lifecycle tags filter
  • Loading branch information
radeksimko authored Jun 30, 2017
2 parents e3a12a9 + 3a38399 commit f14e748
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
36 changes: 30 additions & 6 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ func resourceAwsS3Bucket() *schema.Resource {
},
"prefix": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"tags": tagsSchema(),
"enabled": {
Type: schema.TypeBool,
Required: true,
Expand Down Expand Up @@ -782,9 +783,21 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
if lifecycleRule.ID != nil && *lifecycleRule.ID != "" {
rule["id"] = *lifecycleRule.ID
}
// Prefix
if lifecycleRule.Prefix != nil && *lifecycleRule.Prefix != "" {
rule["prefix"] = *lifecycleRule.Prefix
filter := lifecycleRule.Filter
if filter.And != nil {
// Prefix
if filter.And.Prefix != nil && *filter.And.Prefix != "" {
rule["prefix"] = *filter.And.Prefix
}
// Tag
if len(filter.And.Tags) > 0 {
rule["tags"] = tagsToMapS3(filter.And.Tags)
}
} else {
// Prefix
if filter.Prefix != nil && *filter.Prefix != "" {
rule["prefix"] = *filter.Prefix
}
}
// Enabled
if lifecycleRule.Status != nil {
Expand Down Expand Up @@ -1514,9 +1527,20 @@ func resourceAwsS3BucketLifecycleUpdate(s3conn *s3.S3, d *schema.ResourceData) e
for i, lifecycleRule := range lifecycleRules {
r := lifecycleRule.(map[string]interface{})

rule := &s3.LifecycleRule{
Prefix: aws.String(r["prefix"].(string)),
rule := &s3.LifecycleRule{}

// Filter
tags := r["tags"].(map[string]interface{})
filter := &s3.LifecycleRuleFilter{}
if len(tags) > 0 {
lifecycleRuleAndOp := &s3.LifecycleRuleAndOperator{}
lifecycleRuleAndOp.SetPrefix(r["prefix"].(string))
lifecycleRuleAndOp.SetTags(tagsFromMapS3(tags))
filter.SetAnd(lifecycleRuleAndOp)
} else {
filter.SetPrefix(r["prefix"].(string))
}
rule.SetFilter(filter)

// ID
if val, ok := r["id"].(string); ok && val != "" {
Expand Down
22 changes: 22 additions & 0 deletions aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,14 @@ func TestAccAWSS3Bucket_Lifecycle(t *testing.T) {
"aws_s3_bucket.bucket", "lifecycle_rule.2.prefix", "path3/"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "lifecycle_rule.2.transition.460947558.days", "0"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "lifecycle_rule.3.id", "id4"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "lifecycle_rule.3.prefix", "path4/"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "lifecycle_rule.3.tags.tagKey", "tagValue"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "lifecycle_rule.3.tags.terraform", "hashicorp"),
),
},
{
Expand Down Expand Up @@ -1454,6 +1462,20 @@ resource "aws_s3_bucket" "bucket" {
storage_class = "GLACIER"
}
}
lifecycle_rule {
id = "id4"
prefix = "path4/"
enabled = true
tags {
"tagKey" = "tagValue"
"terraform" = "hashicorp"
}
expiration {
date = "2016-01-12"
}
}
}
`, randInt)
}
Expand Down
10 changes: 8 additions & 2 deletions website/docs/r/s3_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,14 @@ resource "aws_s3_bucket" "bucket" {
lifecycle_rule {
id = "log"
prefix = "log/"
enabled = true
prefix = "log/"
tags {
"rule" = "log"
"autoclean" = "true"
}
transition {
days = 30
storage_class = "STANDARD_IA"
Expand Down Expand Up @@ -337,7 +342,8 @@ The `logging` object supports the following:
The `lifecycle_rule` object supports the following:

* `id` - (Optional) Unique identifier for the rule.
* `prefix` - (Required) Object key prefix identifying one or more objects to which the rule applies.
* `prefix` - (Optional) Object key prefix identifying one or more objects to which the rule applies.
* `tags` - (Optional) Specifies object tags key and value.
* `enabled` - (Required) Specifies lifecycle rule status.
* `abort_incomplete_multipart_upload_days` (Optional) Specifies the number of days after initiating a multipart upload when the multipart upload must be completed.
* `expiration` - (Optional) Specifies a period in the object's expire (documented below).
Expand Down

0 comments on commit f14e748

Please sign in to comment.