-
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 #8076 from kterada0509/feature/add-support-resourc…
…e-tags-for-aws_cloudwatch_event_rule-resource Feature/add support resource tags for aws cloudwatch event rule resource
- Loading branch information
Showing
5 changed files
with
311 additions
and
1 deletion.
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,133 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
events "github.com/aws/aws-sdk-go/service/cloudwatchevents" | ||
"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 setTagsCloudWatchEvents(conn *events.CloudWatchEvents, 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 := diffTagsCloudWatchEvents(tagsFromMapCloudWatchEvents(o), tagsFromMapCloudWatchEvents(n)) | ||
|
||
// Set tags | ||
if len(remove) > 0 { | ||
log.Printf("[DEBUG] Removing tags: %#v", remove) | ||
k := make([]*string, 0, len(remove)) | ||
for _, t := range remove { | ||
k = append(k, t.Key) | ||
} | ||
_, err := conn.UntagResource(&events.UntagResourceInput{ | ||
ResourceARN: aws.String(arn), | ||
TagKeys: k, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if len(create) > 0 { | ||
log.Printf("[DEBUG] Creating tags: %#v", create) | ||
_, err := conn.TagResource(&events.TagResourceInput{ | ||
ResourceARN: aws.String(arn), | ||
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 diffTagsCloudWatchEvents(oldTags, newTags []*events.Tag) ([]*events.Tag, []*events.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 []*events.Tag | ||
for _, t := range oldTags { | ||
old, ok := create[*t.Key] | ||
if !ok || old != *t.Value { | ||
// Delete it! | ||
remove = append(remove, t) | ||
} | ||
} | ||
|
||
return tagsFromMapCloudWatchEvents(create), remove | ||
} | ||
|
||
// tagsFromMap returns the tags for the given map of data. | ||
func tagsFromMapCloudWatchEvents(m map[string]interface{}) []*events.Tag { | ||
var result []*events.Tag | ||
for k, v := range m { | ||
t := &events.Tag{ | ||
Key: aws.String(k), | ||
Value: aws.String(v.(string)), | ||
} | ||
if !tagIgnoredCloudWatchEvents(t) { | ||
result = append(result, t) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// tagsToMap turns the list of tags into a map. | ||
func tagsToMapCloudWatchEvents(ts []*events.Tag) map[string]string { | ||
result := make(map[string]string) | ||
for _, t := range ts { | ||
if !tagIgnoredCloudWatchEvents(t) { | ||
result[*t.Key] = *t.Value | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func saveTagsCloudWatchEvents(conn *events.CloudWatchEvents, d *schema.ResourceData, arn string) error { | ||
resp, err := conn.ListTagsForResource(&events.ListTagsForResourceInput{ | ||
ResourceARN: aws.String(arn), | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error retreiving tags for ARN: %s", arn) | ||
} | ||
|
||
var tagList []*events.Tag | ||
if len(resp.Tags) > 0 { | ||
tagList = resp.Tags | ||
} | ||
|
||
return d.Set("tags", tagsToMapCloudWatchEvents(tagList)) | ||
} | ||
|
||
// compare a tag against a list of strings and checks if it should | ||
// be ignored or not | ||
func tagIgnoredCloudWatchEvents(t *events.Tag) bool { | ||
filter := []string{"^aws:"} | ||
for _, v := range filter { | ||
log.Printf("[DEBUG] Matching %v with %v\n", v, *t.Key) | ||
r, _ := regexp.MatchString(v, *t.Key) | ||
if r { | ||
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,77 @@ | ||
package aws | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
events "github.com/aws/aws-sdk-go/service/cloudwatchevents" | ||
) | ||
|
||
func TestDiffCloudWatchEventTags(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 := diffTagsCloudWatchEvents(tagsFromMapCloudWatchEvents(tc.Old), tagsFromMapCloudWatchEvents(tc.New)) | ||
cm := tagsToMapCloudWatchEvents(c) | ||
rm := tagsToMapCloudWatchEvents(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) | ||
} | ||
} | ||
} | ||
|
||
func TestIgnoringTagsCloudWatchEvents(t *testing.T) { | ||
var ignoredTags []*events.Tag | ||
ignoredTags = append(ignoredTags, &events.Tag{ | ||
Key: aws.String("aws:cloudformation:logical-id"), | ||
Value: aws.String("foo"), | ||
}) | ||
ignoredTags = append(ignoredTags, &events.Tag{ | ||
Key: aws.String("aws:foo:bar"), | ||
Value: aws.String("baz"), | ||
}) | ||
for _, tag := range ignoredTags { | ||
if !tagIgnoredCloudWatchEvents(tag) { | ||
t.Fatalf("Tag %v with value %v not ignored, but should be!", *tag.Key, *tag.Value) | ||
} | ||
} | ||
} |
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