Skip to content

Commit

Permalink
internal/keyvaluetags: Implement IgnoreRds for Neptune and RDS
Browse files Browse the repository at this point in the history
Reference: #10018 (review)
  • Loading branch information
bflad committed Sep 8, 2019
1 parent a7894ff commit e5758eb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
30 changes: 26 additions & 4 deletions aws/internal/keyvaluetags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"strings"
)

const AwsTagKeyPrefix = `aws:`
const ElasticbeanstalkTagKeyPrefix = `elasticbeanstalk:`
const NameTagKey = `Name`
const (
AwsTagKeyPrefix = `aws:`
ElasticbeanstalkTagKeyPrefix = `elasticbeanstalk:`
NameTagKey = `Name`
RdsTagKeyPrefix = `rds:`
)

// KeyValueTags is a standard implementation for AWS key-value resource tags.
// The AWS Go SDK is split into multiple service packages, each service with
Expand All @@ -31,7 +34,7 @@ func (tags KeyValueTags) IgnoreAws() KeyValueTags {
return result
}

// IgnoreAws returns non-AWS and non-Elasticbeanstalk tag keys.
// IgnoreElasticbeanstalk returns non-AWS and non-Elasticbeanstalk tag keys.
func (tags KeyValueTags) IgnoreElasticbeanstalk() KeyValueTags {
result := make(KeyValueTags)

Expand All @@ -54,6 +57,25 @@ func (tags KeyValueTags) IgnoreElasticbeanstalk() KeyValueTags {
return result
}

// IgnoreRDS returns non-AWS and non-RDS tag keys.
func (tags KeyValueTags) IgnoreRds() KeyValueTags {
result := make(KeyValueTags)

for k, v := range tags {
if strings.HasPrefix(k, AwsTagKeyPrefix) {
continue
}

if strings.HasPrefix(k, RdsTagKeyPrefix) {
continue
}

result[k] = v
}

return result
}

// Ignore returns non-matching tag keys.
func (tags KeyValueTags) Ignore(ignoreTags KeyValueTags) KeyValueTags {
result := make(KeyValueTags)
Expand Down
56 changes: 56 additions & 0 deletions aws/internal/keyvaluetags/key_value_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,62 @@ func TestKeyValueTagsIgnoreElasticbeanstalk(t *testing.T) {
}
}

func TestKeyValueTagsIgnoreRds(t *testing.T) {
testCases := []struct {
name string
tags KeyValueTags
want map[string]string
}{
{
name: "empty",
tags: New(map[string]string{}),
want: map[string]string{},
},
{
name: "all",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"rds:key2": "value2",
}),
want: map[string]string{},
},
{
name: "mixed",
tags: New(map[string]string{
"aws:cloudformation:key1": "value1",
"key2": "value2",
"rds:key3": "value3",
"key4": "value4",
}),
want: map[string]string{
"key2": "value2",
"key4": "value4",
},
},
{
name: "none",
tags: New(map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}),
want: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got := testCase.tags.IgnoreRds()

testKeyValueTagsVerifyMap(t, got.Map(), testCase.want)
})
}
}

func TestKeyValueTagsIgnore(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit e5758eb

Please sign in to comment.