From a16ed4a8b365582c3d0df8381b2243e6b17ecf02 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Tue, 10 Nov 2020 15:58:26 +0200 Subject: [PATCH] really optional --- aws/resource_aws_backup_plan.go | 10 +++- aws/resource_aws_backup_plan_test.go | 88 +++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_backup_plan.go b/aws/resource_aws_backup_plan.go index ddffa8d5f91..43001acd376 100644 --- a/aws/resource_aws_backup_plan.go +++ b/aws/resource_aws_backup_plan.go @@ -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) } @@ -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) diff --git a/aws/resource_aws_backup_plan_test.go b/aws/resource_aws_backup_plan_test.go index 0f44e0d6456..8d0c5597b1d 100644 --- a/aws/resource_aws_backup_plan_test.go +++ b/aws/resource_aws_backup_plan_test.go @@ -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" @@ -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" @@ -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" {