Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_dlm_lifecycle_policy: retain rule #11503

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions aws/resource_aws_dlm_lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,29 @@ func resourceAwsDlmLifecyclePolicy() *schema.Resource {
Schema: map[string]*schema.Schema{
"count": {
Type: schema.TypeInt,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 1000),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably have ConflictsWith: []string{"policy_details.0.schedule.0.retain_rule.0.interval", "policy_details.0.schedule.0.retain_rule.0.interval_unit"}, and then ConflictsWith: []string{"policy_details.0.schedule.0.retain_rule.0.count"}, on the interval and interval_unit blocks.

Copy link
Contributor

@tomelliff tomelliff Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw your comment above but in this case nearly all of the parameters in the DLM API that is a list is also required to be 1 element in the list (AWS sure do like future proofing things at the expense of more complex APIs) so you can just hardcode the list element as 0 as in my comment here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realised that that's not currently true because schedule is missing a MaxItems: 1 despite that being required by the API which I think I missed in my initial pull request that added this resource.

We should add that in this pull request I think. It then makes the ConflictsWith robust as well as giving plan time validation in the event someone adds multiple schedule blocks to a policy_details block.

},
"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,
dlm.RetentionIntervalUnitValuesWeeks,
dlm.RetentionIntervalUnitValuesMonths,
dlm.RetentionIntervalUnitValuesYears,
}, false),
},
},
},
},
Expand Down Expand Up @@ -347,6 +367,7 @@ func flattenDlmCreateRule(createRule *dlm.CreateRule) []map[string]interface{} {
}

result := make(map[string]interface{})

result["interval"] = aws.Int64Value(createRule.Interval)
result["interval_unit"] = aws.StringValue(createRule.IntervalUnit)
result["times"] = flattenStringList(createRule.Times)
Expand All @@ -359,14 +380,34 @@ func expandDlmRetainRule(cfg []interface{}) *dlm.RetainRule {
return nil
}
m := cfg[0].(map[string]interface{})
return &dlm.RetainRule{
Count: aws.Int64(int64(m["count"].(int))),

retainRule := &dlm.RetainRule{}

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

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

if v, ok := m["interval_unit"]; ok {
retainRule.IntervalUnit = aws.String(v.(string))
}

return retainRule
}

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

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
157 changes: 156 additions & 1 deletion aws/resource_aws_dlm_lifecycle_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,44 @@ func TestAccAWSDlmLifecyclePolicy_Full(t *testing.T) {
})
}

func TestAccAWSDlmLifecyclePolicy_Retain(t *testing.T) {
resourceName := "aws_dlm_lifecycle_policy.full"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSDlm(t) },
Providers: testAccProviders,
CheckDestroy: dlmLifecyclePolicyDestroy,
Steps: []resource.TestStep{
{
Config: dlmLifecyclePolicyFullUpdateConfigWithAgeBasedRetain(rName),
Check: resource.ComposeTestCheckFunc(
checkDlmLifecyclePolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "description", "tf-acc-full-updated-age"),
resource.TestCheckResourceAttrSet(resourceName, "execution_role_arn"),
resource.TestCheckResourceAttr(resourceName, "state", "DISABLED"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.resource_types.0", "VOLUME"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.name", "tf-acc-full-updated-age"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.create_rule.0.interval", "24"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.create_rule.0.interval_unit", "HOURS"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.create_rule.0.times.0", "09:42"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.retain_rule.0.interval", "15"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.retain_rule.0.interval_unit", "DAYS"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.tags_to_add.tf-acc-test-added", "full-updated-age"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.schedule.0.copy_tags", "true"),
resource.TestCheckResourceAttr(resourceName, "policy_details.0.target_tags.tf-acc-test", "full-updated-age"),
),
},
{
Config: dlmLifecyclePolicyFullUpdateConfigWithAgeBasedRetainless(rName),
Check: resource.ComposeTestCheckFunc(
checkDlmLifecyclePolicyExists(resourceName),
),
},
},
})
}

func TestAccAWSDlmLifecyclePolicy_Tags(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_dlm_lifecycle_policy.test"
Expand Down Expand Up @@ -246,7 +284,7 @@ resource "aws_dlm_lifecycle_policy" "basic" {
}

retain_rule {
count = 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an odd diff here. Is this accidental?

count = 10
}
}

Expand Down Expand Up @@ -374,6 +412,123 @@ resource "aws_dlm_lifecycle_policy" "full" {
`, rName)
}

func dlmLifecyclePolicyFullUpdateConfigWithAgeBasedRetain(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 {
interval = 15
interval_unit = "DAYS"
}

tags_to_add = {
tf-acc-test-added = "full-updated-age"
}

copy_tags = true
}

target_tags = {
tf-acc-test = "full-updated-age"
}
}
}
`, 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
8 changes: 6 additions & 2 deletions website/docs/r/dlm_lifecycle_policy.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -121,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 All @@ -132,7 +132,11 @@ The following arguments are supported:

#### Retain Rule arguments

* `count` - (Required) How many snapshots to keep. Must be an integer between 1 and 1000.
Note: One of either `count` or `interval`/`interval_unit` must be specified.

* `count` - (Optional) How many snapshots to keep. Must be an integer between 1 and 1000.
* `interval` - (Optional) The amount of time to retain each snapshot. The maximum is 100 years or that equivalent in months (1200), weeks (5200), and days (36500).
* `interval_unit` - (Optional) The unit of time for time-based retention.

## Attributes Reference

Expand Down