provider/aws: Fix copy_tags_to_snapshot for DB Instance

This commit is contained in:
clint shryock 2016-02-18 11:33:21 -06:00
parent 396dc303ac
commit ca29437581
3 changed files with 46 additions and 10 deletions

View File

@ -322,7 +322,8 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
DBInstanceIdentifier: aws.String(d.Get("identifier").(string)),
DBSnapshotIdentifier: aws.String(d.Get("snapshot_identifier").(string)),
AutoMinorVersionUpgrade: aws.Bool(d.Get("auto_minor_version_upgrade").(bool)),
Tags: tags,
Tags: tags,
CopyTagsToSnapshot: aws.Bool(d.Get("copy_tags_to_snapshot").(bool)),
}
if attr, ok := d.GetOk("availability_zone"); ok {
@ -439,7 +440,8 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
EngineVersion: aws.String(d.Get("engine_version").(string)),
StorageEncrypted: aws.Bool(d.Get("storage_encrypted").(bool)),
AutoMinorVersionUpgrade: aws.Bool(d.Get("auto_minor_version_upgrade").(bool)),
Tags: tags,
Tags: tags,
CopyTagsToSnapshot: aws.Bool(d.Get("copy_tags_to_snapshot").(bool)),
}
attr := d.Get("backup_retention_period")
@ -586,7 +588,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
// list tags for resource
// set tags
conn := meta.(*AWSClient).rdsconn
arn, err := buildRDSARN(d, meta)
arn, err := buildRDSARN(d.Id(), meta)
if err != nil {
name := "<empty>"
if v.DBName != nil && *v.DBName != "" {
@ -825,7 +827,7 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
}
}
if arn, err := buildRDSARN(d, meta); err == nil {
if arn, err := buildRDSARN(d.Id(), meta); err == nil {
if err := setTagsRDS(conn, d, arn); err != nil {
return err
} else {
@ -888,7 +890,7 @@ func resourceAwsDbInstanceStateRefreshFunc(
}
}
func buildRDSARN(d *schema.ResourceData, meta interface{}) (string, error) {
func buildRDSARN(identifier string, meta interface{}) (string, error) {
iamconn := meta.(*AWSClient).iamconn
region := meta.(*AWSClient).region
// An zero value GetUserInput{} defers to the currently logged in user
@ -898,6 +900,6 @@ func buildRDSARN(d *schema.ResourceData, meta interface{}) (string, error) {
}
userARN := *resp.User.Arn
accountID := strings.Split(userARN, ":")[4]
arn := fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", region, accountID, d.Id())
arn := fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", region, accountID, identifier)
return arn, nil
}

View File

@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"math/rand"
"testing"
@ -216,7 +217,36 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
if newerr.Code() == "DBSnapshotNotFound" {
return fmt.Errorf("Snapshot %s not found", snapshot_identifier)
}
} else {
} else { // snapshot was found
// verify we have the tags copied to the snapshot
instanceARN, err := buildRDSARN(snapshot_identifier, testAccProvider.Meta())
// tags have a different ARN, just swapping :db: for :snapshot:
tagsARN := strings.Replace(instanceARN, ":db:", ":snapshot:", 1)
if err != nil {
return fmt.Errorf("Error building ARN for tags check with ARN (%s): %s", tagsARN, err)
}
resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{
ResourceName: aws.String(tagsARN),
})
if err != nil {
return fmt.Errorf("Error retrieving tags for ARN (%s): ", tagsARN, err)
}
if resp.TagList == nil || len(resp.TagList) == 0 {
return fmt.Errorf("Tag list is nil or zero: %s", resp.TagList)
}
var found bool
for _, t := range resp.TagList {
if *t.Key == "Name" && *t.Value == "tf-tags-db" {
found = true
}
}
if !found {
return fmt.Errorf("Expected to find tag Name (%s), but wasn't found. Tags: %s", "tf-tags-db", resp.TagList)
}
// end tag search
log.Printf("[INFO] Deleting the Snapshot %s", snapshot_identifier)
_, snapDeleteErr := conn.DeleteDBSnapshot(
&rds.DeleteDBSnapshotInput{
@ -225,7 +255,7 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
if snapDeleteErr != nil {
return err
}
}
} // end snapshot was found
}
return nil
@ -391,7 +421,11 @@ resource "aws_db_instance" "snapshot" {
parameter_group_name = "default.mysql5.6"
skip_final_snapshot = false
copy_tags_to_snapshot = true
final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-1"
tags {
Name = "tf-tags-db"
}
}`, acctest.RandInt())
}

View File

@ -165,7 +165,7 @@ func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{})
d.Set("publicly_accessible", db.PubliclyAccessible)
// Fetch and save tags
arn, err := buildRDSARN(d, meta)
arn, err := buildRDSARN(d.Id(), meta)
if err != nil {
log.Printf("[DEBUG] Error building ARN for RDS Cluster Instance (%s), not setting Tags", *db.DBInstanceIdentifier)
} else {
@ -180,7 +180,7 @@ func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{})
func resourceAwsRDSClusterInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn
if arn, err := buildRDSARN(d, meta); err == nil {
if arn, err := buildRDSARN(d.Id(), meta); err == nil {
if err := setTagsRDS(conn, d, arn); err != nil {
return err
}