2015-11-12 00:37:56 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2016-05-24 19:43:12 +02:00
|
|
|
"log"
|
2016-06-21 17:33:12 +02:00
|
|
|
"regexp"
|
2016-05-24 19:43:12 +02:00
|
|
|
|
2015-11-12 00:37:56 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/redshift"
|
2016-05-24 19:43:12 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-11-12 00:37:56 +01:00
|
|
|
)
|
|
|
|
|
2016-05-24 19:43:12 +02:00
|
|
|
func setTagsRedshift(conn *redshift.Redshift, d *schema.ResourceData, arn string) error {
|
|
|
|
if d.HasChange("tags") {
|
|
|
|
oraw, nraw := d.GetChange("tags")
|
|
|
|
o := oraw.(map[string]interface{})
|
|
|
|
n := nraw.(map[string]interface{})
|
|
|
|
create, remove := diffTagsRedshift(tagsFromMapRedshift(o), tagsFromMapRedshift(n))
|
|
|
|
|
|
|
|
// Set tags
|
|
|
|
if len(remove) > 0 {
|
|
|
|
log.Printf("[DEBUG] Removing tags: %#v", remove)
|
|
|
|
k := make([]*string, len(remove), len(remove))
|
|
|
|
for i, t := range remove {
|
|
|
|
k[i] = t.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.DeleteTags(&redshift.DeleteTagsInput{
|
|
|
|
ResourceName: aws.String(arn),
|
|
|
|
TagKeys: k,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(create) > 0 {
|
|
|
|
log.Printf("[DEBUG] Creating tags: %#v", create)
|
|
|
|
_, err := conn.CreateTags(&redshift.CreateTagsInput{
|
|
|
|
ResourceName: aws.String(arn),
|
|
|
|
Tags: create,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func diffTagsRedshift(oldTags, newTags []*redshift.Tag) ([]*redshift.Tag, []*redshift.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 []*redshift.Tag
|
|
|
|
for _, t := range oldTags {
|
|
|
|
old, ok := create[*t.Key]
|
|
|
|
if !ok || old != *t.Value {
|
|
|
|
// Delete it!
|
|
|
|
remove = append(remove, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagsFromMapRedshift(create), remove
|
|
|
|
}
|
|
|
|
|
2015-11-12 00:37:56 +01:00
|
|
|
func tagsFromMapRedshift(m map[string]interface{}) []*redshift.Tag {
|
|
|
|
result := make([]*redshift.Tag, 0, len(m))
|
|
|
|
for k, v := range m {
|
2016-06-21 17:33:12 +02:00
|
|
|
t := &redshift.Tag{
|
2015-11-12 00:37:56 +01:00
|
|
|
Key: aws.String(k),
|
|
|
|
Value: aws.String(v.(string)),
|
2016-06-21 17:33:12 +02:00
|
|
|
}
|
|
|
|
if !tagIgnoredRedshift(t) {
|
|
|
|
result = append(result, t)
|
|
|
|
}
|
2015-11-12 00:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func tagsToMapRedshift(ts []*redshift.Tag) map[string]string {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for _, t := range ts {
|
2016-06-21 17:33:12 +02:00
|
|
|
if !tagIgnoredRedshift(t) {
|
|
|
|
result[*t.Key] = *t.Value
|
|
|
|
}
|
2015-11-12 00:37:56 +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 tagIgnoredRedshift(t *redshift.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
|
|
|
|
}
|