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

@ -323,6 +323,7 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
DBSnapshotIdentifier: aws.String(d.Get("snapshot_identifier").(string)), DBSnapshotIdentifier: aws.String(d.Get("snapshot_identifier").(string)),
AutoMinorVersionUpgrade: aws.Bool(d.Get("auto_minor_version_upgrade").(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)),
} }
if attr, ok := d.GetOk("availability_zone"); ok { if attr, ok := d.GetOk("availability_zone"); ok {
@ -440,6 +441,7 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
StorageEncrypted: aws.Bool(d.Get("storage_encrypted").(bool)), StorageEncrypted: aws.Bool(d.Get("storage_encrypted").(bool)),
AutoMinorVersionUpgrade: aws.Bool(d.Get("auto_minor_version_upgrade").(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") attr := d.Get("backup_retention_period")
@ -586,7 +588,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
// list tags for resource // list tags for resource
// set tags // set tags
conn := meta.(*AWSClient).rdsconn conn := meta.(*AWSClient).rdsconn
arn, err := buildRDSARN(d, meta) arn, err := buildRDSARN(d.Id(), meta)
if err != nil { if err != nil {
name := "<empty>" name := "<empty>"
if v.DBName != nil && *v.DBName != "" { 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 { if err := setTagsRDS(conn, d, arn); err != nil {
return err return err
} else { } 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 iamconn := meta.(*AWSClient).iamconn
region := meta.(*AWSClient).region region := meta.(*AWSClient).region
// An zero value GetUserInput{} defers to the currently logged in user // 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 userARN := *resp.User.Arn
accountID := strings.Split(userARN, ":")[4] 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 return arn, nil
} }

View File

@ -3,6 +3,7 @@ package aws
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"math/rand" "math/rand"
"testing" "testing"
@ -216,7 +217,36 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
if newerr.Code() == "DBSnapshotNotFound" { if newerr.Code() == "DBSnapshotNotFound" {
return fmt.Errorf("Snapshot %s not found", snapshot_identifier) 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) log.Printf("[INFO] Deleting the Snapshot %s", snapshot_identifier)
_, snapDeleteErr := conn.DeleteDBSnapshot( _, snapDeleteErr := conn.DeleteDBSnapshot(
&rds.DeleteDBSnapshotInput{ &rds.DeleteDBSnapshotInput{
@ -225,7 +255,7 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
if snapDeleteErr != nil { if snapDeleteErr != nil {
return err return err
} }
} } // end snapshot was found
} }
return nil return nil
@ -391,7 +421,11 @@ resource "aws_db_instance" "snapshot" {
parameter_group_name = "default.mysql5.6" parameter_group_name = "default.mysql5.6"
skip_final_snapshot = false skip_final_snapshot = false
copy_tags_to_snapshot = true
final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-1" final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-1"
tags {
Name = "tf-tags-db"
}
}`, acctest.RandInt()) }`, acctest.RandInt())
} }

View File

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