95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
![]() |
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
|
||
|
}
|