-
Notifications
You must be signed in to change notification settings - Fork 74
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
New rule: aws_provider_missing_tags #633
New rule: aws_provider_missing_tags #633
Conversation
rules/aws_provider_missing_tags.go
Outdated
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
"github.com/terraform-linters/tflint-ruleset-aws/project" | ||
"github.com/zclconf/go-cty/cty" | ||
"golang.org/x/exp/slices" |
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.
The slices
package is already available as a standard package in Go 1.21.
https://pkg.go.dev/slices@go1.21
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.
fixed
rules/aws_provider_missing_tags.go
Outdated
|
||
// Name returns the rule name | ||
func (r *AwsProviderMissingTagsRule) Name() string { | ||
return "aws_provider_missing_tags" |
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.
Maybe aws_provider_missing_default_tags
is a better name. What do you think?
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.
renamed
Expected: helper.Issues{ | ||
{ | ||
Rule: NewAwsProviderMissingTagsRule(), | ||
Message: "The provider `default` is missing the `default_tags` block", |
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.
You may be wondering what this "default" is if you haven't set up an alias.
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.
changed the output, though it's kind of awkward
rules/aws_provider_missing_tags.go
Outdated
sort.Strings(missing) | ||
wanted := strings.Join(missing, ", ") | ||
issue := fmt.Sprintf("The provider is missing the following tags: %s.", wanted) | ||
runner.EmitIssue(r, issue, location) |
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.
Please check the error return value.
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.
done
rules/aws_provider_missing_tags.go
Outdated
var providerTags []string | ||
attr, ok := block.Body.Attributes[providerTagsAttributeName] | ||
if !ok { | ||
r.emitIssue(runner, providerTags, config, provider.DefRange) |
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.
block.DefRange
would be better than provider.DefRange
.
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.
There is no block, this case is for when default_tags doesn't exist.
rules/aws_provider_missing_tags.go
Outdated
} | ||
|
||
// Check tags | ||
r.emitIssue(runner, providerTags, config, provider.DefRange) |
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.
attr.Range
would be better than provider.DefRange
.
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.
changed
|
||
## Why | ||
|
||
You want to set a standardized set of tags for your AWS resources via the provider default tags. |
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.
I think it would be more helpful to mention its use with the aws_resource_missing_tags
rule in specific scenarios.
At least, this is what I thought about this rule first:
- Why not
aws_resource_missing_tags
rule? - What is the difference from
aws_resource_missing_tags
rule? - When should I use this rule?
It's a good idea to include answers to these questions in the "Why" section. Perhaps the explanation in the PR will be helpful.
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.
fleshed out the doc
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.
Looks good. Thanks!
I love this rule, thanks @bootswithdefer 💖 |
@wata727 could create a new tag so we can use it? AFAIK we can't add a plugin with a commit ID as the version? |
v0.32.0 has been released. |
Adds an
aws_provider_missing_tags
rule, which requires specific tags for AWS provider default_tags blocks. Theaws_resource_missing_tags
rule is not sufficient, it enforces the set of tags that end up on the resource, but it doesn't enforce the practice of applying them via default_tags instead of resource tags. Using default tags results in more DRY terraform.I would see this being used in conjunction with
aws_resource_missing_tags
, whereaws_provider_missing_tags
is used for common tags andaws_resource_missing_tags
is used for more resource-specific tags (like Name).