2015-03-24 19:37:42 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2015-04-14 23:41:36 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/elb"
|
2015-03-24 19:37:42 +01:00
|
|
|
"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)
|
2015-04-14 23:41:36 +02:00
|
|
|
k := make([]*elb.TagKeyOnly, 0, len(remove))
|
2015-03-24 19:37:42 +01:00
|
|
|
for _, t := range remove {
|
2015-04-14 23:41:36 +02:00
|
|
|
k = append(k, &elb.TagKeyOnly{Key: t.Key})
|
2015-03-24 19:37:42 +01:00
|
|
|
}
|
|
|
|
_, err := conn.RemoveTags(&elb.RemoveTagsInput{
|
2015-04-14 23:41:36 +02:00
|
|
|
LoadBalancerNames: []*string{aws.String(d.Get("name").(string))},
|
2015-03-24 19:37:42 +01:00
|
|
|
Tags: k,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(create) > 0 {
|
|
|
|
log.Printf("[DEBUG] Creating tags: %#v", create)
|
|
|
|
_, err := conn.AddTags(&elb.AddTagsInput{
|
2015-04-14 23:41:36 +02:00
|
|
|
LoadBalancerNames: []*string{aws.String(d.Get("name").(string))},
|
2015-03-24 19:37:42 +01:00
|
|
|
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.
|
2015-04-14 23:41:36 +02:00
|
|
|
func diffTagsELB(oldTags, newTags []*elb.Tag) ([]*elb.Tag, []*elb.Tag) {
|
2015-03-24 19:37:42 +01:00
|
|
|
// 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
|
2015-04-14 23:41:36 +02:00
|
|
|
var remove []*elb.Tag
|
2015-03-24 19:37:42 +01:00
|
|
|
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.
|
2015-04-14 23:41:36 +02:00
|
|
|
func tagsFromMapELB(m map[string]interface{}) []*elb.Tag {
|
|
|
|
result := make([]*elb.Tag, 0, len(m))
|
2015-03-24 19:37:42 +01:00
|
|
|
for k, v := range m {
|
2015-04-14 23:41:36 +02:00
|
|
|
result = append(result, &elb.Tag{
|
2015-03-24 19:37:42 +01:00
|
|
|
Key: aws.String(k),
|
|
|
|
Value: aws.String(v.(string)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsToMap turns the list of tags into a map.
|
2015-04-14 23:41:36 +02:00
|
|
|
func tagsToMapELB(ts []*elb.Tag) map[string]string {
|
2015-03-24 19:37:42 +01:00
|
|
|
result := make(map[string]string)
|
|
|
|
for _, t := range ts {
|
|
|
|
result[*t.Key] = *t.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|