Skip to content

Commit 2dc198d

Browse files
committed
Bump tflint-plugin-sdk to v0.10.0
1 parent f3361ed commit 2dc198d

7 files changed

+288
-116
lines changed

go.mod

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ go 1.17
55
require (
66
github.com/hashicorp/go-version v1.3.0
77
github.com/hashicorp/hcl/v2 v2.11.1
8-
github.com/terraform-linters/tflint-plugin-sdk v0.9.1
8+
github.com/terraform-linters/tflint-plugin-sdk v0.10.0
9+
github.com/zclconf/go-cty v1.10.0
910
)
1011

1112
require (
1213
github.com/agext/levenshtein v1.2.1 // indirect
1314
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
1415
github.com/fatih/color v1.7.0 // indirect
15-
github.com/golang/protobuf v1.3.4 // indirect
16-
github.com/google/go-cmp v0.5.6 // indirect
17-
github.com/hashicorp/go-hclog v0.16.2 // indirect
18-
github.com/hashicorp/go-plugin v1.4.2 // indirect
19-
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
16+
github.com/golang/protobuf v1.5.2 // indirect
17+
github.com/google/go-cmp v0.5.7 // indirect
18+
github.com/hashicorp/go-hclog v1.2.0 // indirect
19+
github.com/hashicorp/go-plugin v1.4.3 // indirect
2020
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
2121
github.com/mattn/go-colorable v0.1.4 // indirect
2222
github.com/mattn/go-isatty v0.0.10 // indirect
@@ -25,12 +25,12 @@ require (
2525
github.com/oklog/run v1.0.0 // indirect
2626
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
2727
github.com/vmihailenco/tagparser v0.1.1 // indirect
28-
github.com/zclconf/go-cty v1.9.0 // indirect
29-
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
30-
golang.org/x/sys v0.0.0-20191008105621-543471e840be // indirect
28+
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
29+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
3130
golang.org/x/text v0.3.5 // indirect
32-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
31+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
3332
google.golang.org/appengine v1.6.5 // indirect
34-
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect
35-
google.golang.org/grpc v1.27.1 // indirect
33+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
34+
google.golang.org/grpc v1.45.0 // indirect
35+
google.golang.org/protobuf v1.27.1 // indirect
3636
)

go.sum

+81-26
Large diffs are not rendered by default.

rules/aws_instance_example_type.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package rules
33
import (
44
"fmt"
55

6-
hcl "github.com/hashicorp/hcl/v2"
6+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
77
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
88
)
99

1010
// AwsInstanceExampleTypeRule checks whether ...
11-
type AwsInstanceExampleTypeRule struct{}
11+
type AwsInstanceExampleTypeRule struct {
12+
tflint.DefaultRule
13+
}
1214

1315
// NewAwsInstanceExampleTypeRule returns a new rule
1416
func NewAwsInstanceExampleTypeRule() *AwsInstanceExampleTypeRule {
@@ -26,7 +28,7 @@ func (r *AwsInstanceExampleTypeRule) Enabled() bool {
2628
}
2729

2830
// Severity returns the rule severity
29-
func (r *AwsInstanceExampleTypeRule) Severity() string {
31+
func (r *AwsInstanceExampleTypeRule) Severity() tflint.Severity {
3032
return tflint.ERROR
3133
}
3234

@@ -37,16 +39,35 @@ func (r *AwsInstanceExampleTypeRule) Link() string {
3739

3840
// Check checks whether ...
3941
func (r *AwsInstanceExampleTypeRule) Check(runner tflint.Runner) error {
40-
return runner.WalkResourceAttributes("aws_instance", "instance_type", func(attribute *hcl.Attribute) error {
42+
resources, err := runner.GetResourceContent("aws_instance", &hclext.BodySchema{
43+
Attributes: []hclext.AttributeSchema{
44+
{Name: "instance_type"},
45+
},
46+
}, nil)
47+
if err != nil {
48+
return err
49+
}
50+
51+
for _, resource := range resources.Blocks {
52+
attribute, exists := resource.Body.Attributes["instance_type"]
53+
if !exists {
54+
continue
55+
}
56+
4157
var instanceType string
4258
err := runner.EvaluateExpr(attribute.Expr, &instanceType, nil)
4359

44-
return runner.EnsureNoError(err, func() error {
45-
return runner.EmitIssueOnExpr(
60+
err = runner.EnsureNoError(err, func() error {
61+
return runner.EmitIssue(
4662
r,
4763
fmt.Sprintf("instance type is %s", instanceType),
48-
attribute.Expr,
64+
attribute.Expr.Range(),
4965
)
5066
})
51-
})
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
72+
return nil
5273
}
+37-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package rules
22

33
import (
4-
hcl "github.com/hashicorp/hcl/v2"
4+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
55
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
66
)
77

88
// AwsS3BucketExampleLifecycleRuleRule checks whether ...
9-
type AwsS3BucketExampleLifecycleRuleRule struct{}
9+
type AwsS3BucketExampleLifecycleRuleRule struct {
10+
tflint.DefaultRule
11+
}
1012

1113
// NewAwsS3BucketExampleLifecycleRuleRule returns a new rule
1214
func NewAwsS3BucketExampleLifecycleRuleRule() *AwsS3BucketExampleLifecycleRuleRule {
@@ -24,7 +26,7 @@ func (r *AwsS3BucketExampleLifecycleRuleRule) Enabled() bool {
2426
}
2527

2628
// Severity returns the rule severity
27-
func (r *AwsS3BucketExampleLifecycleRuleRule) Severity() string {
29+
func (r *AwsS3BucketExampleLifecycleRuleRule) Severity() tflint.Severity {
2830
return tflint.ERROR
2931
}
3032

@@ -35,35 +37,44 @@ func (r *AwsS3BucketExampleLifecycleRuleRule) Link() string {
3537

3638
// Check checks whether ...
3739
func (r *AwsS3BucketExampleLifecycleRuleRule) Check(runner tflint.Runner) error {
38-
return runner.WalkResourceBlocks("aws_s3_bucket", "lifecycle_rule", func(block *hcl.Block) error {
39-
if err := runner.EmitIssue(r, "`lifecycle_rule` block found", block.DefRange); err != nil {
40-
return err
41-
}
42-
43-
content, _, diags := block.Body.PartialContent(&hcl.BodySchema{
44-
Attributes: []hcl.AttributeSchema{
45-
{Name: "enabled"},
40+
resources, err := runner.GetResourceContent("aws_s3_bucket", &hclext.BodySchema{
41+
Blocks: []hclext.BlockSchema{
42+
{
43+
Type: "lifecycle_rule",
44+
Body: &hclext.BodySchema{
45+
Attributes: []hclext.AttributeSchema{
46+
{Name: "enabled"},
47+
},
48+
Blocks: []hclext.BlockSchema{
49+
{Type: "transition"},
50+
},
51+
},
4652
},
47-
Blocks: []hcl.BlockHeaderSchema{
48-
{Type: "transition"},
49-
},
50-
})
51-
if diags.HasErrors() {
52-
return diags
53-
}
53+
},
54+
}, nil)
55+
if err != nil {
56+
return err
57+
}
5458

55-
if attr, exists := content.Attributes["enabled"]; exists {
56-
if err := runner.EmitIssueOnExpr(r, "`enabled` attribute found", attr.Expr); err != nil {
59+
for _, resource := range resources.Blocks {
60+
for _, rule := range resource.Body.Blocks {
61+
if err := runner.EmitIssue(r, "`lifecycle_rule` block found", rule.DefRange); err != nil {
5762
return err
5863
}
59-
}
6064

61-
for _, block := range content.Blocks {
62-
if err := runner.EmitIssue(r, "`transition` block found", block.DefRange); err != nil {
63-
return err
65+
if attr, exists := rule.Body.Attributes["enabled"]; exists {
66+
if err := runner.EmitIssue(r, "`enabled` attribute found", attr.Expr.Range()); err != nil {
67+
return err
68+
}
69+
}
70+
71+
for _, transitions := range rule.Body.Blocks {
72+
if err := runner.EmitIssue(r, "`transition` block found", transitions.DefRange); err != nil {
73+
return err
74+
}
6475
}
6576
}
77+
}
6678

67-
return nil
68-
})
79+
return nil
6980
}
+28-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package rules
22

33
import (
4-
hcl "github.com/hashicorp/hcl/v2"
5-
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
4+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
65
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
76
)
87

98
// LocalFileExampleProvisionerRule checks whether ...
10-
type LocalFileExampleProvisionerRule struct{}
9+
type LocalFileExampleProvisionerRule struct {
10+
tflint.DefaultRule
11+
}
1112

1213
// NewLocalFileExampleProvisionerRule returns a new rule
1314
func NewLocalFileExampleProvisionerRule() *LocalFileExampleProvisionerRule {
@@ -25,7 +26,7 @@ func (r *LocalFileExampleProvisionerRule) Enabled() bool {
2526
}
2627

2728
// Severity returns the rule severity
28-
func (r *LocalFileExampleProvisionerRule) Severity() string {
29+
func (r *LocalFileExampleProvisionerRule) Severity() tflint.Severity {
2930
return tflint.ERROR
3031
}
3132

@@ -36,29 +37,36 @@ func (r *LocalFileExampleProvisionerRule) Link() string {
3637

3738
// Check checks whether ...
3839
func (r *LocalFileExampleProvisionerRule) Check(runner tflint.Runner) error {
39-
return runner.WalkResources("local_file", func(resource *configs.Resource) error {
40+
resources, err := runner.GetResourceContent("local_file", &hclext.BodySchema{
41+
Blocks: []hclext.BlockSchema{
42+
{
43+
Type: "provisioner",
44+
LabelNames: []string{"name"},
45+
Body: &hclext.BodySchema{
46+
Attributes: []hclext.AttributeSchema{
47+
{Name: "command"},
48+
},
49+
},
50+
},
51+
},
52+
}, nil)
53+
if err != nil {
54+
return err
55+
}
4056

41-
for _, provisioner := range resource.Managed.Provisioners {
42-
if provisioner.Type != "local-exec" {
57+
for _, resource := range resources.Blocks {
58+
for _, provisioner := range resource.Body.Blocks {
59+
if provisioner.Labels[0] != "local-exec" {
4360
continue
4461
}
4562

46-
content, _, diags := provisioner.Config.PartialContent(&hcl.BodySchema{
47-
Attributes: []hcl.AttributeSchema{
48-
{Name: "command"},
49-
},
50-
})
51-
if diags.HasErrors() {
52-
return diags
53-
}
54-
55-
if attr, exists := content.Attributes["command"]; exists {
56-
if err := runner.EmitIssueOnExpr(r, "local-exec provisioner command found", attr.Expr); err != nil {
63+
if attr, exists := provisioner.Body.Attributes["command"]; exists {
64+
if err := runner.EmitIssue(r, "local-exec provisioner command found", attr.Expr.Range()); err != nil {
5765
return err
5866
}
5967
}
6068
}
69+
}
6170

62-
return nil
63-
})
71+
return nil
6472
}

rules/module_call_validity.go

+68-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package rules
22

33
import (
44
"github.com/hashicorp/go-version"
5-
hcl "github.com/hashicorp/hcl/v2"
6-
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
5+
"github.com/hashicorp/hcl/v2"
6+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
77
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
8+
"github.com/zclconf/go-cty/cty/gocty"
89
)
910

1011
// ModuleCallValidityRule checks whether ...
11-
type ModuleCallValidityRule struct{}
12+
type ModuleCallValidityRule struct {
13+
tflint.DefaultRule
14+
}
1215

1316
// NewModuleCallValidityRule returns a new rule
1417
func NewModuleCallValidityRule() *ModuleCallValidityRule {
@@ -26,7 +29,7 @@ func (r *ModuleCallValidityRule) Enabled() bool {
2629
}
2730

2831
// Severity returns the rule severity
29-
func (r *ModuleCallValidityRule) Severity() string {
32+
func (r *ModuleCallValidityRule) Severity() tflint.Severity {
3033
return tflint.ERROR
3134
}
3235

@@ -37,20 +40,71 @@ func (r *ModuleCallValidityRule) Link() string {
3740

3841
// Check checks whether ...
3942
func (r *ModuleCallValidityRule) Check(runner tflint.Runner) error {
40-
return runner.WalkModuleCalls(func(call *configs.ModuleCall) error {
41-
if call.SourceAddr != "acceptable/source" {
42-
return runner.EmitIssue(r, "unacceptable module source", call.SourceAddrRange)
43+
content, err := runner.GetModuleContent(&hclext.BodySchema{
44+
Blocks: []hclext.BlockSchema{
45+
{
46+
Type: "module",
47+
LabelNames: []string{"name"},
48+
Body: &hclext.BodySchema{
49+
Attributes: []hclext.AttributeSchema{
50+
{Name: "source"},
51+
{Name: "providers"},
52+
{Name: "version"},
53+
},
54+
},
55+
},
56+
},
57+
}, nil)
58+
if err != nil {
59+
return err
60+
}
61+
62+
for _, module := range content.Blocks {
63+
if sourceAttr, exists := module.Body.Attributes["source"]; exists {
64+
var source string
65+
val, diags := sourceAttr.Expr.Value(nil)
66+
if diags.HasErrors() {
67+
return diags
68+
}
69+
if err := gocty.FromCtyValue(val, &source); err != nil {
70+
return err
71+
}
72+
73+
if source != "acceptable/source" {
74+
if err := runner.EmitIssue(r, "unacceptable module source", sourceAttr.Expr.Range()); err != nil {
75+
return err
76+
}
77+
}
4378
}
4479

45-
if len(call.Providers) != 0 {
46-
return runner.EmitIssue(r, "must not pass providers", hcl.Range{})
80+
if _, exists := module.Body.Attributes["providers"]; exists {
81+
if err := runner.EmitIssue(r, "must not pass providers", hcl.Range{}); err != nil {
82+
return err
83+
}
4784
}
4885

49-
expectedVersion, _ := version.NewVersion("0.1.0")
50-
if !call.Version.Required.Check(expectedVersion) {
51-
return runner.EmitIssue(r, "must accept version 0.1.0", call.Version.DeclRange)
86+
if versionAttr, exists := module.Body.Attributes["version"]; exists {
87+
var versionConstraint string
88+
val, diags := versionAttr.Expr.Value(nil)
89+
if diags.HasErrors() {
90+
return diags
91+
}
92+
if err := gocty.FromCtyValue(val, &versionConstraint); err != nil {
93+
return err
94+
}
95+
96+
expectedVersion, _ := version.NewVersion("0.1.0")
97+
constraint, err := version.NewConstraint(versionConstraint)
98+
if err != nil {
99+
return err
100+
}
101+
if !constraint.Check(expectedVersion) {
102+
if err := runner.EmitIssue(r, "must accept version 0.1.0", versionAttr.Expr.Range()); err != nil {
103+
return err
104+
}
105+
}
52106
}
107+
}
53108

54-
return nil
55-
})
109+
return nil
56110
}

0 commit comments

Comments
 (0)