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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_ebs_snapshot_copy - refactor to use keyvaluetags #10936

Merged
merged 5 commits into from
Nov 21, 2019
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
34 changes: 21 additions & 13 deletions aws/resource_aws_ebs_snapshot_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -63,11 +65,7 @@ func resourceAwsEbsSnapshotCopy() *schema.Resource {
Required: true,
ForceNew: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -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))
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
108 changes: 107 additions & 1 deletion aws/resource_aws_ebs_snapshot_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down Expand Up @@ -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"
Expand Down