Skip to content
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

Allow for key suffixes and key regexes in ignore tags #38032

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func New(ctx context.Context) (*schema.Provider, error) {
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Resource tag key prefixes to ignore across all resources.",
},
"key_suffixes": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Resource tag key prefixes to ignore across all resources.",
},
},
},
},
Expand Down Expand Up @@ -867,6 +873,10 @@ func expandIgnoreTags(ctx context.Context, tfMap map[string]interface{}) *tftags
ignoreConfig.KeyPrefixes = tftags.New(ctx, v.List())
}

if v, ok := tfMap["key_suffixes"].(*schema.Set); ok {
ignoreConfig.KeySuffixes = tftags.New(ctx, v.List())
}

return ignoreConfig
}

Expand Down
26 changes: 26 additions & 0 deletions internal/tags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type DefaultConfig struct {
type IgnoreConfig struct {
Keys KeyValueTags
KeyPrefixes KeyValueTags
KeySuffixes KeyValueTags
}

// KeyValueTags is a standard implementation for AWS key-value resource tags.
Expand Down Expand Up @@ -106,6 +107,7 @@ func (tags KeyValueTags) IgnoreConfig(config *IgnoreConfig) KeyValueTags {
}

result := tags.IgnorePrefixes(config.KeyPrefixes)
result := tags.IgnoreSuffixes(config.KeySuffixes)
result = result.Ignore(config.Keys)

return result
Expand Down Expand Up @@ -158,6 +160,30 @@ func (tags KeyValueTags) IgnorePrefixes(ignoreTagPrefixes KeyValueTags) KeyValue
return result
}

// IgnoreSuffixes returns non-matching tag key prefixes.
func (tags KeyValueTags) IgnoreSuffixes(ignoreTagSuffixes KeyValueTags) KeyValueTags {
result := make(KeyValueTags)

for k, v := range tags {
var ignore bool

for ignoreTagSuffix := range ignoreTagSuffixes {
if strings.HasSuffix(k, ignoreTagSuffix) {
ignore = true
break
}
}

if ignore {
continue
}

result[k] = v
}

return result
}

// IgnoreServerlessApplicationRepository returns non-AWS and non-ServerlessApplicationRepository tag keys.
func (tags KeyValueTags) IgnoreServerlessApplicationRepository() KeyValueTags {
result := make(KeyValueTags)
Expand Down
85 changes: 85 additions & 0 deletions internal/tags/key_value_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,18 @@ func TestKeyValueTagsIgnoreConfig(t *testing.T) {
},
want: map[string]string{},
},
{
name: "no tags with suffix",
tags: New(ctx, map[string]string{}),
ignoreConfig: &IgnoreConfig{
KeySuffixes: New(ctx, []string{
"key1",
"key2",
"key3",
}),
},
want: map[string]string{},
},
{
name: "keys all matching",
tags: New(ctx, map[string]string{
Expand Down Expand Up @@ -501,6 +513,26 @@ func TestKeyValueTagsIgnoreConfig(t *testing.T) {
"key3": "value3",
},
},
{
name: "keys and key prefixes and key suffixes",
tags: New(ctx, map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}),
ignoreConfig: &IgnoreConfig{
Keys: New(ctx, []string{
"key1",
}),
KeyPrefixes: New(ctx, []string{
"key2",
}),
KeySuffixes: New(ctx, []string{
"key3",
}),
},
want: map[string]string{},
},
{
name: "key prefixes all exact",
tags: New(ctx, map[string]string{
Expand All @@ -517,6 +549,22 @@ func TestKeyValueTagsIgnoreConfig(t *testing.T) {
},
want: map[string]string{},
},
{
name: "key suffixes all exact",
tags: New(ctx, map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}),
ignoreConfig: &IgnoreConfig{
KeySuffixes: New(ctx, []string{
"key1",
"key2",
"key3",
}),
},
want: map[string]string{},
},
{
name: "key prefixes all prefixed",
tags: New(ctx, map[string]string{
Expand All @@ -531,6 +579,23 @@ func TestKeyValueTagsIgnoreConfig(t *testing.T) {
},
want: map[string]string{},
},
{
name: "key suffixes just 1",
tags: New(ctx, map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}),
ignoreConfig: &IgnoreConfig{
KeySuffixes: New(ctx, []string{
"key1",
}),
},
want: map[string]string{
"key2": "value2",
"key3": "value3",
},
},
{
name: "key prefixes some prefixed",
tags: New(ctx, map[string]string{
Expand Down Expand Up @@ -568,6 +633,26 @@ func TestKeyValueTagsIgnoreConfig(t *testing.T) {
"key3": "value3",
},
},
{
name: "key suffixes none suffixed",
tags: New(ctx, map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}),
ignoreConfig: &IgnoreConfig{
KeySuffixes: New(ctx, []string{
"key4",
"key5",
"key6",
}),
},
want: map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
},
},
}

for _, testCase := range testCases {
Expand Down