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

plugin: Add support for schema mode #1530

Merged
merged 1 commit into from
Oct 2, 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
5 changes: 5 additions & 0 deletions integrationtest/inspection/inspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func TestIntegration(t *testing.T) {
Command: "tflint --format json",
Dir: "sensitive",
},
{
Name: "just attributes",
Command: "tflint --format json",
Dir: "just-attributes",
},
}

// Disable the bundled plugin because the `os.Executable()` is go(1) in the tests
Expand Down
3 changes: 3 additions & 0 deletions integrationtest/inspection/just-attributes/.tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin "testing" {
enabled = true
}
25 changes: 25 additions & 0 deletions integrationtest/inspection/just-attributes/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"issues": [
{
"rule": {
"name": "locals_just_attributes_example",
"severity": "info",
"link": ""
},
"message": "found 3 local values",
"range": {
"filename": "template.tf",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 7
}
},
"callers": []
}
],
"errors": []
}
5 changes: 5 additions & 0 deletions integrationtest/inspection/just-attributes/template.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
locals {
foo = "foo"
bar = "bar"
just_attributes = "just_attributes"
}
1 change: 1 addition & 0 deletions plugin/stub-generator/sources/testing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
rules.NewAwsRoute53RecordEvalOnRootCtxExampleRule(),
rules.NewAwsDBInstanceWithDefaultConfigExampleRule(),
rules.NewAwsCloudFormationStackErrorRule(),
rules.NewLocalsJustAttributesExampleRule(),
},
},
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package rules

import (
"fmt"

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

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

// LocalsJustAttributesExampleRule returns a new rule
func NewLocalsJustAttributesExampleRule() *LocalsJustAttributesExampleRule {
return &LocalsJustAttributesExampleRule{}
}

// Name returns the rule name
func (r *LocalsJustAttributesExampleRule) Name() string {
return "locals_just_attributes_example"
}

// Enabled returns whether the rule is enabled by default
func (r *LocalsJustAttributesExampleRule) Enabled() bool {
return true
}

// Severity returns the rule severity
func (r *LocalsJustAttributesExampleRule) Severity() tflint.Severity {
return tflint.NOTICE
}

// Link returns the rule reference link
func (r *LocalsJustAttributesExampleRule) Link() string {
return ""
}

// Check checks whether ...
func (r *LocalsJustAttributesExampleRule) Check(runner tflint.Runner) error {
body, err := runner.GetModuleContent(&hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "locals",
Body: &hclext.BodySchema{Mode: hclext.SchemaJustAttributesMode},
},
},
}, nil)
if err != nil || len(body.Blocks) == 0 {
return err
}

locals := body.Blocks[0]

if _, exists := locals.Body.Attributes["just_attributes"]; !exists {
return nil
}

return runner.EmitIssue(r, fmt.Sprintf("found %d local values", len(locals.Body.Attributes)), locals.DefRange)
}
32 changes: 32 additions & 0 deletions terraform/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,38 @@ resource "aws_instance" "foo" {
},
},
},
{
name: "just attributes",
files: map[string]string{
"main.tf": `
locals {
foo = "foo"
bar = "bar"
}`,
},
schema: &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "locals",
Body: &hclext.BodySchema{Mode: hclext.SchemaJustAttributesMode},
},
},
},
want: &hclext.BodyContent{
Blocks: hclext.Blocks{
{
Type: "locals",
Body: &hclext.BodyContent{
Attributes: hclext.Attributes{
"foo": &hclext.Attribute{Name: "foo", Range: hcl.Range{Filename: "main.tf"}},
"bar": &hclext.Attribute{Name: "bar", Range: hcl.Range{Filename: "main.tf"}},
},
},
DefRange: hcl.Range{Filename: "main.tf"},
},
},
},
},
{
name: "contains not created resource",
files: map[string]string{
Expand Down