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

Add tags to EBS volumes #2135

Merged
merged 1 commit into from
May 29, 2015
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
19 changes: 19 additions & 0 deletions builtin/providers/aws/resource_aws_ebs_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func resourceAwsEbsVolume() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEbsVolumeCreate,
Read: resourceAwsEbsVolumeRead,
Update: resourceAWSEbsVolumeUpdate,
Delete: resourceAwsEbsVolumeDelete,

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -61,6 +62,7 @@ func resourceAwsEbsVolume() *schema.Resource {
Computed: true,
ForceNew: true,
},
"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions builtin/providers/aws/resource_aws_ebs_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
}
}
`
4 changes: 2 additions & 2 deletions builtin/providers/aws/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion website/source/docs/providers/aws/r/ebs_volume.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Manages a single EBS volume.
resource "aws_ebs_volume" "example" {
availability_zone = "us-west-1a"
size = 40
tags {
Name = "HelloWorld"
}
}
```

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

Expand Down