Skip to content

Commit 079116f

Browse files
authored
Merge pull request #6 from terraform-linters/migrate_tags_rules
Migrate tags rule from TFLint core
2 parents b4fabaf + 55562ad commit 079116f

File tree

782 files changed

+1795
-776
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

782 files changed

+1795
-776
lines changed

aws/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (d *ProviderData) Get(key string) (string, bool, error) {
168168
}
169169

170170
var val string
171-
err := d.runner.EvaluateExprOnRootCtx(attribute.Expr, &val)
171+
err := d.runner.EvaluateExprOnRootCtx(attribute.Expr, &val, nil)
172172

173173
err = d.runner.EnsureNoError(err, func() error { return nil })
174174
if err != nil {

aws/runner.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package aws
22

33
import (
4-
"fmt"
54
"log"
65

76
"github.com/hashicorp/hcl/v2"
@@ -42,7 +41,7 @@ func NewRunner(runner tflint.Runner, config *Config) (*Runner, error) {
4241
// If not, the given expression is used as it is
4342
func (r *Runner) EachStringSliceExprs(expr hcl.Expression, proc func(val string, expr hcl.Expression)) error {
4443
var vals []string
45-
err := r.EvaluateExpr(expr, &vals)
44+
err := r.EvaluateExpr(expr, &vals, nil)
4645

4746
exprs, diags := hcl.ExprList(expr)
4847
if diags.HasErrors() {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# aws_resource_missing_tags
2+
3+
Require specific tags for all AWS resource types that support them.
4+
5+
## Configuration
6+
7+
```hcl
8+
rule "aws_resource_missing_tags" {
9+
enabled = true
10+
tags = ["Foo", "Bar"]
11+
exclude = ["aws_autoscaling_group"] # (Optional) Exclude some resource types from tag checks
12+
}
13+
```
14+
15+
## Examples
16+
17+
Most resources use the `tags` attribute with simple `key`=`value` pairs:
18+
19+
```hcl
20+
resource "aws_instance" "instance" {
21+
instance_type = "m5.large"
22+
tags = {
23+
foo = "Bar"
24+
bar = "Baz"
25+
}
26+
}
27+
```
28+
29+
```
30+
$ tflint
31+
1 issue(s) found:
32+
33+
Notice: aws_instance.instance is missing the following tags: "Bar", "Foo". (aws_resource_missing_tags)
34+
35+
on test.tf line 3:
36+
3: tags = {
37+
4: foo = "Bar"
38+
5: bar = "Baz"
39+
6: }
40+
```
41+
42+
Iterators in `dynamic` blocks cannot be expanded, so the tags in the following example will not be detected.
43+
44+
```hcl
45+
locals {
46+
tags = [
47+
{
48+
key = "Name",
49+
value = "SomeName",
50+
},
51+
{
52+
key = "env",
53+
value = "SomeEnv",
54+
},
55+
]
56+
}
57+
resource "aws_autoscaling_group" "this" {
58+
dynamic "tag" {
59+
for_each = local.tags
60+
61+
content {
62+
key = tag.key
63+
value = tag.value
64+
propagate_at_launch = true
65+
}
66+
}
67+
}
68+
```
69+
70+
## Why
71+
72+
You want to set a standardized set of tags for your AWS resources.
73+
74+
## How To Fix
75+
76+
For each resource type that supports tags, ensure that each missing tag is present.

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/onsi/ginkgo v1.14.2 // indirect
1414
github.com/onsi/gomega v1.10.3 // indirect
1515
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e
16-
github.com/terraform-linters/tflint-plugin-sdk v0.6.1-0.20201205142940-d49361e1c42c
16+
github.com/terraform-linters/tflint-plugin-sdk v0.6.1-0.20201214165213-827cf110e0a9
1717
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20201118192700-9cc6324740c9
18+
github.com/zclconf/go-cty v1.7.0
1819
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
326326
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
327327
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
328328
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
329-
github.com/terraform-linters/tflint-plugin-sdk v0.6.1-0.20201205142940-d49361e1c42c h1:c19JJeT1VbTXFpqaxi7FBAv4OJAeKVg0mpNs2ySbgi0=
330-
github.com/terraform-linters/tflint-plugin-sdk v0.6.1-0.20201205142940-d49361e1c42c/go.mod h1:EMiQwq0IiBwylbSgx53sdPBRhOHEXrjXhrD0x5C8SjY=
329+
github.com/terraform-linters/tflint-plugin-sdk v0.6.1-0.20201214165213-827cf110e0a9 h1:ADqZANXUwQzsBtG4nuaz8qGjXsetvHgk67Fr2OmRJs4=
330+
github.com/terraform-linters/tflint-plugin-sdk v0.6.1-0.20201214165213-827cf110e0a9/go.mod h1:EMiQwq0IiBwylbSgx53sdPBRhOHEXrjXhrD0x5C8SjY=
331331
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20201118192700-9cc6324740c9 h1:0u9SqTq2nbof0t+7xqfI8Ejhmooe3Qqe09fobOCZY6g=
332332
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20201118192700-9cc6324740c9/go.mod h1:sToOUnPCXFPwMljH57zM6uOI3q1YVREy4GSlg1Wm8/Y=
333333
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/terraform-linters/tflint-plugin-sdk/plugin"
55
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
66
"github.com/terraform-linters/tflint-ruleset-aws/aws"
7+
"github.com/terraform-linters/tflint-ruleset-aws/project"
78
"github.com/terraform-linters/tflint-ruleset-aws/rules"
89
"github.com/terraform-linters/tflint-ruleset-aws/rules/api"
910
)
@@ -13,7 +14,7 @@ func main() {
1314
RuleSet: &aws.RuleSet{
1415
BuiltinRuleSet: tflint.BuiltinRuleSet{
1516
Name: "aws",
16-
Version: "0.1.0",
17+
Version: project.Version,
1718
Rules: rules.Rules,
1819
},
1920
APIRules: api.Rules,

project/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package project
2+
3+
import "fmt"
4+
5+
// Version is ruleset version
6+
const Version string = "0.1.0"
7+
8+
// ReferenceLink returns the rule reference link
9+
func ReferenceLink(name string) string {
10+
return fmt.Sprintf("https://github.com/terraform-linters/tflint-ruleset-aws/blob/v%s/docs/rules/%s.md", Version, name)
11+
}

rules/api/aws_db_instance_invalid_db_subnet_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (r *AwsDBInstanceInvalidDBSubnetGroupRule) Check(rr tflint.Runner) error {
7272
}
7373

7474
var val string
75-
err := runner.EvaluateExpr(attribute.Expr, &val)
75+
err := runner.EvaluateExpr(attribute.Expr, &val, nil)
7676

7777
return runner.EnsureNoError(err, func() error {
7878
if !r.data[val] {

rules/api/aws_db_instance_invalid_option_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (r *AwsDBInstanceInvalidOptionGroupRule) Check(rr tflint.Runner) error {
7272
}
7373

7474
var val string
75-
err := runner.EvaluateExpr(attribute.Expr, &val)
75+
err := runner.EvaluateExpr(attribute.Expr, &val, nil)
7676

7777
return runner.EnsureNoError(err, func() error {
7878
if !r.data[val] {

rules/api/aws_db_instance_invalid_parameter_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (r *AwsDBInstanceInvalidParameterGroupRule) Check(rr tflint.Runner) error {
7272
}
7373

7474
var val string
75-
err := runner.EvaluateExpr(attribute.Expr, &val)
75+
err := runner.EvaluateExpr(attribute.Expr, &val, nil)
7676

7777
return runner.EnsureNoError(err, func() error {
7878
if !r.data[val] {

0 commit comments

Comments
 (0)