Skip to content

Commit

Permalink
refactor resource tag for appmesh
Browse files Browse the repository at this point in the history
  • Loading branch information
teraken0509 committed Jun 27, 2019
1 parent 0aa114d commit 0aa99f7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
19 changes: 11 additions & 8 deletions aws/tagsAppmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ func diffTagsAppmesh(oldTags, newTags []*appmesh.TagRef) ([]*appmesh.TagRef, []*
// First, we're creating everything we have
create := make(map[string]interface{})
for _, t := range newTags {
create[*t.Key] = *t.Value
create[aws.StringValue(t.Key)] = aws.StringValue(t.Value)
}

// Build the list of what to remove
var remove []*string
for _, t := range oldTags {
if _, ok := create[*t.Key]; !ok {
// Delete it!
old, ok := create[aws.StringValue(t.Key)]
if !ok || old != aws.StringValue(t.Value) {
remove = append(remove, t.Key)
} else if ok {
// already present so remove from new
delete(create, aws.StringValue(t.Key))
}
}

Expand Down Expand Up @@ -89,7 +92,7 @@ func tagsToMapAppmesh(ts []*appmesh.TagRef) map[string]string {
result := make(map[string]string)
for _, t := range ts {
if !tagIgnoredAppmesh(t) {
result[*t.Key] = *t.Value
result[aws.StringValue(t.Key)] = aws.StringValue(t.Value)
}
}

Expand All @@ -115,12 +118,12 @@ func saveTagsAppmesh(conn *appmesh.AppMesh, d *schema.ResourceData, arn string)
// compare a tag against a list of strings and checks if it should
// be ignored or not
func tagIgnoredAppmesh(t *appmesh.TagRef) bool {
filter := []string{"^aws:", "^appmesh:", "Name"}
filter := []string{"^aws:"}
for _, v := range filter {
log.Printf("[DEBUG] Matching %v with %v\n", v, *t.Key)
r, _ := regexp.MatchString(v, *t.Key)
log.Printf("[DEBUG] Matching %v with %v\n", v, aws.StringValue(t.Key))
r, _ := regexp.MatchString(v, aws.StringValue(t.Key))
if r {
log.Printf("[DEBUG] Found AWS specific tag %s (val: %s), ignoring.\n", *t.Key, *t.Value)
log.Printf("[DEBUG] Found AWS specific tag %s (val: %s), ignoring.\n", aws.StringValue(t.Key), aws.StringValue(t.Value))
return true
}
}
Expand Down
44 changes: 39 additions & 5 deletions aws/tagsAppmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ func TestDiffAppmeshTags(t *testing.T) {
Create map[string]string
Remove []string
}{
// Basic add/remove
// Add
{
Old: map[string]interface{}{
"foo": "bar",
},
New: map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
Create: map[string]string{
"bar": "baz",
},
Remove: []string{
"foo",
},
Remove: map[string]string{},
},

// Modify
Expand All @@ -41,7 +40,42 @@ func TestDiffAppmeshTags(t *testing.T) {
Create: map[string]string{
"foo": "baz",
},
Remove: []string{},
Remove: map[string]string{
"foo": "bar",
},
},

// Overlap
{
Old: map[string]interface{}{
"foo": "bar",
"hello": "world",
},
New: map[string]interface{}{
"foo": "baz",
"hello": "world",
},
Create: map[string]string{
"foo": "baz",
},
Remove: map[string]string{
"foo": "bar",
},
},

// Remove
{
Old: map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
New: map[string]interface{}{
"foo": "bar",
},
Create: map[string]string{},
Remove: map[string]string{
"bar": "baz",
},
},
}

Expand Down

0 comments on commit 0aa99f7

Please sign in to comment.