-
Notifications
You must be signed in to change notification settings - Fork 25
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
Adding rule for azurerm resource missing tags #194
Adding rule for azurerm resource missing tags #194
Conversation
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.
Nice! Added integration test would be very helpful :)
Left some comments, but mostly looks good. If you can check and fix it, I would like to merge this PR.
sort.Strings(missing) | ||
wanted := strings.Join(missing, ", ") | ||
issue := fmt.Sprintf("The resource 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.
runner.EmitIssue
returns an error, so handle the error in this function and handle it in the caller.
https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk/tflint#Runner.EmitIssue
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.
error handled both in this function and in the caller using runner.EnsureNoError
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 EnsureNoError
is a helper function for handling warnings returned by EvaluateExpr
. So, there is no need to use it here. Return an error and check it in the caller as follows:
diff --git a/rules/azurerm_resource_missing_tags.go b/rules/azurerm_resource_missing_tags.go
index c6fc048..07bcbc7 100644
--- a/rules/azurerm_resource_missing_tags.go
+++ b/rules/azurerm_resource_missing_tags.go
@@ -87,15 +87,16 @@ func (r *AzurermResourceMissingTagsRule) Check(runner tflint.Runner) error {
wantType := cty.Map(cty.String)
err := runner.EvaluateExpr(attribute.Expr, &resourceTags, &tflint.EvaluateExprOption{WantType: &wantType})
err = runner.EnsureNoError(err, func() error {
- r.emitIssue(runner, resourceTags, config, attribute.Expr.Range())
- return nil
+ return r.emitIssue(runner, resourceTags, config, attribute.Expr.Range())
})
if err != nil {
return err
}
} else {
logger.Debug("checking", "resource type", resource.Labels[0], "resource name", resource.Labels[1])
- r.emitIssue(runner, map[string]string{}, config, resource.DefRange)
+ if err := r.emitIssue(runner, map[string]string{}, config, resource.DefRange); err != nil {
+ return err
+ }
}
}
}
@@ -103,7 +104,7 @@ func (r *AzurermResourceMissingTagsRule) Check(runner tflint.Runner) error {
return nil
}
-func (r *AzurermResourceMissingTagsRule) emitIssue(runner tflint.Runner, tags map[string]string, config azurermResourceTagsRuleConfig, location hcl.Range) {
+func (r *AzurermResourceMissingTagsRule) emitIssue(runner tflint.Runner, tags map[string]string, config azurermResourceTagsRuleConfig, location hcl.Range) error {
var missing []string
for _, tag := range config.Tags {
if _, ok := tags[tag]; !ok {
@@ -114,8 +115,9 @@ func (r *AzurermResourceMissingTagsRule) emitIssue(runner tflint.Runner, tags ma
sort.Strings(missing)
wanted := strings.Join(missing, ", ")
issue := fmt.Sprintf("The resource is missing the following tags: %s.", wanted)
- runner.EmitIssue(r, issue, location)
+ return runner.EmitIssue(r, issue, location)
}
+ return nil
}
func stringInSlice(a string, list []string) bool {
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.
got it, thanks!
fixed.
@@ -0,0 +1 @@ | |||
package 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.
It would be nice if go generate
could easily update the list of resources by adding a line like this:
//go:generate go run -tags generators ./generator/main.go
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.
go generate
comment was added,
I had to put it after the package declaration, otherwise golint
asks for special comment format Package tags...
by the way I've noticed that golint is deprecated and should be replaced (golang/go#38968)
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.
Yeah, we'll need to replace it with golangci-lint or simply turn it off. For rulesets, godoc is not so important, so we may not need to enforce this rule.
Thanks for the feedback @WATA72 😊 |
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.
Good, please check the error handling lastly.
did I fix it correctly? |
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.
Perfect. Thanks!
Can you please create a new release so we could use this rule? |
Sure. I'm planning to release a new version within a few days. |
Resolves #133 based on the work done in terraform-linters/tflint#617 for AWS.
this PR allows you to get notified for resources with missing required tags: