Skip to content

Commit

Permalink
r-dlm_lifecycle_policy-retainRuleAdditions
Browse files Browse the repository at this point in the history
  • Loading branch information
thatderek committed Jan 6, 2020
1 parent 6816045 commit 7eb7542
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
24 changes: 12 additions & 12 deletions aws/resource_aws_dlm_lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,20 @@ func resourceAwsDlmLifecyclePolicy() *schema.Resource {
"count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 1000),
},
"interval": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.IntAtLeast(1),
},
"interval_unit": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
dlm.RetentionIntervalUnitValuesDays,
Expand Down Expand Up @@ -381,11 +383,11 @@ func expandDlmRetainRule(cfg []interface{}) *dlm.RetainRule {

retainRule := &dlm.RetainRule{}

if v, _ := m["count"]; v != 0 {
if v := m["count"]; v.(int) > 0 {
retainRule.Count = aws.Int64(int64(v.(int)))
}

if v, _ := m["interval"]; v != 0 {
if v := m["interval"]; v.(int) > 0 {
retainRule.Interval = aws.Int64(int64(v.(int)))
}

Expand All @@ -394,19 +396,17 @@ func expandDlmRetainRule(cfg []interface{}) *dlm.RetainRule {
}

return retainRule
/*
return &dlm.RetainRule{
Count: aws.Int64(int64(m["count"].(int))),
Interval: aws.Int64(int64(m["interval"].(int))),
IntervalUnit: aws.String(string(m["interval_unit"].(string))),
}
*/
}

func flattenDlmRetainRule(retainRule *dlm.RetainRule) []map[string]interface{} {
result := make(map[string]interface{})
result["count"] = aws.Int64Value(retainRule.Count)
result["interval"] = aws.Int64Value(retainRule.Interval)

if aws.Int64Value(retainRule.Count) > int64(0) {
result["count"] = aws.Int64Value(retainRule.Count)
}
if aws.Int64Value(retainRule.Interval) != 0 {
result["interval"] = aws.Int64Value(retainRule.Interval)
}
result["interval_unit"] = aws.StringValue(retainRule.IntervalUnit)

return []map[string]interface{}{result}
Expand Down
64 changes: 64 additions & 0 deletions aws/resource_aws_dlm_lifecycle_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func TestAccAWSDlmLifecyclePolicy_Retain(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "policy_details.0.target_tags.tf-acc-test", "full-updated-age"),
),
},
{
Config: dlmLifecyclePolicyFullUpdateConfigWithAgeBasedRetainless(rName),
Check: resource.ComposeTestCheckFunc(
checkDlmLifecyclePolicyExists(resourceName),
),
},
},
})
}
Expand Down Expand Up @@ -465,6 +471,64 @@ resource "aws_dlm_lifecycle_policy" "full" {
`, rName)
}

func dlmLifecyclePolicyFullUpdateConfigWithAgeBasedRetainless(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "dlm_lifecycle_role" {
name = %q
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "dlm.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_dlm_lifecycle_policy" "full" {
description = "tf-acc-full-updated-age"
execution_role_arn = "${aws_iam_role.dlm_lifecycle_role.arn}-doesnt-exist"
state = "DISABLED"
policy_details {
resource_types = ["VOLUME"]
schedule {
name = "tf-acc-full-updated-age"
create_rule {
interval = 24
interval_unit = "HOURS"
times = ["09:42"]
}
retain_rule {
count = 5
}
tags_to_add = {
tf-acc-test-added = "full-updated-age"
}
copy_tags = true
}
target_tags = {
tf-acc-test = "full-updated-age"
}
}
}
`, rName)
}

func dlmLifecyclePolicyConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
Expand Down
6 changes: 2 additions & 4 deletions website/docs/r/dlm_lifecycle_policy.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ resource "aws_dlm_lifecycle_policy" "example" {
}
retain_rule {
count = 14
interval = 15
interval_unit = "WEEKS"
count = 14
}
tags_to_add = {
Expand Down Expand Up @@ -123,7 +121,7 @@ The following arguments are supported:
* `copy_tags` - (Optional) Copy all user-defined tags on a source volume to snapshots of the volume created by this policy.
* `create_rule` - (Required) See the [`create_rule`](#create-rule-arguments) block. Max of 1 per schedule.
* `name` - (Required) A name for the schedule.
* `retain_rule` - (Required) See the [`retain_rule`](#retain-rule-arguments) block. Max of 1 per schedule.
* `retain_rule` - (Required) See the [`retain_rule`](#retain-rule-arguments) block.
* `tags_to_add` - (Optional) A mapping of tag keys and their values. DLM lifecycle policies will already tag the snapshot with the tags on the volume. This configuration adds extra tags on top of these.

#### Create Rule arguments
Expand Down

0 comments on commit 7eb7542

Please sign in to comment.