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/backup_plan - lifecycle block in copy_action is optional #16116

Merged
Merged
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
10 changes: 8 additions & 2 deletions aws/resource_aws_backup_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,10 @@ func expandBackupPlanCopyActions(actionList []interface{}) []*backup.CopyAction
action := &backup.CopyAction{}

action.DestinationBackupVaultArn = aws.String(item["destination_vault_arn"].(string))
action.Lifecycle = expandBackupPlanLifecycle(item["lifecycle"].([]interface{}))

if v, ok := item["lifecycle"].([]interface{}); ok && len(v) > 0 {
action.Lifecycle = expandBackupPlanLifecycle(v)
}

actions = append(actions, action)
}
Expand Down Expand Up @@ -416,7 +419,10 @@ func flattenBackupPlanCopyActions(copyActions []*backup.CopyAction) []interface{

tfMap := map[string]interface{}{
"destination_vault_arn": aws.StringValue(copyAction.DestinationBackupVaultArn),
"lifecycle": flattenBackupPlanCopyActionLifecycle(copyAction.Lifecycle),
}

if copyAction.Lifecycle != nil {
tfMap["lifecycle"] = flattenBackupPlanCopyActionLifecycle(copyAction.Lifecycle)
}

tfList = append(tfList, tfMap)
Expand Down
88 changes: 87 additions & 1 deletion aws/resource_aws_backup_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,66 @@ func TestAccAwsBackupPlan_Rule_CopyAction_SameRegion(t *testing.T) {
})
}

func TestAccAwsBackupPlan_Rule_CopyAction_NoLifecycle(t *testing.T) {
var plan backup.GetBackupPlanOutput
resourceName := "aws_backup_plan.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBackup(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsBackupPlanDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsBackupPlanConfigRuleCopyActionNoLifecycle(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupPlanExists(resourceName, &plan),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "rule.*", map[string]string{
"rule_name": rName,
"lifecycle.#": "0",
"copy_action.#": "1",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAwsBackupPlanConfigRuleCopyAction(rName, 60, 365),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupPlanExists(resourceName, &plan),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "rule.*", map[string]string{
"rule_name": rName,
"lifecycle.#": "1",
"lifecycle.0.cold_storage_after": "30",
"lifecycle.0.delete_after": "180",
"copy_action.#": "1",
}),
),
},
{
Config: testAccAwsBackupPlanConfigRuleCopyActionNoLifecycle(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupPlanExists(resourceName, &plan),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "rule.*", map[string]string{
"rule_name": rName,
"lifecycle.#": "0",
"copy_action.#": "1",
}),
),
},
},
})
}

func TestAccAwsBackupPlan_Rule_CopyAction_Multiple(t *testing.T) {
var plan backup.GetBackupPlanOutput
resourceName := "aws_backup_plan.test"
Expand Down Expand Up @@ -800,7 +860,7 @@ resource "aws_backup_plan" "test" {
`, rName)
}

func testAccAwsBackupPlanConfigRuleCopyAction(rName string, coldStorageAfter int, deleteAfter int) string {
func testAccAwsBackupPlanConfigRuleCopyAction(rName string, coldStorageAfter, deleteAfter int) string {
return fmt.Sprintf(`
resource "aws_backup_vault" "test" {
name = "%[1]s-1"
Expand Down Expand Up @@ -922,6 +982,32 @@ resource "aws_backup_plan" "test" {
`, rName)
}

func testAccAwsBackupPlanConfigRuleCopyActionNoLifecycle(rName string) string {
return fmt.Sprintf(`
resource "aws_backup_vault" "test" {
name = "%[1]s-1"
}

resource "aws_backup_vault" "test2" {
name = "%[1]s-2"
}

resource "aws_backup_plan" "test" {
name = %[1]q

rule {
rule_name = %[1]q
target_vault_name = aws_backup_vault.test.name
schedule = "cron(0 12 * * ? *)"

copy_action {
destination_vault_arn = aws_backup_vault.test2.arn
}
}
}
`, rName)
}

func testAccAwsBackupPlanConfigAdvancedBackupSetting(rName string) string {
return fmt.Sprintf(`
resource "aws_backup_vault" "test" {
Expand Down