mirror of https://github.com/hashicorp/terraform
Add tagging support to the 'aws_lambda_function' resource. (#13873)
parent
9aaf220efb
commit
ff9af4c90b
@ -0,0 +1,69 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"log"
|
||||
"regexp"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
)
|
||||
|
||||
// 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 diffTagsGeneric(oldTags, newTags map[string]interface{}) (map[string]*string, map[string]*string) {
|
||||
// First, we're creating everything we have
|
||||
create := make(map[string]*string)
|
||||
for k, v := range newTags {
|
||||
create[k] = aws.String(v.(string))
|
||||
}
|
||||
|
||||
// Build the map of what to remove
|
||||
remove := make(map[string]*string)
|
||||
for k, v := range oldTags {
|
||||
old, ok := create[k]
|
||||
if !ok || old != aws.String(v.(string)) {
|
||||
// Delete it!
|
||||
remove[k] = aws.String(v.(string))
|
||||
}
|
||||
}
|
||||
|
||||
return create, remove
|
||||
}
|
||||
|
||||
// tagsFromMap returns the tags for the given map of data.
|
||||
func tagsFromMapGeneric(m map[string]interface{}) map[string]*string {
|
||||
result := make(map[string]*string)
|
||||
for k, v := range m {
|
||||
if !tagIgnoredGeneric(k) {
|
||||
result[k] = aws.String(v.(string))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// tagsToMap turns the tags into a map.
|
||||
func tagsToMapGeneric(ts map[string]*string) map[string]string {
|
||||
result := make(map[string]string)
|
||||
for k, v := range ts {
|
||||
if !tagIgnoredGeneric(k) {
|
||||
result[k] = aws.StringValue(v)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// compare a tag against a list of strings and checks if it should
|
||||
// be ignored or not
|
||||
func tagIgnoredGeneric(k string) bool {
|
||||
filter := []string{"^aws:*"}
|
||||
for _, v := range filter {
|
||||
log.Printf("[DEBUG] Matching %v with %v\n", v, k)
|
||||
if r, _ := regexp.MatchString(v, k); r == true {
|
||||
log.Printf("[DEBUG] Found AWS specific tag %s, ignoring.\n", k)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@ -0,0 +1,73 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
)
|
||||
|
||||
// go test -v -run="TestDiffGenericTags"
|
||||
func TestDiffGenericTags(t *testing.T) {
|
||||
cases := []struct {
|
||||
Old, New map[string]interface{}
|
||||
Create, Remove map[string]string
|
||||
}{
|
||||
// Basic add/remove
|
||||
{
|
||||
Old: map[string]interface{}{
|
||||
"foo": "bar",
|
||||
},
|
||||
New: map[string]interface{}{
|
||||
"bar": "baz",
|
||||
},
|
||||
Create: map[string]string{
|
||||
"bar": "baz",
|
||||
},
|
||||
Remove: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
|
||||
// Modify
|
||||
{
|
||||
Old: map[string]interface{}{
|
||||
"foo": "bar",
|
||||
},
|
||||
New: map[string]interface{}{
|
||||
"foo": "baz",
|
||||
},
|
||||
Create: map[string]string{
|
||||
"foo": "baz",
|
||||
},
|
||||
Remove: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
c, r := diffTagsGeneric(tc.Old, tc.New)
|
||||
cm := tagsToMapGeneric(c)
|
||||
rm := tagsToMapGeneric(r)
|
||||
if !reflect.DeepEqual(cm, tc.Create) {
|
||||
t.Fatalf("%d: bad create: %#v", i, cm)
|
||||
}
|
||||
if !reflect.DeepEqual(rm, tc.Remove) {
|
||||
t.Fatalf("%d: bad remove: %#v", i, rm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// go test -v -run="TestIgnoringTagsGeneric"
|
||||
func TestIgnoringTagsGeneric(t *testing.T) {
|
||||
ignoredTags := map[string]*string{
|
||||
"aws:cloudformation:logical-id": aws.String("foo"),
|
||||
"aws:foo:bar": aws.String("baz"),
|
||||
}
|
||||
for k, v := range ignoredTags {
|
||||
if !tagIgnoredGeneric(k) {
|
||||
t.Fatalf("Tag %v with value %v not ignored, but should be!", k, *v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/lambda"
|
||||
"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 setTagsLambda(conn *lambda.Lambda, 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 := diffTagsGeneric(o, n)
|
||||
|
||||
// Set tags
|
||||
if len(remove) > 0 {
|
||||
log.Printf("[DEBUG] Removing tags: %#v", remove)
|
||||
keys := make([]*string, 0, len(remove))
|
||||
for k := range remove {
|
||||
keys = append(keys, aws.String(k))
|
||||
}
|
||||
|
||||
_, err := conn.UntagResource(&lambda.UntagResourceInput{
|
||||
Resource: aws.String(arn),
|
||||
TagKeys: keys,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(create) > 0 {
|
||||
log.Printf("[DEBUG] Creating tags: %#v", create)
|
||||
|
||||
_, err := conn.TagResource(&lambda.TagResourceInput{
|
||||
Resource: aws.String(arn),
|
||||
Tags: create,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in new issue