Add tags to EBS volumes

This commit is contained in:
Radek Simko 2015-05-29 08:05:21 +01:00 committed by Radek Simko
parent 2e1463ba0f
commit 01399f3240
4 changed files with 51 additions and 3 deletions

View File

@ -17,6 +17,7 @@ func resourceAwsEbsVolume() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Create: resourceAwsEbsVolumeCreate, Create: resourceAwsEbsVolumeCreate,
Read: resourceAwsEbsVolumeRead, Read: resourceAwsEbsVolumeRead,
Update: resourceAWSEbsVolumeUpdate,
Delete: resourceAwsEbsVolumeDelete, Delete: resourceAwsEbsVolumeDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
@ -61,6 +62,7 @@ func resourceAwsEbsVolume() *schema.Resource {
Computed: true, Computed: true,
ForceNew: true, ForceNew: true,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -115,9 +117,23 @@ func resourceAwsEbsVolumeCreate(d *schema.ResourceData, meta interface{}) error
*result.VolumeID, err) *result.VolumeID, err)
} }
d.SetId(*result.VolumeID)
if _, ok := d.GetOk("tags"); ok {
setTags(conn, d)
}
return readVolume(d, result) 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 // volumeStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a the state of a Volume. Returns successfully when volume is available // a the state of a Volume. Returns successfully when volume is available
func volumeStateRefreshFunc(conn *ec2.EC2, volumeID string) resource.StateRefreshFunc { 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 { if volume.VolumeType != nil {
d.Set("type", *volume.VolumeType) d.Set("type", *volume.VolumeType)
} }
if volume.Tags != nil {
d.Set("tags", tagsToMap(volume.Tags))
}
return nil return nil
} }

View File

@ -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 { func testAccCheckVolumeExists(n string, v *ec2.Volume) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
@ -60,3 +76,13 @@ resource "aws_ebs_volume" "test" {
size = 1 size = 1
} }
` `
const testAccAwsEbsVolumeConfigWithTags = `
resource "aws_ebs_volume" "tags_test" {
availability_zone = "us-west-2a"
size = 1
tags {
Name = "TerraformTest"
}
}
`

View File

@ -28,7 +28,7 @@ func setTags(conn *ec2.EC2, d *schema.ResourceData) error {
// Set tags // Set tags
if len(remove) > 0 { 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{ _, err := conn.DeleteTags(&ec2.DeleteTagsInput{
Resources: []*string{aws.String(d.Id())}, Resources: []*string{aws.String(d.Id())},
Tags: remove, Tags: remove,
@ -38,7 +38,7 @@ func setTags(conn *ec2.EC2, d *schema.ResourceData) error {
} }
} }
if len(create) > 0 { 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{ _, err := conn.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{aws.String(d.Id())}, Resources: []*string{aws.String(d.Id())},
Tags: create, Tags: create,

View File

@ -16,6 +16,9 @@ Manages a single EBS volume.
resource "aws_ebs_volume" "example" { resource "aws_ebs_volume" "example" {
availability_zone = "us-west-1a" availability_zone = "us-west-1a"
size = 40 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. * `snapshot_id` (Optional) A snapshot to base the EBS volume off of.
* `type` - (Optional) The type of EBS volume. * `type` - (Optional) The type of EBS volume.
* `kms_key_id` - (Optional) The KMS key ID for the 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 ## Attributes Reference