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{
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
}

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 {
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"
}
}
`

View File

@ -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,

View File

@ -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