-
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
AWS - Tag support for Elasticsearch #4973
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,94 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice" | ||
"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 setTagsElasticsearchService(conn *elasticsearch.ElasticsearchService, 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 := diffTagsElasticsearchService(tagsFromMapElasticsearchService(o), tagsFromMapElasticsearchService(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.RemoveTags(&elasticsearch.RemoveTagsInput{ | ||
ARN: aws.String(arn), | ||
TagKeys: k, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if len(create) > 0 { | ||
log.Printf("[DEBUG] Creating tags: %#v", create) | ||
_, err := conn.AddTags(&elasticsearch.AddTagsInput{ | ||
ARN: aws.String(arn), | ||
TagList: 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 diffTagsElasticsearchService(oldTags, newTags []*elasticsearch.Tag) ([]*elasticsearch.Tag, []*elasticsearch.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 []*elasticsearch.Tag | ||
for _, t := range oldTags { | ||
old, ok := create[*t.Key] | ||
if !ok || old != *t.Value { | ||
// Delete it! | ||
remove = append(remove, t) | ||
} | ||
} | ||
|
||
return tagsFromMapElasticsearchService(create), remove | ||
} | ||
|
||
// tagsFromMap returns the tags for the given map of data. | ||
func tagsFromMapElasticsearchService(m map[string]interface{}) []*elasticsearch.Tag { | ||
var result []*elasticsearch.Tag | ||
for k, v := range m { | ||
result = append(result, &elasticsearch.Tag{ | ||
Key: aws.String(k), | ||
Value: aws.String(v.(string)), | ||
}) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// tagsToMap turns the list of tags into a map. | ||
func tagsToMapElasticsearchService(ts []*elasticsearch.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,85 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestDiffElasticsearchServiceTags(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 := diffTagsElasticsearchService(tagsFromMapElasticsearchService(tc.Old), tagsFromMapElasticsearchService(tc.New)) | ||
cm := tagsToMapElasticsearchService(c) | ||
rm := tagsToMapElasticsearchService(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 testAccCheckElasticsearchServiceTags( | ||
ts *[]*elasticsearch.Tag, key string, value string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
m := tagsToMapElasticsearchService(*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 | ||
} | ||
} |
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.
@paultyng why has this ValidateFunc changed?
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.
Those are the actual rules for es domain names, I had it failing in the API call locally instead of in validation if I didn't meet those. Let me dig up the docs, one sec.
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.
http://docs.aws.amazon.com/sdk-for-go/api/service/elasticsearchservice.html#type-CreateElasticsearchDomainInput
The text from the actual console is:
And when I try to start with a number it doesn't work.
Feel free to cherry pick around this, or I can rip it out, I just hit this bug as well.