Skip to content

Commit

Permalink
Bump tflint-plugin-sdk to v0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Mar 27, 2022
1 parent f3361ed commit 96b72ba
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 116 deletions.
24 changes: 12 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ go 1.17
require (
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl/v2 v2.11.1
github.com/terraform-linters/tflint-plugin-sdk v0.9.1
github.com/terraform-linters/tflint-plugin-sdk v0.9.2-0.20220327063603-919fa2776c6c
github.com/zclconf/go-cty v1.10.0
)

require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/golang/protobuf v1.3.4 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/hashicorp/go-hclog v0.16.2 // indirect
github.com/hashicorp/go-plugin v1.4.2 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-plugin v1.4.3 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
Expand All @@ -25,12 +25,12 @@ require (
github.com/oklog/run v1.0.0 // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.9.0 // indirect
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
golang.org/x/sys v0.0.0-20191008105621-543471e840be // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect
google.golang.org/grpc v1.27.1 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
107 changes: 81 additions & 26 deletions go.sum

Large diffs are not rendered by default.

37 changes: 29 additions & 8 deletions rules/aws_instance_example_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package rules
import (
"fmt"

hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)

// AwsInstanceExampleTypeRule checks whether ...
type AwsInstanceExampleTypeRule struct{}
type AwsInstanceExampleTypeRule struct {
tflint.DefaultRule
}

// NewAwsInstanceExampleTypeRule returns a new rule
func NewAwsInstanceExampleTypeRule() *AwsInstanceExampleTypeRule {
Expand All @@ -26,7 +28,7 @@ func (r *AwsInstanceExampleTypeRule) Enabled() bool {
}

// Severity returns the rule severity
func (r *AwsInstanceExampleTypeRule) Severity() string {
func (r *AwsInstanceExampleTypeRule) Severity() tflint.Severity {
return tflint.ERROR
}

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

// Check checks whether ...
func (r *AwsInstanceExampleTypeRule) Check(runner tflint.Runner) error {
return runner.WalkResourceAttributes("aws_instance", "instance_type", func(attribute *hcl.Attribute) error {
resources, err := runner.GetResourceContent("aws_instance", &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{
{Name: "instance_type"},
},
}, nil)
if err != nil {
return err
}

for _, resource := range resources.Blocks {
attribute, exists := resource.Body.Attributes["instance_type"]
if !exists {
continue
}

var instanceType string
err := runner.EvaluateExpr(attribute.Expr, &instanceType, nil)

return runner.EnsureNoError(err, func() error {
return runner.EmitIssueOnExpr(
err = runner.EnsureNoError(err, func() error {
return runner.EmitIssue(
r,
fmt.Sprintf("instance type is %s", instanceType),
attribute.Expr,
attribute.Expr.Range(),
)
})
})
if err != nil {
return err
}
}

return nil
}
63 changes: 37 additions & 26 deletions rules/aws_s3_bucket_example_lifecycle_rule.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package rules

import (
hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)

// AwsS3BucketExampleLifecycleRuleRule checks whether ...
type AwsS3BucketExampleLifecycleRuleRule struct{}
type AwsS3BucketExampleLifecycleRuleRule struct {
tflint.DefaultRule
}

// NewAwsS3BucketExampleLifecycleRuleRule returns a new rule
func NewAwsS3BucketExampleLifecycleRuleRule() *AwsS3BucketExampleLifecycleRuleRule {
Expand All @@ -24,7 +26,7 @@ func (r *AwsS3BucketExampleLifecycleRuleRule) Enabled() bool {
}

// Severity returns the rule severity
func (r *AwsS3BucketExampleLifecycleRuleRule) Severity() string {
func (r *AwsS3BucketExampleLifecycleRuleRule) Severity() tflint.Severity {
return tflint.ERROR
}

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

// Check checks whether ...
func (r *AwsS3BucketExampleLifecycleRuleRule) Check(runner tflint.Runner) error {
return runner.WalkResourceBlocks("aws_s3_bucket", "lifecycle_rule", func(block *hcl.Block) error {
if err := runner.EmitIssue(r, "`lifecycle_rule` block found", block.DefRange); err != nil {
return err
}

content, _, diags := block.Body.PartialContent(&hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{Name: "enabled"},
resources, err := runner.GetResourceContent("aws_s3_bucket", &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "lifecycle_rule",
Body: &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{
{Name: "enabled"},
},
Blocks: []hclext.BlockSchema{
{Type: "transition"},
},
},
},
Blocks: []hcl.BlockHeaderSchema{
{Type: "transition"},
},
})
if diags.HasErrors() {
return diags
}
},
}, nil)
if err != nil {
return err
}

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

for _, block := range content.Blocks {
if err := runner.EmitIssue(r, "`transition` block found", block.DefRange); err != nil {
return err
if attr, exists := rule.Body.Attributes["enabled"]; exists {
if err := runner.EmitIssue(r, "`enabled` attribute found", attr.Expr.Range()); err != nil {
return err
}
}

for _, transitions := range rule.Body.Blocks {
if err := runner.EmitIssue(r, "`transition` block found", transitions.DefRange); err != nil {
return err
}
}
}
}

return nil
})
return nil
}
48 changes: 28 additions & 20 deletions rules/local_file_example_provisioner.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package rules

import (
hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)

// LocalFileExampleProvisionerRule checks whether ...
type LocalFileExampleProvisionerRule struct{}
type LocalFileExampleProvisionerRule struct {
tflint.DefaultRule
}

// NewLocalFileExampleProvisionerRule returns a new rule
func NewLocalFileExampleProvisionerRule() *LocalFileExampleProvisionerRule {
Expand All @@ -25,7 +26,7 @@ func (r *LocalFileExampleProvisionerRule) Enabled() bool {
}

// Severity returns the rule severity
func (r *LocalFileExampleProvisionerRule) Severity() string {
func (r *LocalFileExampleProvisionerRule) Severity() tflint.Severity {
return tflint.ERROR
}

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

// Check checks whether ...
func (r *LocalFileExampleProvisionerRule) Check(runner tflint.Runner) error {
return runner.WalkResources("local_file", func(resource *configs.Resource) error {
resources, err := runner.GetResourceContent("local_file", &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "provisioner",
LabelNames: []string{"name"},
Body: &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{
{Name: "command"},
},
},
},
},
}, nil)
if err != nil {
return err
}

for _, provisioner := range resource.Managed.Provisioners {
if provisioner.Type != "local-exec" {
for _, resource := range resources.Blocks {
for _, provisioner := range resource.Body.Blocks {
if provisioner.Labels[0] != "local-exec" {
continue
}

content, _, diags := provisioner.Config.PartialContent(&hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{Name: "command"},
},
})
if diags.HasErrors() {
return diags
}

if attr, exists := content.Attributes["command"]; exists {
if err := runner.EmitIssueOnExpr(r, "local-exec provisioner command found", attr.Expr); err != nil {
if attr, exists := provisioner.Body.Attributes["command"]; exists {
if err := runner.EmitIssue(r, "local-exec provisioner command found", attr.Expr.Range()); err != nil {
return err
}
}
}
}

return nil
})
return nil
}
82 changes: 68 additions & 14 deletions rules/module_call_validity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package rules

import (
"github.com/hashicorp/go-version"
hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty/gocty"
)

// ModuleCallValidityRule checks whether ...
type ModuleCallValidityRule struct{}
type ModuleCallValidityRule struct {
tflint.DefaultRule
}

// NewModuleCallValidityRule returns a new rule
func NewModuleCallValidityRule() *ModuleCallValidityRule {
Expand All @@ -26,7 +29,7 @@ func (r *ModuleCallValidityRule) Enabled() bool {
}

// Severity returns the rule severity
func (r *ModuleCallValidityRule) Severity() string {
func (r *ModuleCallValidityRule) Severity() tflint.Severity {
return tflint.ERROR
}

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

// Check checks whether ...
func (r *ModuleCallValidityRule) Check(runner tflint.Runner) error {
return runner.WalkModuleCalls(func(call *configs.ModuleCall) error {
if call.SourceAddr != "acceptable/source" {
return runner.EmitIssue(r, "unacceptable module source", call.SourceAddrRange)
content, err := runner.GetModuleContent(&hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "module",
LabelNames: []string{"name"},
Body: &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{
{Name: "source"},
{Name: "providers"},
{Name: "version"},
},
},
},
},
}, nil)
if err != nil {
return err
}

for _, module := range content.Blocks {
if sourceAttr, exists := module.Body.Attributes["source"]; exists {
var source string
val, diags := sourceAttr.Expr.Value(nil)
if diags.HasErrors() {
return diags
}
if err := gocty.FromCtyValue(val, &source); err != nil {
return err
}

if source != "acceptable/source" {
if err := runner.EmitIssue(r, "unacceptable module source", sourceAttr.Expr.Range()); err != nil {
return err
}
}
}

if len(call.Providers) != 0 {
return runner.EmitIssue(r, "must not pass providers", hcl.Range{})
if _, exists := module.Body.Attributes["providers"]; exists {
if err := runner.EmitIssue(r, "must not pass providers", hcl.Range{}); err != nil {
return err
}
}

expectedVersion, _ := version.NewVersion("0.1.0")
if !call.Version.Required.Check(expectedVersion) {
return runner.EmitIssue(r, "must accept version 0.1.0", call.Version.DeclRange)
if versionAttr, exists := module.Body.Attributes["version"]; exists {
var versionConstraint string
val, diags := versionAttr.Expr.Value(nil)
if diags.HasErrors() {
return diags
}
if err := gocty.FromCtyValue(val, &versionConstraint); err != nil {
return err
}

expectedVersion, _ := version.NewVersion("0.1.0")
constraint, err := version.NewConstraint(versionConstraint)
if err != nil {
return err
}
if !constraint.Check(expectedVersion) {
if err := runner.EmitIssue(r, "must accept version 0.1.0", versionAttr.Expr.Range()); err != nil {
return err
}
}
}
}

return nil
})
return nil
}
Loading

0 comments on commit 96b72ba

Please sign in to comment.