diff --git a/aws/resource_aws_ebs_snapshot_copy.go b/aws/resource_aws_ebs_snapshot_copy.go index 1e1db5642e4..84e9f19c061 100644 --- a/aws/resource_aws_ebs_snapshot_copy.go +++ b/aws/resource_aws_ebs_snapshot_copy.go @@ -9,12 +9,14 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsEbsSnapshotCopy() *schema.Resource { return &schema.Resource{ Create: resourceAwsEbsSnapshotCopyCreate, Read: resourceAwsEbsSnapshotCopyRead, + Update: resourceAwsEbsSnapshotCopyUpdate, Delete: resourceAwsEbsSnapshotCopyDelete, Schema: map[string]*schema.Schema{ @@ -63,11 +65,7 @@ func resourceAwsEbsSnapshotCopy() *schema.Resource { Required: true, ForceNew: true, }, - "tags": { - Type: schema.TypeMap, - Optional: true, - ForceNew: true, - }, + "tags": tagsSchema(), }, } } @@ -76,8 +74,9 @@ func resourceAwsEbsSnapshotCopyCreate(d *schema.ResourceData, meta interface{}) conn := meta.(*AWSClient).ec2conn request := &ec2.CopySnapshotInput{ - SourceRegion: aws.String(d.Get("source_region").(string)), - SourceSnapshotId: aws.String(d.Get("source_snapshot_id").(string)), + SourceRegion: aws.String(d.Get("source_region").(string)), + SourceSnapshotId: aws.String(d.Get("source_snapshot_id").(string)), + TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeSnapshot), } if v, ok := d.GetOk("description"); ok { request.Description = aws.String(v.(string)) @@ -101,10 +100,6 @@ func resourceAwsEbsSnapshotCopyCreate(d *schema.ResourceData, meta interface{}) return err } - if err := setTags(conn, d); err != nil { - log.Printf("[WARN] error setting tags: %s", err) - } - return resourceAwsEbsSnapshotCopyRead(d, meta) } @@ -132,8 +127,8 @@ func resourceAwsEbsSnapshotCopyRead(d *schema.ResourceData, meta interface{}) er d.Set("kms_key_id", snapshot.KmsKeyId) d.Set("volume_size", snapshot.VolumeSize) - if err := d.Set("tags", tagsToMap(snapshot.Tags)); err != nil { - log.Printf("[WARN] error saving tags to state: %s", err) + if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(snapshot.Tags).IgnoreAws().Map()); err != nil { + return fmt.Errorf("error setting tags: %s", err) } return nil @@ -172,6 +167,19 @@ func resourceAwsEbsSnapshotCopyDelete(d *schema.ResourceData, meta interface{}) return nil } +func resourceAwsEbsSnapshotCopyUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + if d.HasChange("tags") { + o, n := d.GetChange("tags") + if err := keyvaluetags.Ec2UpdateTags(conn, d.Id(), o, n); err != nil { + return fmt.Errorf("error updating tags: %s", err) + } + } + + return resourceAwsEbsSnapshotRead(d, meta) +} + func resourceAwsEbsSnapshotCopyWaitForAvailable(id string, conn *ec2.EC2) error { log.Printf("Waiting for Snapshot %s to become available...", id) diff --git a/aws/resource_aws_ebs_snapshot_copy_test.go b/aws/resource_aws_ebs_snapshot_copy_test.go index 8d1390cf9cd..d2c0fe4da26 100644 --- a/aws/resource_aws_ebs_snapshot_copy_test.go +++ b/aws/resource_aws_ebs_snapshot_copy_test.go @@ -24,7 +24,46 @@ func TestAccAWSEbsSnapshotCopy_basic(t *testing.T) { Config: testAccAwsEbsSnapshotCopyConfig, Check: resource.ComposeTestCheckFunc( testAccCheckEbsSnapshotCopyExists(resourceName, &snapshot), - testAccCheckTags(&snapshot.Tags, "Name", "testAccAwsEbsSnapshotCopyConfig"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.Name", "testAccAwsEbsSnapshotCopyConfig"), + ), + }, + }, + }) +} + +func TestAccAWSEbsSnapshotCopy_tags(t *testing.T) { + var snapshot ec2.Snapshot + resourceName := "aws_ebs_snapshot_copy.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckEbsSnapshotCopyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAwsEbsSnapshotCopyConfigTags1("key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckEbsSnapshotCopyExists(resourceName, &snapshot), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + Config: testAccAwsEbsSnapshotCopyConfigTags2("key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckEbsSnapshotCopyExists(resourceName, &snapshot), + resource.TestCheckResourceAttr(resourceName, "tags.%", "3"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccAwsEbsSnapshotCopyConfigTags1("key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckEbsSnapshotCopyExists(resourceName, &snapshot), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), ), }, }, @@ -223,6 +262,73 @@ resource "aws_ebs_snapshot_copy" "test" { } ` +func testAccAwsEbsSnapshotCopyConfigTags1(tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +data "aws_availability_zones" "available" { + state = "available" +} + +data "aws_region" "current" {} + +resource "aws_ebs_volume" "test" { + availability_zone = "${data.aws_availability_zones.available.names[0]}" + size = 1 +} + +resource "aws_ebs_snapshot" "test" { + volume_id = "${aws_ebs_volume.test.id}" + + tags = { + Name = "testAccAwsEbsSnapshotCopyConfig" + } +} + +resource "aws_ebs_snapshot_copy" "test" { + source_snapshot_id = "${aws_ebs_snapshot.test.id}" + source_region = "${data.aws_region.current.name}" + + tags = { + Name = "testAccAwsEbsSnapshotCopyConfig" + "%s" = "%s" + } +} +`, tagKey1, tagValue1) +} + +func testAccAwsEbsSnapshotCopyConfigTags2(tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +data "aws_availability_zones" "available" { + state = "available" +} + +data "aws_region" "current" {} + +resource "aws_ebs_volume" "test" { + availability_zone = "${data.aws_availability_zones.available.names[0]}" + size = 1 +} + +resource "aws_ebs_snapshot" "test" { + volume_id = "${aws_ebs_volume.test.id}" + + tags = { + Name = "testAccAwsEbsSnapshotCopyConfig" + } +} + +resource "aws_ebs_snapshot_copy" "test" { + source_snapshot_id = "${aws_ebs_snapshot.test.id}" + source_region = "${data.aws_region.current.name}" + + tags = { + Name = "testAccAwsEbsSnapshotCopyConfig" + "%s" = "%s" + "%s" = "%s" + } +} +`, tagKey1, tagValue1, tagKey2, tagValue2) +} + const testAccAwsEbsSnapshotCopyConfigWithDescription = ` data "aws_availability_zones" "available" { state = "available"