Skip to content

Commit

Permalink
Merge pull request #6881 from sunilkumarmohanty/rds-snapshot-tag
Browse files Browse the repository at this point in the history
add support for tags for rds snapshots
  • Loading branch information
bflad authored Dec 18, 2018
2 parents ca3f9ad + f491b97 commit 27a0c0c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
49 changes: 49 additions & 0 deletions aws/resource_aws_db_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func resourceAwsDbSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDbSnapshotCreate,
Read: resourceAwsDbSnapshotRead,
Update: resourceAwsDbSnapshotUpdate,
Delete: resourceAwsDbSnapshotDelete,

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -102,16 +103,19 @@ func resourceAwsDbSnapshot() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}

func resourceAwsDbSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))

params := &rds.CreateDBSnapshotInput{
DBInstanceIdentifier: aws.String(d.Get("db_instance_identifier").(string)),
DBSnapshotIdentifier: aws.String(d.Get("db_snapshot_identifier").(string)),
Tags: tags,
}

_, err := conn.CreateDBSnapshot(params)
Expand Down Expand Up @@ -167,6 +171,9 @@ func resourceAwsDbSnapshotRead(d *schema.ResourceData, meta interface{}) error {
d.Set("snapshot_type", snapshot.SnapshotType)
d.Set("status", snapshot.Status)
d.Set("vpc_id", snapshot.VpcId)
if err := saveTagsRDS(conn, d, aws.StringValue(snapshot.DBSnapshotArn)); err != nil {
log.Printf("[WARN] Failed to save tags for RDS Snapshot (%s): %s", d.Id(), err)
}

return nil
}
Expand All @@ -185,6 +192,48 @@ func resourceAwsDbSnapshotDelete(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceAwsDbSnapshotUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn
arn := d.Get("db_snapshot_arn").(string)
if d.HasChange("tags") {
oldTagsRaw, newTagsRaw := d.GetChange("tags")
oldTagsMap := oldTagsRaw.(map[string]interface{})
newTagsMap := newTagsRaw.(map[string]interface{})
createTags, removeTags := diffTagsRDS(tagsFromMapRDS(oldTagsMap), tagsFromMapRDS(newTagsMap))

if len(removeTags) > 0 {
removeTagKeys := make([]*string, len(removeTags))
for i, removeTag := range removeTags {
removeTagKeys[i] = removeTag.Key
}

input := &rds.RemoveTagsFromResourceInput{
ResourceName: aws.String(arn),
TagKeys: removeTagKeys,
}

log.Printf("[DEBUG] Untagging RDS Cluster: %s", input)
if _, err := conn.RemoveTagsFromResource(input); err != nil {
return fmt.Errorf("error untagging RDS Cluster (%s): %s", d.Id(), err)
}
}

if len(createTags) > 0 {
input := &rds.AddTagsToResourceInput{
ResourceName: aws.String(arn),
Tags: createTags,
}

log.Printf("[DEBUG] Tagging RDS Cluster: %s", input)
if _, err := conn.AddTagsToResource(input); err != nil {
return fmt.Errorf("error tagging RDS Cluster (%s): %s", d.Id(), err)
}
}
}

return nil
}

func resourceAwsDbSnapshotStateRefreshFunc(
d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
Expand Down
89 changes: 89 additions & 0 deletions aws/resource_aws_db_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ func TestAccAWSDBSnapshot_basic(t *testing.T) {
})
}

func TestAccAWSDBSnapshot_tags(t *testing.T) {
var v rds.DBSnapshot
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAwsDbSnapshotConfigTags1(rInt, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbSnapshotExists("aws_db_snapshot.test", &v),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.key1", "value1"),
),
},
{
Config: testAccAwsDbSnapshotConfigTags2(rInt, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbSnapshotExists("aws_db_snapshot.test", &v),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.%", "2"),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.key1", "value1updated"),
resource.TestCheckResourceAttr("aws_db_snapshot.test", "tags.key2", "value2"),
),
},
},
})
}

func testAccCheckDbSnapshotExists(n string, v *rds.DBSnapshot) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -81,3 +109,64 @@ resource "aws_db_snapshot" "test" {
db_snapshot_identifier = "testsnapshot%d"
}`, rInt)
}

func testAccAwsDbSnapshotConfigTags1(rInt int, tag1Key, tag1Value string) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
allocated_storage = 10
engine = "MySQL"
engine_version = "5.6.35"
instance_class = "db.t2.micro"
name = "baz"
password = "barbarbarbar"
username = "foo"
maintenance_window = "Fri:09:00-Fri:09:30"
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
skip_final_snapshot = true
}
resource "aws_db_snapshot" "test" {
db_instance_identifier = "${aws_db_instance.bar.id}"
db_snapshot_identifier = "testsnapshot%d"
tags = {
%q = %q
}
}`, rInt, tag1Key, tag1Value)
}

func testAccAwsDbSnapshotConfigTags2(rInt int, tag1Key, tag1Value, tag2Key, tag2Value string) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
allocated_storage = 10
engine = "MySQL"
engine_version = "5.6.35"
instance_class = "db.t2.micro"
name = "baz"
password = "barbarbarbar"
username = "foo"
maintenance_window = "Fri:09:00-Fri:09:30"
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
skip_final_snapshot = true
}
resource "aws_db_snapshot" "test" {
db_instance_identifier = "${aws_db_instance.bar.id}"
db_snapshot_identifier = "testsnapshot%d"
tags = {
%q = %q
%q = %q
}
}`, rInt, tag1Key, tag1Value, tag2Key, tag2Value)
}
1 change: 1 addition & 0 deletions website/docs/r/db_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The following arguments are supported:

* `db_instance_identifier` - (Required) The DB Instance Identifier from which to take the snapshot.
* `db_snapshot_identifier` - (Required) The Identifier for the snapshot.
* `tags` - (Optional) Key-value mapping of resource tags


## Attributes Reference
Expand Down

0 comments on commit 27a0c0c

Please sign in to comment.