2016-03-07 21:42:30 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2016-06-21 17:33:12 +02:00
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
|
2016-03-07 21:42:30 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 diffTagsBeanstalk(oldTags, newTags []*elasticbeanstalk.Tag) ([]*elasticbeanstalk.Tag, []*elasticbeanstalk.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 []*elasticbeanstalk.Tag
|
|
|
|
for _, t := range oldTags {
|
|
|
|
old, ok := create[*t.Key]
|
|
|
|
if !ok || old != *t.Value {
|
|
|
|
// Delete it!
|
|
|
|
remove = append(remove, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagsFromMapBeanstalk(create), remove
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsFromMap returns the tags for the given map of data.
|
|
|
|
func tagsFromMapBeanstalk(m map[string]interface{}) []*elasticbeanstalk.Tag {
|
|
|
|
var result []*elasticbeanstalk.Tag
|
|
|
|
for k, v := range m {
|
2016-06-21 17:33:12 +02:00
|
|
|
t := &elasticbeanstalk.Tag{
|
2016-03-07 21:42:30 +01:00
|
|
|
Key: aws.String(k),
|
|
|
|
Value: aws.String(v.(string)),
|
2016-06-21 17:33:12 +02:00
|
|
|
}
|
|
|
|
if !tagIgnoredBeanstalk(t) {
|
|
|
|
result = append(result, t)
|
|
|
|
}
|
2016-03-07 21:42:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsToMap turns the list of tags into a map.
|
|
|
|
func tagsToMapBeanstalk(ts []*elasticbeanstalk.Tag) map[string]string {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for _, t := range ts {
|
2016-06-21 17:33:12 +02:00
|
|
|
if !tagIgnoredBeanstalk(t) {
|
|
|
|
result[*t.Key] = *t.Value
|
|
|
|
}
|
2016-03-07 21:42:30 +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 tagIgnoredBeanstalk(t *elasticbeanstalk.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
|
|
|
|
}
|