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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update step for change tags for aws_backup_vault resource #7933

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
56 changes: 55 additions & 1 deletion aws/resource_aws_backup_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"regexp"

"github.com/hashicorp/terraform/helper/validation"
Expand All @@ -15,6 +16,7 @@ func resourceAwsBackupVault() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBackupVaultCreate,
Read: resourceAwsBackupVaultRead,
Update: resourceAwsBackupVaultUpdate,
Delete: resourceAwsBackupVaultDelete,

Schema: map[string]*schema.Schema{
Expand All @@ -27,7 +29,6 @@ func resourceAwsBackupVault() *schema.Resource {
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"kms_key_arn": {
Expand Down Expand Up @@ -82,6 +83,11 @@ func resourceAwsBackupVaultRead(d *schema.ResourceData, meta interface{}) error
}

resp, err := conn.DescribeBackupVault(input)
if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") {
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)
}
Expand All @@ -105,6 +111,54 @@ func resourceAwsBackupVaultRead(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceAwsBackupVaultUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn

if d.HasChange("tags") {
resourceArn := d.Get("arn").(string)
oraw, nraw := d.GetChange("tags")
create, remove := diffTagsGeneric(oraw.(map[string]interface{}), nraw.(map[string]interface{}))

if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %#v", remove)
keys := make([]*string, 0, len(remove))
for k := range remove {
keys = append(keys, aws.String(k))
}

_, err := conn.UntagResource(&backup.UntagResourceInput{
ResourceArn: aws.String(resourceArn),
TagKeyList: keys,
})
if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Backup Vault %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error removing tags for (%s): %s", d.Id(), err)
}
}
if len(create) > 0 {
log.Printf("[DEBUG] Creating tags: %#v", create)
_, err := conn.TagResource(&backup.TagResourceInput{
ResourceArn: aws.String(resourceArn),
Tags: create,
})
if isAWSErr(err, backup.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Backup Vault %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error setting tags for (%s): %s", d.Id(), err)
}
}
}

return resourceAwsBackupVaultRead(d, meta)
}

func resourceAwsBackupVaultDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).backupconn

Expand Down
48 changes: 48 additions & 0 deletions aws/resource_aws_backup_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ func TestAccAwsBackupVault_withTags(t *testing.T) {
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.left", "right"),
),
},
{
Config: testAccBackupVaultWithUpdateTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupVaultExists("aws_backup_vault.test", &vault),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.%", "4"),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.up", "downdown"),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.left", "rightright"),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.foo", "bar"),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.fizz", "buzz"),
),
},
{
Config: testAccBackupVaultWithRemoveTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBackupVaultExists("aws_backup_vault.test", &vault),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.%", "2"),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.foo", "bar"),
resource.TestCheckResourceAttr("aws_backup_vault.test", "tags.fizz", "buzz"),
),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to test adding/removing tags too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Added test case.

$ make testacc TEST=./aws TESTARGS='-run=TestAccAwsBackupVault_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -parallel 20 -run=TestAccAwsBackupVault_ -timeout 120m
=== RUN   TestAccAwsBackupVault_basic
=== PAUSE TestAccAwsBackupVault_basic
=== RUN   TestAccAwsBackupVault_withKmsKey
=== PAUSE TestAccAwsBackupVault_withKmsKey
=== RUN   TestAccAwsBackupVault_withTags
=== PAUSE TestAccAwsBackupVault_withTags
=== CONT  TestAccAwsBackupVault_basic
=== CONT  TestAccAwsBackupVault_withTags
=== CONT  TestAccAwsBackupVault_withKmsKey
--- PASS: TestAccAwsBackupVault_basic (33.29s)
--- PASS: TestAccAwsBackupVault_withKmsKey (66.19s)
--- PASS: TestAccAwsBackupVault_withTags (74.41s)
PASS
ok  	github.com/terraform-providers/tmp-terraform-provider-aws/aws	74.504s

},
})
}
Expand Down Expand Up @@ -151,3 +171,31 @@ resource "aws_backup_vault" "test" {
}
`, randInt)
}

func testAccBackupVaultWithUpdateTags(randInt int) string {
return fmt.Sprintf(`
resource "aws_backup_vault" "test" {
name = "tf_acc_test_backup_vault_%d"

tags = {
up = "downdown"
left = "rightright"
foo = "bar"
fizz = "buzz"
}
}
`, randInt)
}

func testAccBackupVaultWithRemoveTags(randInt int) string {
return fmt.Sprintf(`
resource "aws_backup_vault" "test" {
name = "tf_acc_test_backup_vault_%d"

tags = {
foo = "bar"
fizz = "buzz"
}
}
`, randInt)
}