Skip to content

Commit

Permalink
r/aws_backup_vault: Use 'FindBackupVaultByName'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Oct 25, 2021
1 parent 2a102ac commit d5e6563
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
64 changes: 33 additions & 31 deletions internal/service/backup/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

Expand All @@ -26,29 +27,29 @@ func ResourceVault() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"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."),
"arn": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
"arn": {
Type: schema.TypeString,
Computed: true,
"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."),
},
"recovery_points": {
Type: schema.TypeInt,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -60,8 +61,9 @@ func resourceVaultCreate(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)
input := &backup.CreateBackupVaultInput{
BackupVaultName: aws.String(d.Get("name").(string)),
BackupVaultName: aws.String(name),
BackupVaultTags: Tags(tags.IgnoreAWS()),
}

Expand All @@ -70,11 +72,12 @@ func resourceVaultCreate(d *schema.ResourceData, meta interface{}) error {
}

_, err := conn.CreateBackupVault(input)

if err != nil {
return fmt.Errorf("error creating Backup Vault (%s): %s", d.Id(), err)
return fmt.Errorf("error creating Backup Vault (%s): %w", name, err)
}

d.SetId(d.Get("name").(string))
d.SetId(name)

return resourceVaultRead(d, meta)
}
Expand All @@ -84,34 +87,29 @@ func resourceVaultRead(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

input := &backup.DescribeBackupVaultInput{
BackupVaultName: aws.String(d.Id()),
}
output, err := FindBackupVaultByName(conn, d.Id())

resp, err := conn.DescribeBackupVault(input)
if tfawserr.ErrMessageContains(err, backup.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Backup Vault %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
if tfawserr.ErrMessageContains(err, "AccessDeniedException", "") {
log.Printf("[WARN] Backup Vault %s not found, removing from state", d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Backup Vault (%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)
return fmt.Errorf("error reading Backup Vault (%s): %w", d.Id(), err)
}
d.Set("name", resp.BackupVaultName)
d.Set("kms_key_arn", resp.EncryptionKeyArn)
d.Set("arn", resp.BackupVaultArn)
d.Set("recovery_points", resp.NumberOfRecoveryPoints)

d.Set("arn", output.BackupVaultArn)
d.Set("kms_key_arn", output.EncryptionKeyArn)
d.Set("name", output.BackupVaultName)
d.Set("recovery_points", output.NumberOfRecoveryPoints)

tags, err := ListTags(conn, d.Get("arn").(string))

if err != nil {
return fmt.Errorf("error listing tags for Backup Vault (%s): %s", d.Id(), err)
return fmt.Errorf("error listing tags for Backup Vault (%s): %w", d.Id(), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
Expand All @@ -132,7 +130,7 @@ func resourceVaultUpdate(d *schema.ResourceData, meta interface{}) error {
if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags for Backup Vault (%s): %s", d.Id(), err)
return fmt.Errorf("error updating tags for Backup Vault (%s): %w", d.Id(), err)
}
}

Expand All @@ -147,6 +145,10 @@ func resourceVaultDelete(d *schema.ResourceData, meta interface{}) error {
BackupVaultName: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, backup.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil {
return fmt.Errorf("error deleting Backup Vault (%s): %w", d.Id(), err)
}
Expand Down
31 changes: 17 additions & 14 deletions internal/service/backup/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/backup"
sdkacctest "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"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfbackup "github.com/hashicorp/terraform-provider-aws/internal/service/backup"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func TestAccBackupVault_basic(t *testing.T) {
Expand Down Expand Up @@ -146,17 +146,17 @@ func testAccCheckVaultDestroy(s *terraform.State) error {
continue
}

input := &backup.DescribeBackupVaultInput{
BackupVaultName: aws.String(rs.Primary.ID),
}
_, err := tfbackup.FindBackupVaultByName(conn, rs.Primary.ID)

resp, err := conn.DescribeBackupVault(input)
if tfresource.NotFound(err) {
continue
}

if err == nil {
if *resp.BackupVaultName == rs.Primary.ID {
return fmt.Errorf("Vault '%s' was not deleted properly", rs.Primary.ID)
}
if err != nil {
return err
}

return fmt.Errorf("Backup Vault %s still exists", rs.Primary.ID)
}

return nil
Expand All @@ -169,16 +169,19 @@ func testAccCheckVaultExists(name string, vault *backup.DescribeBackupVaultOutpu
return fmt.Errorf("Not found: %s", name)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).BackupConn
params := &backup.DescribeBackupVaultInput{
BackupVaultName: aws.String(rs.Primary.ID),
if rs.Primary.ID == "" {
return fmt.Errorf("No Backup Vault ID is set")
}
resp, err := conn.DescribeBackupVault(params)

conn := acctest.Provider.Meta().(*conns.AWSClient).BackupConn

output, err := tfbackup.FindBackupVaultByName(conn, rs.Primary.ID)

if err != nil {
return err
}

*vault = *resp
*vault = *output

return nil
}
Expand Down

0 comments on commit d5e6563

Please sign in to comment.