Skip to content
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

Bump tflint-plugin-sdk to v0.10.0 #48

Merged
merged 1 commit into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is a template repository for building a custom ruleset. You can create a pl

## Requirements

- TFLint v0.30+
- TFLint v0.35+
- Go v1.17

## Installation
Expand Down
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.10.0
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
}
Loading