-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6548 from ewbankkit/issue-4781
Add support for tags on Kinesis Firehose delivery streams
- Loading branch information
Showing
5 changed files
with
332 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
"regexp" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/firehose" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// getTags is a helper to get the tags for a resource. It expects the | ||
// tags field to be named "tags" | ||
func getTagsKinesisFirehose(conn *firehose.Firehose, d *schema.ResourceData, sn string) error { | ||
tags := make([]*firehose.Tag, 0) | ||
var exclusiveStartTagKey string | ||
for { | ||
req := &firehose.ListTagsForDeliveryStreamInput{ | ||
DeliveryStreamName: aws.String(sn), | ||
} | ||
if exclusiveStartTagKey != "" { | ||
req.ExclusiveStartTagKey = aws.String(exclusiveStartTagKey) | ||
} | ||
|
||
resp, err := conn.ListTagsForDeliveryStream(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, tag := range resp.Tags { | ||
tags = append(tags, tag) | ||
} | ||
|
||
// If HasMoreTags is true in the response, more tags are available. | ||
// To list the remaining tags, set ExclusiveStartTagKey to the key | ||
// of the last tag returned and call ListTagsForDeliveryStream again. | ||
if !aws.BoolValue(resp.HasMoreTags) { | ||
break | ||
} | ||
exclusiveStartTagKey = aws.StringValue(tags[len(tags)-1].Key) | ||
} | ||
|
||
if err := d.Set("tags", tagsToMapKinesisFirehose(tags)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// setTags is a helper to set the tags for a resource. It expects the | ||
// tags field to be named "tags" | ||
func setTagsKinesisFirehose(conn *firehose.Firehose, d *schema.ResourceData, sn string) error { | ||
if d.HasChange("tags") { | ||
oraw, nraw := d.GetChange("tags") | ||
o := oraw.(map[string]interface{}) | ||
n := nraw.(map[string]interface{}) | ||
create, remove := diffTagsKinesisFirehose(tagsFromMapKinesisFirehose(o), tagsFromMapKinesisFirehose(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.UntagDeliveryStream(&firehose.UntagDeliveryStreamInput{ | ||
DeliveryStreamName: aws.String(sn), | ||
TagKeys: k, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if len(create) > 0 { | ||
log.Printf("[DEBUG] Creating tags: %#v", create) | ||
_, err := conn.TagDeliveryStream(&firehose.TagDeliveryStreamInput{ | ||
DeliveryStreamName: aws.String(sn), | ||
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 diffTagsKinesisFirehose(oldTags, newTags []*firehose.Tag) ([]*firehose.Tag, []*firehose.Tag) { | ||
// First, we're creating everything we have | ||
create := make(map[string]interface{}) | ||
for _, t := range newTags { | ||
create[aws.StringValue(t.Key)] = aws.StringValue(t.Value) | ||
} | ||
|
||
// Build the list of what to remove | ||
var remove []*firehose.Tag | ||
for _, t := range oldTags { | ||
old, ok := create[aws.StringValue(t.Key)] | ||
if !ok || old != aws.StringValue(t.Value) { | ||
// Delete it! | ||
remove = append(remove, t) | ||
} | ||
} | ||
|
||
return tagsFromMapKinesisFirehose(create), remove | ||
} | ||
|
||
// tagsFromMap returns the tags for the given map of data. | ||
func tagsFromMapKinesisFirehose(m map[string]interface{}) []*firehose.Tag { | ||
result := make([]*firehose.Tag, 0, len(m)) | ||
for k, v := range m { | ||
t := &firehose.Tag{ | ||
Key: aws.String(k), | ||
Value: aws.String(v.(string)), | ||
} | ||
if !tagIgnoredKinesisFirehose(t) { | ||
result = append(result, t) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// tagsToMap turns the list of tags into a map. | ||
func tagsToMapKinesisFirehose(ts []*firehose.Tag) map[string]string { | ||
result := make(map[string]string) | ||
for _, t := range ts { | ||
if !tagIgnoredKinesisFirehose(t) { | ||
result[aws.StringValue(t.Key)] = aws.StringValue(t.Value) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// compare a tag against a list of strings and checks if it should | ||
// be ignored or not | ||
func tagIgnoredKinesisFirehose(t *firehose.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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package aws | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/firehose" | ||
) | ||
|
||
// go test -v -run="TestDiffKinesisFirehoseTags" | ||
func TestDiffKinesisFirehoseTags(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 := diffTagsKinesisFirehose(tagsFromMapKinesisFirehose(tc.Old), tagsFromMapKinesisFirehose(tc.New)) | ||
cm := tagsToMapKinesisFirehose(c) | ||
rm := tagsToMapKinesisFirehose(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="TestIgnoringTagsKinesisFirehose" | ||
func TestIgnoringTagsKinesisFirehose(t *testing.T) { | ||
var ignoredTags []*firehose.Tag | ||
ignoredTags = append(ignoredTags, &firehose.Tag{ | ||
Key: aws.String("aws:cloudformation:logical-id"), | ||
Value: aws.String("foo"), | ||
}) | ||
ignoredTags = append(ignoredTags, &firehose.Tag{ | ||
Key: aws.String("aws:foo:bar"), | ||
Value: aws.String("baz"), | ||
}) | ||
for _, tag := range ignoredTags { | ||
if !tagIgnoredKinesisFirehose(tag) { | ||
t.Fatalf("Tag %v with value %v not ignored, but should be!", *tag.Key, *tag.Value) | ||
} | ||
} | ||
} |
Oops, something went wrong.