From 01399f3240004b52f64bfd745bce5b7b704361d9 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Fri, 29 May 2015 08:05:21 +0100 Subject: [PATCH] Add tags to EBS volumes --- .../providers/aws/resource_aws_ebs_volume.go | 19 ++++++++++++++ .../aws/resource_aws_ebs_volume_test.go | 26 +++++++++++++++++++ builtin/providers/aws/tags.go | 4 +-- .../docs/providers/aws/r/ebs_volume.html.md | 5 +++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_ebs_volume.go b/builtin/providers/aws/resource_aws_ebs_volume.go index 450fd7f74..9382605f8 100644 --- a/builtin/providers/aws/resource_aws_ebs_volume.go +++ b/builtin/providers/aws/resource_aws_ebs_volume.go @@ -17,6 +17,7 @@ func resourceAwsEbsVolume() *schema.Resource { return &schema.Resource{ Create: resourceAwsEbsVolumeCreate, Read: resourceAwsEbsVolumeRead, + Update: resourceAWSEbsVolumeUpdate, Delete: resourceAwsEbsVolumeDelete, Schema: map[string]*schema.Schema{ @@ -61,6 +62,7 @@ func resourceAwsEbsVolume() *schema.Resource { Computed: true, ForceNew: true, }, + "tags": tagsSchema(), }, } } @@ -115,9 +117,23 @@ func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error *result.VolumeID, err) } + d.SetId(*result.VolumeID) + + if _, ok := d.GetOk("tags"); ok { + setTags(conn, d) + } + return readVolume(d, result) } +func resourceAWSEbsVolumeUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + if _, ok := d.GetOk("tags"); ok { + setTags(conn, d) + } + return resourceAwsEbsVolumeRead(d, meta) +} + // volumeStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // a the state of a Volume. Returns successfully when volume is available func volumeStateRefreshFunc(conn *ec2.EC2, volumeID string) resource.StateRefreshFunc { @@ -198,6 +214,9 @@ func readVolume(d *schema.ResourceData, volume *ec2.Volume) error { if volume.VolumeType != nil { d.Set("type", *volume.VolumeType) } + if volume.Tags != nil { + d.Set("tags", tagsToMap(volume.Tags)) + } return nil } diff --git a/builtin/providers/aws/resource_aws_ebs_volume_test.go b/builtin/providers/aws/resource_aws_ebs_volume_test.go index d5bc21203..7fb669707 100644 --- a/builtin/providers/aws/resource_aws_ebs_volume_test.go +++ b/builtin/providers/aws/resource_aws_ebs_volume_test.go @@ -26,6 +26,22 @@ func TestAccAWSEBSVolume(t *testing.T) { }) } +func TestAccAWSEBSVolume_withTags(t *testing.T) { + var v ec2.Volume + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAwsEbsVolumeConfigWithTags, + Check: resource.ComposeTestCheckFunc( + testAccCheckVolumeExists("aws_ebs_volume.tags_test", &v), + ), + }, + }, + }) +} + func testAccCheckVolumeExists(n string, v *ec2.Volume) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -60,3 +76,13 @@ resource "aws_ebs_volume" "test" { size = 1 } ` + +const testAccAwsEbsVolumeConfigWithTags = ` +resource "aws_ebs_volume" "tags_test" { + availability_zone = "us-west-2a" + size = 1 + tags { + Name = "TerraformTest" + } +} +` diff --git a/builtin/providers/aws/tags.go b/builtin/providers/aws/tags.go index 07a64bbaf..88934c799 100644 --- a/builtin/providers/aws/tags.go +++ b/builtin/providers/aws/tags.go @@ -28,7 +28,7 @@ func setTags(conn *ec2.EC2, d *schema.ResourceData) error { // Set tags if len(remove) > 0 { - log.Printf("[DEBUG] Removing tags: %#v", remove) + log.Printf("[DEBUG] Removing tags: %#v from %s", remove, d.Id()) _, err := conn.DeleteTags(&ec2.DeleteTagsInput{ Resources: []*string{aws.String(d.Id())}, Tags: remove, @@ -38,7 +38,7 @@ func setTags(conn *ec2.EC2, d *schema.ResourceData) error { } } if len(create) > 0 { - log.Printf("[DEBUG] Creating tags: %#v", create) + log.Printf("[DEBUG] Creating tags: %#v for %s", create, d.Id()) _, err := conn.CreateTags(&ec2.CreateTagsInput{ Resources: []*string{aws.String(d.Id())}, Tags: create, diff --git a/website/source/docs/providers/aws/r/ebs_volume.html.md b/website/source/docs/providers/aws/r/ebs_volume.html.md index eee5f99d4..00bb639a6 100644 --- a/website/source/docs/providers/aws/r/ebs_volume.html.md +++ b/website/source/docs/providers/aws/r/ebs_volume.html.md @@ -16,6 +16,9 @@ Manages a single EBS volume. resource "aws_ebs_volume" "example" { availability_zone = "us-west-1a" size = 40 + tags { + Name = "HelloWorld" + } } ``` @@ -30,7 +33,7 @@ The following arguments are supported: * `snapshot_id` (Optional) A snapshot to base the EBS volume off of. * `type` - (Optional) The type of EBS volume. * `kms_key_id` - (Optional) The KMS key ID for the volume. - +* `tags` - (Optional) A mapping of tags to assign to the resource. ## Attributes Reference