From 490f196fad39e7724222679cfd347f589cf2f92a Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 23 Mar 2020 00:22:21 +0200 Subject: [PATCH 01/19] add backup vault notifications resource --- aws/provider.go | 1 + ...resource_aws_backup_vault_notifications.go | 120 ++++++++++++++++ ...rce_aws_backup_vault_notifications_test.go | 130 ++++++++++++++++++ .../backup_vault_notifications.html.markdown | 44 ++++++ 4 files changed, 295 insertions(+) create mode 100644 aws/resource_aws_backup_vault_notifications.go create mode 100644 aws/resource_aws_backup_vault_notifications_test.go create mode 100644 website/docs/r/backup_vault_notifications.html.markdown diff --git a/aws/provider.go b/aws/provider.go index 34e5c292584..d5101b1c8ff 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -433,6 +433,7 @@ func Provider() *schema.Provider { "aws_backup_plan": resourceAwsBackupPlan(), "aws_backup_selection": resourceAwsBackupSelection(), "aws_backup_vault": resourceAwsBackupVault(), + "aws_backup_vault_notifications": resourceAwsBackupVaultNotifications(), "aws_budgets_budget": resourceAwsBudgetsBudget(), "aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(), "aws_cloudformation_stack": resourceAwsCloudFormationStack(), diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go new file mode 100644 index 00000000000..6c019e50e41 --- /dev/null +++ b/aws/resource_aws_backup_vault_notifications.go @@ -0,0 +1,120 @@ +package aws + +import ( + "fmt" + "regexp" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/backup" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" +) + +func resourceAwsBackupVaultNotifications() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsBackupVaultNotificationsCreate, + Read: resourceAwsBackupVaultNotificationsRead, + Delete: resourceAwsBackupVaultNotificationsDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "backup_vault_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]{1,50}$`), "must consist of lowercase letters, numbers, and hyphens."), + }, + "sns_topic_arn": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateArn, + }, + "backup_vault_events": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice([]string{ + backup.VaultEventBackupJobStarted, + backup.VaultEventBackupJobCompleted, + backup.VaultEventBackupJobSuccessful, + backup.VaultEventBackupJobFailed, + backup.VaultEventBackupJobExpired, + backup.VaultEventRestoreJobStarted, + backup.VaultEventRestoreJobSuccessful, + backup.VaultEventRestoreJobCompleted, + backup.VaultEventRestoreJobFailed, + backup.VaultEventCopyJobFailed, + backup.VaultEventCopyJobStarted, + backup.VaultEventCopyJobSuccessful, + backup.VaultEventRecoveryPointModified, + backup.VaultEventBackupPlanCreated, + backup.VaultEventBackupPlanModified, + }, false), + }, + }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAwsBackupVaultNotificationsCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).backupconn + + input := &backup.PutBackupVaultNotificationsInput{ + BackupVaultName: aws.String(d.Get("backup_vault_name").(string)), + SNSTopicArn: aws.String(d.Get("sns_topic_arn").(string)), + BackupVaultEvents: expandStringSet(d.Get("backup_vault_events").(*schema.Set)), + } + + _, err := conn.PutBackupVaultNotifications(input) + if err != nil { + return fmt.Errorf("error creating Backup Vault Notification (%s): %s", d.Id(), err) + } + + d.SetId(d.Get("backup_vault_name").(string)) + + return resourceAwsBackupVaultNotificationsRead(d, meta) +} + +func resourceAwsBackupVaultNotificationsRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).backupconn + + input := &backup.GetBackupVaultNotificationsInput{ + BackupVaultName: aws.String(d.Id()), + } + + resp, err := conn.GetBackupVaultNotifications(input) + + if err != nil { + return fmt.Errorf("error reading Backup Vault (%s): %s", d.Id(), err) + } + d.Set("backup_vault_name", resp.BackupVaultName) + d.Set("sns_topic_arn", resp.SNSTopicArn) + d.Set("arn", resp.BackupVaultArn) + d.Set("backup_vault_events", flattenStringSet(resp.BackupVaultEvents)) + + return nil +} + +func resourceAwsBackupVaultNotificationsDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).backupconn + + input := &backup.DeleteBackupVaultNotificationsInput{ + BackupVaultName: aws.String(d.Id()), + } + + _, err := conn.DeleteBackupVaultNotifications(input) + if err != nil { + return fmt.Errorf("error deleting Backup Vault Notification (%s): %s", d.Id(), err) + } + + return nil +} diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go new file mode 100644 index 00000000000..8ae2bc0cf6d --- /dev/null +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -0,0 +1,130 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/backup" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccAwsBackupVaultNotification_basic(t *testing.T) { + var vault backup.GetBackupVaultNotificationsOutput + + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_backup_vault_notifications.test" + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBackup(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsBackupVaultNotificationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBackupVaultNotificationConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBackupVaultNotificationExists(resourceName, &vault), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAwsBackupVaultNotification_disappears(t *testing.T) { + var vault backup.GetBackupVaultNotificationsOutput + + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_backup_vault_notifications.test" + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBackup(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsBackupVaultNotificationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBackupVaultNotificationConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBackupVaultNotificationExists(resourceName, &vault), + testAccCheckAwsBackupVaultNotificationDisappears(&vault), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckAwsBackupVaultNotificationDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).backupconn + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_backup_vault_notifications" { + continue + } + + input := &backup.DeleteBackupVaultNotificationsInput{ + BackupVaultName: aws.String(rs.Primary.ID), + } + + _, err := conn.DeleteBackupVaultNotifications(input) + + return err + } + + return nil +} + +func testAccCheckAwsBackupVaultNotificationExists(name string, vault *backup.GetBackupVaultNotificationsOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + conn := testAccProvider.Meta().(*AWSClient).backupconn + params := &backup.GetBackupVaultNotificationsInput{ + BackupVaultName: aws.String(rs.Primary.ID), + } + resp, err := conn.GetBackupVaultNotifications(params) + if err != nil { + return err + } + + *vault = *resp + + return nil + } +} + +func testAccCheckAwsBackupVaultNotificationDisappears(vault *backup.GetBackupVaultNotificationsOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).backupconn + params := &backup.DeleteBackupVaultNotificationsInput{ + BackupVaultName: vault.BackupVaultName, + } + _, err := conn.DeleteBackupVaultNotifications(params) + + return err + } +} + +func testAccBackupVaultNotificationConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_backup_vault" "test" { + name = %[1]q +} + +resource "aws_sns_topic" "test" { + name = %[1]q +} + +resource "aws_backup_vault_notifications" "test" { + backup_vault_name = "${aws_backup_vault.test.name}" + sns_topic_arn = "${aws_sns_topic.test.arn}" + backup_vault_events = ["BACKUP_JOB_STARTED", "RESTORE_JOB_COMPLETED"] +} +`, rName) +} diff --git a/website/docs/r/backup_vault_notifications.html.markdown b/website/docs/r/backup_vault_notifications.html.markdown new file mode 100644 index 00000000000..1e36a148ed6 --- /dev/null +++ b/website/docs/r/backup_vault_notifications.html.markdown @@ -0,0 +1,44 @@ +--- +subcategory: "Backup" +layout: "aws" +page_title: "AWS: aws_backup_vault_notifications" +description: |- + Provides an AWS Backup vault notifications resource. +--- + +# Resource: aws_backup_vault_notifications + +Provides an AWS Backup vault notifications resource. + +## Example Usage + +```hcl +resource "aws_backup_vault_notifications" "test" { + backup_vault_name = "example_backup_vault" + sns_topic_arn = "${sns_topic_arn.test.arn}" + backup_vault_events = ["BACKUP_JOB_STARTED", "RESTORE_JOB_COMPLETED"] +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Name of the backup vault to add notifications for. +* `sns_topic_arn` - (Required) The Amazon Resource Name (ARN) that specifies the topic for a backup vault’s events +* `backup_vault_events` - (Required) An array of events that indicate the status of jobs to back up resources to the backup vault. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The name of the vault. +* `arn` - The ARN of the vault. + +## Import + +Backup vault notifications can be imported using the `name`, e.g. + +``` +$ terraform import aws_backup_vault_notifications.test TestVault +``` From 4e779ae8fc83ca32473018033bcd9832d8fb3ed4 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 23 Mar 2020 00:27:00 +0200 Subject: [PATCH 02/19] add sns policy for backup events + docs --- ...rce_aws_backup_vault_notifications_test.go | 15 +++++++++++++++ .../backup_vault_notifications.html.markdown | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index 8ae2bc0cf6d..c62bbf5648f 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -121,6 +121,21 @@ resource "aws_sns_topic" "test" { name = %[1]q } +resource "aws_sns_topic_policy" "test" { + arn = "${aws_sns_topic.test.arn}" + policy = < Date: Mon, 23 Mar 2020 00:28:36 +0200 Subject: [PATCH 03/19] ammend docs --- website/docs/r/backup_vault_notifications.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/backup_vault_notifications.html.markdown b/website/docs/r/backup_vault_notifications.html.markdown index f689dc10c41..3fe0b547935 100644 --- a/website/docs/r/backup_vault_notifications.html.markdown +++ b/website/docs/r/backup_vault_notifications.html.markdown @@ -43,7 +43,7 @@ resource "aws_backup_vault_notifications" "test" { The following arguments are supported: -* `name` - (Required) Name of the backup vault to add notifications for. +* `backup_vault_name` - (Required) Name of the backup vault to add notifications for. * `sns_topic_arn` - (Required) The Amazon Resource Name (ARN) that specifies the topic for a backup vault’s events * `backup_vault_events` - (Required) An array of events that indicate the status of jobs to back up resources to the backup vault. From a6e6d186f73ccf3278506bedf57c2d7c9cedfed2 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 23 Mar 2020 00:48:30 +0200 Subject: [PATCH 04/19] fix destroy check + permissions --- ...resource_aws_backup_vault_notifications.go | 6 +++ ...rce_aws_backup_vault_notifications_test.go | 48 +++++++++++++------ .../backup_vault_notifications.html.markdown | 37 +++++++++----- 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index 6c019e50e41..23b7dab6057 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "log" "regexp" "github.com/aws/aws-sdk-go/aws" @@ -92,6 +93,11 @@ func resourceAwsBackupVaultNotificationsRead(d *schema.ResourceData, meta interf } resp, err := conn.GetBackupVaultNotifications(input) + if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") { + log.Printf("[WARN] Backup Vault Notifcations %s not found, removing from state", d.Id()) + d.SetId("") + return nil + } if err != nil { return fmt.Errorf("error reading Backup Vault (%s): %s", d.Id(), err) diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index c62bbf5648f..a404e41d4cd 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -25,6 +25,7 @@ func TestAccAwsBackupVaultNotification_basic(t *testing.T) { Config: testAccBackupVaultNotificationConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAwsBackupVaultNotificationExists(resourceName, &vault), + resource.TestCheckResourceAttr(resourceName, "backup_vault_events.#", "2"), ), }, { @@ -65,13 +66,17 @@ func testAccCheckAwsBackupVaultNotificationDestroy(s *terraform.State) error { continue } - input := &backup.DeleteBackupVaultNotificationsInput{ + input := &backup.GetBackupVaultNotificationsInput{ BackupVaultName: aws.String(rs.Primary.ID), } - _, err := conn.DeleteBackupVaultNotifications(input) + resp, err := conn.GetBackupVaultNotifications(input) - return err + if err == nil { + if *resp.BackupVaultName == rs.Primary.ID { + return fmt.Errorf("Backup Plan notifications '%s' was not deleted properly", rs.Primary.ID) + } + } } return nil @@ -121,19 +126,32 @@ resource "aws_sns_topic" "test" { name = %[1]q } -resource "aws_sns_topic_policy" "test" { - arn = "${aws_sns_topic.test.arn}" - policy = < Date: Mon, 23 Mar 2020 00:52:43 +0200 Subject: [PATCH 05/19] rename arn attrib --- aws/resource_aws_backup_vault_notifications.go | 4 ++-- website/docs/r/backup_vault_notifications.html.markdown | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index 23b7dab6057..ce2aefdcc0a 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -58,7 +58,7 @@ func resourceAwsBackupVaultNotifications() *schema.Resource { }, false), }, }, - "arn": { + "backup_vault_arn": { Type: schema.TypeString, Computed: true, }, @@ -104,7 +104,7 @@ func resourceAwsBackupVaultNotificationsRead(d *schema.ResourceData, meta interf } d.Set("backup_vault_name", resp.BackupVaultName) d.Set("sns_topic_arn", resp.SNSTopicArn) - d.Set("arn", resp.BackupVaultArn) + d.Set("backup_vault_arn", resp.BackupVaultArn) d.Set("backup_vault_events", flattenStringSet(resp.BackupVaultEvents)) return nil diff --git a/website/docs/r/backup_vault_notifications.html.markdown b/website/docs/r/backup_vault_notifications.html.markdown index b3747777c07..29c8c2f83a9 100644 --- a/website/docs/r/backup_vault_notifications.html.markdown +++ b/website/docs/r/backup_vault_notifications.html.markdown @@ -65,7 +65,7 @@ The following arguments are supported: In addition to all arguments above, the following attributes are exported: * `id` - The name of the vault. -* `arn` - The ARN of the vault. +* `backup_vault_arn` - The ARN of the vault. ## Import From 3ef1116c14ab349ba45da64c6f9bf6c738dc3ad3 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 23 Mar 2020 09:49:40 +0200 Subject: [PATCH 06/19] rename arn attrib --- website/docs/r/backup_vault_notifications.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/backup_vault_notifications.html.markdown b/website/docs/r/backup_vault_notifications.html.markdown index 29c8c2f83a9..b8623f26e74 100644 --- a/website/docs/r/backup_vault_notifications.html.markdown +++ b/website/docs/r/backup_vault_notifications.html.markdown @@ -41,7 +41,7 @@ data "aws_iam_policy_document" "test" { } resource "aws_sns_topic_policy" "test" { - arn = "${aws_sns_topic.test.arn}" + arn = "${aws_sns_topic.test.arn}" policy = "${data.aws_iam_policy_document.test.json}" } From 42c9710aad5fd1e5740d9ba1ab4276a149c7d6b0 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 23 Mar 2020 09:55:55 +0200 Subject: [PATCH 07/19] fix linting problem --- website/docs/r/backup_vault_notifications.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/backup_vault_notifications.html.markdown b/website/docs/r/backup_vault_notifications.html.markdown index b8623f26e74..0019dcfc3f2 100644 --- a/website/docs/r/backup_vault_notifications.html.markdown +++ b/website/docs/r/backup_vault_notifications.html.markdown @@ -48,7 +48,7 @@ resource "aws_sns_topic_policy" "test" { resource "aws_backup_vault_notifications" "test" { backup_vault_name = "example_backup_vault" sns_topic_arn = "${sns_topic_arn.test.arn}" - backup_vault_events = ["BACKUP_JOB_STARTED", "RESTORE_JOB_COMPLETED"] + backup_vault_events = ["BACKUP_JOB_STARTED", "RESTORE_JOB_COMPLETED"] } ``` From 05b5ef66852fd6779812b1aba3cfc104124b25f3 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sat, 16 May 2020 19:43:02 +0300 Subject: [PATCH 08/19] refactor --- ...resource_aws_backup_vault_notifications_test.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index a404e41d4cd..005e4941556 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -51,7 +51,7 @@ func TestAccAwsBackupVaultNotification_disappears(t *testing.T) { Config: testAccBackupVaultNotificationConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAwsBackupVaultNotificationExists(resourceName, &vault), - testAccCheckAwsBackupVaultNotificationDisappears(&vault), + testAccCheckResourceDisappears(testAccProvider, resourceAwsBackupVaultNotifications(), resourceName), ), ExpectNonEmptyPlan: true, }, @@ -104,18 +104,6 @@ func testAccCheckAwsBackupVaultNotificationExists(name string, vault *backup.Get } } -func testAccCheckAwsBackupVaultNotificationDisappears(vault *backup.GetBackupVaultNotificationsOutput) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).backupconn - params := &backup.DeleteBackupVaultNotificationsInput{ - BackupVaultName: vault.BackupVaultName, - } - _, err := conn.DeleteBackupVaultNotifications(params) - - return err - } -} - func testAccBackupVaultNotificationConfig(rName string) string { return fmt.Sprintf(` resource "aws_backup_vault" "test" { From 620ef10d76b277e651bb149b518f7b19b7ade39f Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sun, 31 May 2020 20:10:12 +0300 Subject: [PATCH 09/19] add sweeper --- ...rce_aws_backup_vault_notifications_test.go | 53 +++++++++++++++++++ aws/resource_aws_sns_topic_test.go | 1 + 2 files changed, 54 insertions(+) diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index 005e4941556..4ef06c58f60 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "log" "testing" "github.com/aws/aws-sdk-go/aws" @@ -11,6 +12,58 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/terraform" ) +func init() { + resource.AddTestSweepers("aws_backup_vault_notifications", &resource.Sweeper{ + Name: "aws_backup_vault_notifications", + F: testSweepBackupVaultNotifications, + }) +} + +func testSweepBackupVaultNotifications(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("Error getting client: %s", err) + } + conn := client.(*AWSClient).backupconn + + input := &backup.ListBackupVaultsInput{} + + for { + output, err := conn.ListBackupVaults(input) + if err != nil { + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping Backup Vault Notifications sweep for %s: %s", region, err) + return nil + } + return fmt.Errorf("Error retrieving Backup Vault Notifications: %s", err) + } + + if len(output.BackupVaultList) == 0 { + log.Print("[DEBUG] No Backup Vault Notifications to sweep") + return nil + } + + for _, rule := range output.BackupVaultList { + name := aws.StringValue(rule.BackupVaultName) + + log.Printf("[INFO] Deleting Backup Vault Notifications %s", name) + _, err := conn.DeleteBackupVaultNotifications(&backup.DeleteBackupVaultNotificationsInput{ + BackupVaultName: aws.String(name), + }) + if err != nil { + return fmt.Errorf("Error deleting Backup Vault Notifications %s: %s", name, err) + } + } + + if output.NextToken == nil { + break + } + input.NextToken = output.NextToken + } + + return nil +} + func TestAccAwsBackupVaultNotification_basic(t *testing.T) { var vault backup.GetBackupVaultNotificationsOutput diff --git a/aws/resource_aws_sns_topic_test.go b/aws/resource_aws_sns_topic_test.go index 7c08884c97c..935515d9021 100644 --- a/aws/resource_aws_sns_topic_test.go +++ b/aws/resource_aws_sns_topic_test.go @@ -21,6 +21,7 @@ func init() { F: testSweepSnsTopics, Dependencies: []string{ "aws_autoscaling_group", + "aws_backup_vault_notifications", "aws_budgets_budget", "aws_config_delivery_channel", "aws_dax_cluster", From edf4c2d453a595e0034f1ea4744e4e3c6d722876 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 3 Aug 2020 19:42:02 +0300 Subject: [PATCH 10/19] sdk wrapper and errors --- aws/resource_aws_backup_vault_notifications.go | 9 ++++++--- aws/resource_aws_backup_vault_notifications_test.go | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index ce2aefdcc0a..7c95b848408 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -77,7 +77,7 @@ func resourceAwsBackupVaultNotificationsCreate(d *schema.ResourceData, meta inte _, err := conn.PutBackupVaultNotifications(input) if err != nil { - return fmt.Errorf("error creating Backup Vault Notification (%s): %s", d.Id(), err) + return fmt.Errorf("error creating Backup Vault Notification (%s): %w", d.Id(), err) } d.SetId(d.Get("backup_vault_name").(string)) @@ -100,7 +100,7 @@ func resourceAwsBackupVaultNotificationsRead(d *schema.ResourceData, meta interf } if err != nil { - return fmt.Errorf("error reading Backup Vault (%s): %s", d.Id(), err) + return fmt.Errorf("error reading Backup Vault (%s): %w", d.Id(), err) } d.Set("backup_vault_name", resp.BackupVaultName) d.Set("sns_topic_arn", resp.SNSTopicArn) @@ -119,7 +119,10 @@ func resourceAwsBackupVaultNotificationsDelete(d *schema.ResourceData, meta inte _, err := conn.DeleteBackupVaultNotifications(input) if err != nil { - return fmt.Errorf("error deleting Backup Vault Notification (%s): %s", d.Id(), err) + if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") { + return nil + } + return fmt.Errorf("error deleting Backup Vault Notification (%s): %w", d.Id(), err) } return nil diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index 4ef06c58f60..d86fccb3322 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -22,7 +22,7 @@ func init() { func testSweepBackupVaultNotifications(region string) error { client, err := sharedClientForRegion(region) if err != nil { - return fmt.Errorf("Error getting client: %s", err) + return fmt.Errorf("Error getting client: %w", err) } conn := client.(*AWSClient).backupconn @@ -35,7 +35,7 @@ func testSweepBackupVaultNotifications(region string) error { log.Printf("[WARN] Skipping Backup Vault Notifications sweep for %s: %s", region, err) return nil } - return fmt.Errorf("Error retrieving Backup Vault Notifications: %s", err) + return fmt.Errorf("Error retrieving Backup Vault Notifications: %w", err) } if len(output.BackupVaultList) == 0 { @@ -51,7 +51,7 @@ func testSweepBackupVaultNotifications(region string) error { BackupVaultName: aws.String(name), }) if err != nil { - return fmt.Errorf("Error deleting Backup Vault Notifications %s: %s", name, err) + return fmt.Errorf("Error deleting Backup Vault Notifications %s: %w", name, err) } } @@ -126,7 +126,7 @@ func testAccCheckAwsBackupVaultNotificationDestroy(s *terraform.State) error { resp, err := conn.GetBackupVaultNotifications(input) if err == nil { - if *resp.BackupVaultName == rs.Primary.ID { + if aws.StringValue(resp.BackupVaultName) == rs.Primary.ID { return fmt.Errorf("Backup Plan notifications '%s' was not deleted properly", rs.Primary.ID) } } From 81af541004c0cd1b6280d39d82eb08de4b72814e Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 3 Aug 2020 19:49:03 +0300 Subject: [PATCH 11/19] tf 12 syntax --- aws/resource_aws_backup_vault_notifications_test.go | 8 ++++---- website/docs/r/backup_vault_notifications.html.markdown | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index d86fccb3322..a517e2d9e78 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -191,13 +191,13 @@ data "aws_iam_policy_document" "test" { } resource "aws_sns_topic_policy" "test" { - arn = "${aws_sns_topic.test.arn}" - policy = "${data.aws_iam_policy_document.test.json}" + arn = aws_sns_topic.test.arn + policy = data.aws_iam_policy_document.test.json } resource "aws_backup_vault_notifications" "test" { - backup_vault_name = "${aws_backup_vault.test.name}" - sns_topic_arn = "${aws_sns_topic.test.arn}" + backup_vault_name = aws_backup_vault.test.name + sns_topic_arn = aws_sns_topic.test.arn backup_vault_events = ["BACKUP_JOB_STARTED", "RESTORE_JOB_COMPLETED"] } `, rName) diff --git a/website/docs/r/backup_vault_notifications.html.markdown b/website/docs/r/backup_vault_notifications.html.markdown index 0019dcfc3f2..08231b27289 100644 --- a/website/docs/r/backup_vault_notifications.html.markdown +++ b/website/docs/r/backup_vault_notifications.html.markdown @@ -41,13 +41,13 @@ data "aws_iam_policy_document" "test" { } resource "aws_sns_topic_policy" "test" { - arn = "${aws_sns_topic.test.arn}" - policy = "${data.aws_iam_policy_document.test.json}" + arn = aws_sns_topic.test.arn + policy = data.aws_iam_policy_document.test.json } resource "aws_backup_vault_notifications" "test" { backup_vault_name = "example_backup_vault" - sns_topic_arn = "${sns_topic_arn.test.arn}" + sns_topic_arn = sns_topic_arn.test.arn backup_vault_events = ["BACKUP_JOB_STARTED", "RESTORE_JOB_COMPLETED"] } ``` From 0deab32aae31d8d47f9d4c348a5752f9a3f3b552 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 3 Aug 2020 23:01:03 +0300 Subject: [PATCH 12/19] Update aws/resource_aws_backup_vault_notifications.go Co-authored-by: Kit Ewbank --- aws/resource_aws_backup_vault_notifications.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index 7c95b848408..ecb2ff14993 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -100,7 +100,7 @@ func resourceAwsBackupVaultNotificationsRead(d *schema.ResourceData, meta interf } if err != nil { - return fmt.Errorf("error reading Backup Vault (%s): %w", d.Id(), err) + return fmt.Errorf("error reading Backup Vault Notifications (%s): %w", d.Id(), err) } d.Set("backup_vault_name", resp.BackupVaultName) d.Set("sns_topic_arn", resp.SNSTopicArn) From b04bb2738cac1600bd2253459b18cb7af0adecbe Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 3 Aug 2020 23:01:14 +0300 Subject: [PATCH 13/19] Update aws/resource_aws_backup_vault_notifications.go Co-authored-by: Kit Ewbank --- aws/resource_aws_backup_vault_notifications.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index ecb2ff14993..56381efd2ae 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -77,7 +77,7 @@ func resourceAwsBackupVaultNotificationsCreate(d *schema.ResourceData, meta inte _, err := conn.PutBackupVaultNotifications(input) if err != nil { - return fmt.Errorf("error creating Backup Vault Notification (%s): %w", d.Id(), err) + return fmt.Errorf("error creating Backup Vault Notifications (%s): %w", d.Id(), err) } d.SetId(d.Get("backup_vault_name").(string)) From c01ae2618d2ae84672f578092a6e9802b78b83dd Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 3 Aug 2020 23:02:45 +0300 Subject: [PATCH 14/19] Update aws/resource_aws_backup_vault_notifications.go Co-authored-by: Kit Ewbank --- aws/resource_aws_backup_vault_notifications.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index 56381efd2ae..b77fc91bf86 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -105,7 +105,9 @@ func resourceAwsBackupVaultNotificationsRead(d *schema.ResourceData, meta interf d.Set("backup_vault_name", resp.BackupVaultName) d.Set("sns_topic_arn", resp.SNSTopicArn) d.Set("backup_vault_arn", resp.BackupVaultArn) - d.Set("backup_vault_events", flattenStringSet(resp.BackupVaultEvents)) + if err := d.Set("backup_vault_events", flattenStringSet(resp.BackupVaultEvents)); err != nil { + return fmt.Errorf("error setting backup_vault_events: %w", err) + } return nil } From 46a747e1b83090e19bedd88b7c732620a8005a5c Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 3 Aug 2020 23:02:56 +0300 Subject: [PATCH 15/19] Update aws/resource_aws_backup_vault_notifications.go Co-authored-by: Kit Ewbank --- aws/resource_aws_backup_vault_notifications.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index b77fc91bf86..bde3778e84f 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -124,7 +124,7 @@ func resourceAwsBackupVaultNotificationsDelete(d *schema.ResourceData, meta inte if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") { return nil } - return fmt.Errorf("error deleting Backup Vault Notification (%s): %w", d.Id(), err) + return fmt.Errorf("error deleting Backup Vault Notifications (%s): %w", d.Id(), err) } return nil From e350c06d7147a645c798e722835a109fcb1d0fe4 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Tue, 4 Aug 2020 00:02:41 +0300 Subject: [PATCH 16/19] fmt + sweeper changes --- aws/resource_aws_backup_vault_notifications.go | 2 +- aws/resource_aws_backup_vault_notifications_test.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index bde3778e84f..2ab012184ba 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -106,7 +106,7 @@ func resourceAwsBackupVaultNotificationsRead(d *schema.ResourceData, meta interf d.Set("sns_topic_arn", resp.SNSTopicArn) d.Set("backup_vault_arn", resp.BackupVaultArn) if err := d.Set("backup_vault_events", flattenStringSet(resp.BackupVaultEvents)); err != nil { - return fmt.Errorf("error setting backup_vault_events: %w", err) + return fmt.Errorf("error setting backup_vault_events: %w", err) } return nil diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index a517e2d9e78..8f86131428c 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "github.com/hashicorp/go-multierror" "log" "testing" @@ -25,6 +26,7 @@ func testSweepBackupVaultNotifications(region string) error { return fmt.Errorf("Error getting client: %w", err) } conn := client.(*AWSClient).backupconn + var sweeperErrs *multierror.Error input := &backup.ListBackupVaultsInput{} @@ -35,7 +37,8 @@ func testSweepBackupVaultNotifications(region string) error { log.Printf("[WARN] Skipping Backup Vault Notifications sweep for %s: %s", region, err) return nil } - return fmt.Errorf("Error retrieving Backup Vault Notifications: %w", err) + sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error retrieving Backup Vault Notifications: %w", err)) + return sweeperErrs.ErrorOrNil() } if len(output.BackupVaultList) == 0 { @@ -51,7 +54,10 @@ func testSweepBackupVaultNotifications(region string) error { BackupVaultName: aws.String(name), }) if err != nil { - return fmt.Errorf("Error deleting Backup Vault Notifications %s: %w", name, err) + sweeperErr := fmt.Errorf("error deleting Backup Vault Notifications %s: %w", name, err) + log.Printf("[ERROR] %s", sweeperErr) + sweeperErrs = multierror.Append(sweeperErrs, sweeperErr) + continue } } @@ -61,7 +67,7 @@ func testSweepBackupVaultNotifications(region string) error { input.NextToken = output.NextToken } - return nil + return sweeperErrs.ErrorOrNil() } func TestAccAwsBackupVaultNotification_basic(t *testing.T) { From 5aaf9c36414b7c14d158d4fa41e633c1634ca11a Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 14 Aug 2020 22:56:09 +0300 Subject: [PATCH 17/19] use v2 api --- ...resource_aws_backup_vault_notifications.go | 35 ++++++------------- ...rce_aws_backup_vault_notifications_test.go | 8 ++--- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index 2ab012184ba..38a7fc79736 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -7,8 +7,8 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/backup" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func resourceAwsBackupVaultNotifications() *schema.Resource { @@ -22,10 +22,13 @@ func resourceAwsBackupVaultNotifications() *schema.Resource { Schema: map[string]*schema.Schema{ "backup_vault_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]{1,50}$`), "must consist of lowercase letters, numbers, and hyphens."), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.All( + validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]$`), "must consist of lowercase letters, numbers, and hyphens."), + validation.StringLenBetween(1, 50), + ), }, "sns_topic_arn": { Type: schema.TypeString, @@ -38,24 +41,8 @@ func resourceAwsBackupVaultNotifications() *schema.Resource { Required: true, ForceNew: true, Elem: &schema.Schema{ - Type: schema.TypeString, - ValidateFunc: validation.StringInSlice([]string{ - backup.VaultEventBackupJobStarted, - backup.VaultEventBackupJobCompleted, - backup.VaultEventBackupJobSuccessful, - backup.VaultEventBackupJobFailed, - backup.VaultEventBackupJobExpired, - backup.VaultEventRestoreJobStarted, - backup.VaultEventRestoreJobSuccessful, - backup.VaultEventRestoreJobCompleted, - backup.VaultEventRestoreJobFailed, - backup.VaultEventCopyJobFailed, - backup.VaultEventCopyJobStarted, - backup.VaultEventCopyJobSuccessful, - backup.VaultEventRecoveryPointModified, - backup.VaultEventBackupPlanCreated, - backup.VaultEventBackupPlanModified, - }, false), + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice(backup.VaultEvent_Values(), false), }, }, "backup_vault_arn": { diff --git a/aws/resource_aws_backup_vault_notifications_test.go b/aws/resource_aws_backup_vault_notifications_test.go index 8f86131428c..0f74ae226e3 100644 --- a/aws/resource_aws_backup_vault_notifications_test.go +++ b/aws/resource_aws_backup_vault_notifications_test.go @@ -2,15 +2,15 @@ package aws import ( "fmt" - "github.com/hashicorp/go-multierror" "log" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/backup" - "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func init() { From 129414fb681e7ec2d457db6f761602695603fca8 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Fri, 14 Aug 2020 22:58:09 +0300 Subject: [PATCH 18/19] docs tf12 --- website/docs/r/backup_vault_notifications.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/backup_vault_notifications.html.markdown b/website/docs/r/backup_vault_notifications.html.markdown index 08231b27289..4db423c8d7c 100644 --- a/website/docs/r/backup_vault_notifications.html.markdown +++ b/website/docs/r/backup_vault_notifications.html.markdown @@ -33,7 +33,7 @@ data "aws_iam_policy_document" "test" { } resources = [ - "${aws_sns_topic.test.arn}", + aws_sns_topic.test.arn, ] sid = "__default_statement_ID" From 68bc9aa82cecfa3322ae9e233d01b6b568e4c9b2 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Sun, 23 Aug 2020 00:26:44 +0300 Subject: [PATCH 19/19] revert validation --- aws/resource_aws_backup_vault_notifications.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/aws/resource_aws_backup_vault_notifications.go b/aws/resource_aws_backup_vault_notifications.go index 38a7fc79736..6efed482944 100644 --- a/aws/resource_aws_backup_vault_notifications.go +++ b/aws/resource_aws_backup_vault_notifications.go @@ -22,13 +22,10 @@ func resourceAwsBackupVaultNotifications() *schema.Resource { Schema: map[string]*schema.Schema{ "backup_vault_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.All( - validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]$`), "must consist of lowercase letters, numbers, and hyphens."), - validation.StringLenBetween(1, 50), - ), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_\.]{1,50}$`), "must consist of lowercase letters, numbers, and hyphens."), }, "sns_topic_arn": { Type: schema.TypeString,