-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provider/aws: Allow tags for kinesis streams #3397
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,105 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/kinesis" | ||
"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 setTagsKinesis(conn *kinesis.Kinesis, d *schema.ResourceData) error { | ||
|
||
sn := d.Get("name").(string) | ||
|
||
if d.HasChange("tags") { | ||
oraw, nraw := d.GetChange("tags") | ||
o := oraw.(map[string]interface{}) | ||
n := nraw.(map[string]interface{}) | ||
create, remove := diffTagsKinesis(tagsFromMapKinesis(o), tagsFromMapKinesis(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.RemoveTagsFromStream(&kinesis.RemoveTagsFromStreamInput{ | ||
StreamName: aws.String(sn), | ||
TagKeys: k, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if len(create) > 0 { | ||
|
||
log.Printf("[DEBUG] Creating tags: %#v", create) | ||
t := make(map[string]*string) | ||
for _, tag := range create { | ||
t[*tag.Key] = tag.Value | ||
} | ||
|
||
_, err := conn.AddTagsToStream(&kinesis.AddTagsToStreamInput{ | ||
StreamName: aws.String(sn), | ||
Tags: t, | ||
}) | ||
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 diffTagsKinesis(oldTags, newTags []*kinesis.Tag) ([]*kinesis.Tag, []*kinesis.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 []*kinesis.Tag | ||
for _, t := range oldTags { | ||
old, ok := create[*t.Key] | ||
if !ok || old != *t.Value { | ||
// Delete it! | ||
remove = append(remove, t) | ||
} | ||
} | ||
|
||
return tagsFromMapKinesis(create), remove | ||
} | ||
|
||
// tagsFromMap returns the tags for the given map of data. | ||
func tagsFromMapKinesis(m map[string]interface{}) []*kinesis.Tag { | ||
var result []*kinesis.Tag | ||
for k, v := range m { | ||
result = append(result, &kinesis.Tag{ | ||
Key: aws.String(k), | ||
Value: aws.String(v.(string)), | ||
}) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// tagsToMap turns the list of tags into a map. | ||
func tagsToMapKinesis(ts []*kinesis.Tag) map[string]string { | ||
result := make(map[string]string) | ||
for _, t := range ts { | ||
result[*t.Key] = *t.Value | ||
} | ||
|
||
return result | ||
} |
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,84 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/kinesis" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestDiffTagsKinesis(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 := diffTagsKinesis(tagsFromMapKinesis(tc.Old), tagsFromMapKinesis(tc.New)) | ||
cm := tagsToMapKinesis(c) | ||
rm := tagsToMapKinesis(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) | ||
} | ||
} | ||
} | ||
|
||
// testAccCheckTags can be used to check the tags on a resource. | ||
func testAccCheckKinesisTags(ts []*kinesis.Tag, key string, value string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
m := tagsToMapKinesis(ts) | ||
v, ok := m[key] | ||
if value != "" && !ok { | ||
return fmt.Errorf("Missing tag: %s", key) | ||
} else if value == "" && ok { | ||
return fmt.Errorf("Extra tag: %s", key) | ||
} | ||
if value == "" { | ||
return nil | ||
} | ||
|
||
if v != value { | ||
return fmt.Errorf("%s: bad value: %s", key, v) | ||
} | ||
|
||
return nil | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I ran the acceptance tests, I got a crash here on setting the tags.
The
var
declaration above declares the storage for the map but it doesn't initialize it. I think you can fix this by changing the declaration tot := make(map[string]*string)
.