diff --git a/aws/resource_aws_dlm_lifecycle_policy.go b/aws/resource_aws_dlm_lifecycle_policy.go index 8b4f1e305bd..8a1c69f058c 100644 --- a/aws/resource_aws_dlm_lifecycle_policy.go +++ b/aws/resource_aws_dlm_lifecycle_policy.go @@ -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), }, + "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), + }, }, }, }, @@ -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) @@ -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} } diff --git a/aws/resource_aws_dlm_lifecycle_policy_test.go b/aws/resource_aws_dlm_lifecycle_policy_test.go index 10b84f5be56..e736f7c4272 100644 --- a/aws/resource_aws_dlm_lifecycle_policy_test.go +++ b/aws/resource_aws_dlm_lifecycle_policy_test.go @@ -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" @@ -246,7 +284,7 @@ resource "aws_dlm_lifecycle_policy" "basic" { } retain_rule { - count = 10 + count = 10 } } @@ -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 = <