Skip to content

Commit

Permalink
aws_ebs_volume: Refactor tagging logic to keyvaluetags package. (#10778)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSEBSVolume_withTags (22.85s)
--- PASS: TestAccAWSEbsVolumeDataSource_basic (23.47s)
--- PASS: TestAccAWSEBSVolume_NoIops (24.20s)
--- PASS: TestAccAWSEBSVolume_basic (24.24s)
--- PASS: TestAccAWSEbsVolumeDataSource_multipleFilters (24.38s)
--- PASS: TestAccAWSEBSVolume_updateType (39.55s)
--- PASS: TestAccAWSEBSVolume_updateSize (40.22s)
--- PASS: TestAccAWSEBSVolume_updateIops (41.02s)
--- PASS: TestAccAWSEBSVolume_kmsKey (43.46s)
--- PASS: TestAccAWSEBSVolume_updateAttachedEbsVolume (149.67s)
```
  • Loading branch information
ewbankkit authored and bflad committed Nov 7, 2019
1 parent bfbe848 commit 6720d59
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
8 changes: 6 additions & 2 deletions aws/data_source_aws_ebs_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsEbsVolume() *schema.Resource {
Expand Down Expand Up @@ -141,6 +142,9 @@ func volumeDescriptionAttributes(d *schema.ResourceData, client *AWSClient, volu
d.Set("snapshot_id", volume.SnapshotId)
d.Set("volume_type", volume.VolumeType)

err := d.Set("tags", tagsToMap(volume.Tags))
return err
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(volume.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
27 changes: 12 additions & 15 deletions aws/resource_aws_ebs_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"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 resourceAwsEbsVolume() *schema.Resource {
Expand Down Expand Up @@ -80,7 +81,8 @@ func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error
conn := meta.(*AWSClient).ec2conn

request := &ec2.CreateVolumeInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeVolume),
}
if value, ok := d.GetOk("encrypted"); ok {
request.Encrypted = aws.Bool(value.(bool))
Expand All @@ -94,14 +96,6 @@ func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error
if value, ok := d.GetOk("snapshot_id"); ok {
request.SnapshotId = aws.String(value.(string))
}
if value, ok := d.GetOk("tags"); ok {
request.TagSpecifications = []*ec2.TagSpecification{
{
ResourceType: aws.String(ec2.ResourceTypeVolume),
Tags: tagsFromMap(value.(map[string]interface{})),
},
}
}

// IOPs are only valid, and required for, storage type io1. The current minimu
// is 100. Instead of a hard validation we we only apply the IOPs to the
Expand Down Expand Up @@ -154,11 +148,6 @@ func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error

func resourceAWSEbsVolumeUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
if _, ok := d.GetOk("tags"); ok {
if err := setTags(conn, d); err != nil {
return fmt.Errorf("Error updating tags for EBS Volume: %s", err)
}
}

requestUpdate := false
params := &ec2.ModifyVolumeInput{
Expand Down Expand Up @@ -203,6 +192,14 @@ func resourceAWSEbsVolumeUpdate(d *schema.ResourceData, meta interface{}) error
}
}

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 resourceAwsEbsVolumeRead(d, meta)
}

Expand Down Expand Up @@ -268,7 +265,7 @@ func resourceAwsEbsVolumeRead(d *schema.ResourceData, meta interface{}) error {
d.Set("size", aws.Int64Value(volume.Size))
d.Set("snapshot_id", aws.StringValue(volume.SnapshotId))

if err := d.Set("tags", tagsToMap(volume.Tags)); err != nil {
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(volume.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand Down

0 comments on commit 6720d59

Please sign in to comment.