2015-03-24 19:37:42 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-06-21 17:33:12 +02:00
|
|
|
"regexp"
|
2015-03-24 19:37:42 +01:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/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 {
|
2015-04-17 21:00:27 +02:00
|
|
|
var result []*elb.Tag
|
2015-03-24 19:37:42 +01:00
|
|
|
for k, v := range m {
|
2016-06-21 17:33:12 +02:00
|
|
|
t := &elb.Tag{
|
2015-03-24 19:37:42 +01:00
|
|
|
Key: aws.String(k),
|
|
|
|
Value: aws.String(v.(string)),
|
2016-06-21 17:33:12 +02:00
|
|
|
}
|
|
|
|
if !tagIgnoredELB(t) {
|
|
|
|
result = append(result, t)
|
|
|
|
}
|
2015-03-24 19:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-06-21 17:33:12 +02:00
|
|
|
if !tagIgnoredELB(t) {
|
|
|
|
result[*t.Key] = *t.Value
|
|
|
|
}
|
2015-03-24 19:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2016-06-21 17:33:12 +02:00
|
|
|
|
|
|
|
// compare a tag against a list of strings and checks if it should
|
|
|
|
// be ignored or not
|
|
|
|
func tagIgnoredELB(t *elb.Tag) bool {
|
|
|
|
filter := []string{"^aws:*"}
|
|
|
|
for _, v := range filter {
|
|
|
|
log.Printf("[DEBUG] Matching %v with %v\n", v, *t.Key)
|
|
|
|
if r, _ := regexp.MatchString(v, *t.Key); r == true {
|
|
|
|
log.Printf("[DEBUG] Found AWS specific tag %s (val: %s), ignoring.\n", *t.Key, *t.Value)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|