diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 7898f2120..b15fe1afa 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -154,6 +154,8 @@ func resourceAwsElb() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "tags": tagsSchema(), }, } } @@ -167,11 +169,12 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error { return err } + tags := tagsFromMapELB(d.Get("tags").(map[string]interface{})) // Provision the elb - elbOpts := &elb.CreateAccessPointInput{ LoadBalancerName: aws.String(d.Get("name").(string)), Listeners: listeners, + Tags: tags, } if scheme, ok := d.GetOk("internal"); ok && scheme.(bool) { @@ -208,6 +211,8 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error { d.SetPartial("security_groups") d.SetPartial("subnets") + d.Set("tags", tagsToMapELB(tags)) + if d.HasChange("health_check") { vs := d.Get("health_check").(*schema.Set).List() if len(vs) > 0 { @@ -267,6 +272,15 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { d.Set("security_groups", lb.SecurityGroups) d.Set("subnets", lb.Subnets) + resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{ + LoadBalancerNames: []string{*lb.LoadBalancerName}, + }) + + var et []elb.Tag + if len(resp.TagDescriptions) > 0 { + et = resp.TagDescriptions[0].Tags + } + d.Set("tags", tagsToMapELB(et)) // There's only one health check, so save that to state as we // currently can if *lb.HealthCheck.Target != "" { @@ -357,6 +371,11 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { } } + if err := setTagsELB(elbconn, d); err != nil { + return err + } else { + d.SetPartial("tags") + } d.Partial(false) return resourceAwsElbRead(d, meta) diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index 037a9557d..2fbe7ace8 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -53,6 +53,61 @@ func TestAccAWSELB_basic(t *testing.T) { }) } +func TestAccAWSELB_tags(t *testing.T) { + var conf elb.LoadBalancerDescription + var td elb.TagDescription + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSELBDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSELBConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSELBExists("aws_elb.bar", &conf), + testAccCheckAWSELBAttributes(&conf), + resource.TestCheckResourceAttr( + "aws_elb.bar", "name", "foobar-terraform-test"), + testAccLoadTags(&conf, &td), + testAccCheckELBTags(&td.Tags, "bar", "baz"), + ), + }, + + resource.TestStep{ + Config: testAccAWSELBConfig_TagUpdate, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSELBExists("aws_elb.bar", &conf), + testAccCheckAWSELBAttributes(&conf), + resource.TestCheckResourceAttr( + "aws_elb.bar", "name", "foobar-terraform-test"), + testAccLoadTags(&conf, &td), + testAccCheckELBTags(&td.Tags, "foo", "bar"), + testAccCheckELBTags(&td.Tags, "new", "type"), + ), + }, + }, + }) +} + +func testAccLoadTags(conf *elb.LoadBalancerDescription, td *elb.TagDescription) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).elbconn + + describe, err := conn.DescribeTags(&elb.DescribeTagsInput{ + LoadBalancerNames: []string{*conf.LoadBalancerName}, + }) + + if err != nil { + return err + } + if len(describe.TagDescriptions) > 0 { + *td = describe.TagDescriptions[0] + } + return nil + } +} + func TestAccAWSELB_InstanceAttaching(t *testing.T) { var conf elb.LoadBalancerDescription @@ -288,6 +343,31 @@ resource "aws_elb" "bar" { lb_protocol = "http" } + tags { + bar = "baz" + } + + cross_zone_load_balancing = true +} +` + +const testAccAWSELBConfig_TagUpdate = ` +resource "aws_elb" "bar" { + name = "foobar-terraform-test" + availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] + + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } + + tags { + foo = "bar" + new = "type" + } + cross_zone_load_balancing = true } ` diff --git a/builtin/providers/aws/tagsELB.go b/builtin/providers/aws/tagsELB.go new file mode 100644 index 000000000..ad5e0752e --- /dev/null +++ b/builtin/providers/aws/tagsELB.go @@ -0,0 +1,94 @@ +package aws + +import ( + "log" + + "github.com/hashicorp/aws-sdk-go/aws" + "github.com/hashicorp/aws-sdk-go/gen/elb" + "github.com/hashicorp/terraform/helper/schema" +) + +// setTags is a helper to set the tags for a resource. It expects the +// tags field to be named "tags" +func setTagsELB(conn *elb.ELB, d *schema.ResourceData) error { + if d.HasChange("tags") { + oraw, nraw := d.GetChange("tags") + o := oraw.(map[string]interface{}) + n := nraw.(map[string]interface{}) + create, remove := diffTagsELB(tagsFromMapELB(o), tagsFromMapELB(n)) + + // Set tags + if len(remove) > 0 { + log.Printf("[DEBUG] Removing tags: %#v", remove) + k := make([]elb.TagKeyOnly, 0, len(remove)) + for _, t := range remove { + k = append(k, elb.TagKeyOnly{Key: t.Key}) + } + _, err := conn.RemoveTags(&elb.RemoveTagsInput{ + LoadBalancerNames: []string{d.Get("name").(string)}, + Tags: k, + }) + if err != nil { + return err + } + } + if len(create) > 0 { + log.Printf("[DEBUG] Creating tags: %#v", create) + _, err := conn.AddTags(&elb.AddTagsInput{ + LoadBalancerNames: []string{d.Get("name").(string)}, + Tags: create, + }) + if err != nil { + return err + } + } + } + + return nil +} + +// diffTags takes our tags locally and the ones remotely and returns +// the set of tags that must be created, and the set of tags that must +// be destroyed. +func diffTagsELB(oldTags, newTags []elb.Tag) ([]elb.Tag, []elb.Tag) { + // First, we're creating everything we have + create := make(map[string]interface{}) + for _, t := range newTags { + create[*t.Key] = *t.Value + } + + // Build the list of what to remove + var remove []elb.Tag + for _, t := range oldTags { + old, ok := create[*t.Key] + if !ok || old != *t.Value { + // Delete it! + remove = append(remove, t) + } + } + + return tagsFromMapELB(create), remove +} + +// tagsFromMap returns the tags for the given map of data. +func tagsFromMapELB(m map[string]interface{}) []elb.Tag { + result := make([]elb.Tag, 0, len(m)) + for k, v := range m { + result = append(result, elb.Tag{ + Key: aws.String(k), + Value: aws.String(v.(string)), + }) + } + + return result +} + +// tagsToMap turns the list of tags into a map. +func tagsToMapELB(ts []elb.Tag) map[string]string { + result := make(map[string]string) + for _, t := range ts { + result[*t.Key] = *t.Value + } + + return result +} diff --git a/builtin/providers/aws/tagsELB_test.go b/builtin/providers/aws/tagsELB_test.go new file mode 100644 index 000000000..79021b4dd --- /dev/null +++ b/builtin/providers/aws/tagsELB_test.go @@ -0,0 +1,85 @@ +package aws + +import ( + "fmt" + "reflect" + "testing" + + "github.com/hashicorp/aws-sdk-go/gen/elb" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestDiffELBTags(t *testing.T) { + cases := []struct { + Old, New map[string]interface{} + Create, Remove map[string]string + }{ + // Basic add/remove + { + Old: map[string]interface{}{ + "foo": "bar", + }, + New: map[string]interface{}{ + "bar": "baz", + }, + Create: map[string]string{ + "bar": "baz", + }, + Remove: map[string]string{ + "foo": "bar", + }, + }, + + // Modify + { + Old: map[string]interface{}{ + "foo": "bar", + }, + New: map[string]interface{}{ + "foo": "baz", + }, + Create: map[string]string{ + "foo": "baz", + }, + Remove: map[string]string{ + "foo": "bar", + }, + }, + } + + for i, tc := range cases { + c, r := diffTagsELB(tagsFromMapELB(tc.Old), tagsFromMapELB(tc.New)) + cm := tagsToMapELB(c) + rm := tagsToMapELB(r) + if !reflect.DeepEqual(cm, tc.Create) { + t.Fatalf("%d: bad create: %#v", i, cm) + } + if !reflect.DeepEqual(rm, tc.Remove) { + t.Fatalf("%d: bad remove: %#v", i, rm) + } + } +} + +// testAccCheckTags can be used to check the tags on a resource. +func testAccCheckELBTags( + ts *[]elb.Tag, key string, value string) resource.TestCheckFunc { + return func(s *terraform.State) error { + m := tagsToMapELB(*ts) + v, ok := m[key] + if value != "" && !ok { + return fmt.Errorf("Missing tag: %s", key) + } else if value == "" && ok { + return fmt.Errorf("Extra tag: %s", key) + } + if value == "" { + return nil + } + + if v != value { + return fmt.Errorf("%s: bad value: %s", key, v) + } + + return nil + } +}