-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make GetRuleConfigContent doesn't return an error even if config not …
…found (#1481)
- Loading branch information
Showing
16 changed files
with
205 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
integrationtest/inspection/enable-config-rule-by-cli/.tflint.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
plugin "testing" { | ||
enabled = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resource "aws_db_instance" "main" { | ||
name = "staging" | ||
} |
25 changes: 25 additions & 0 deletions
25
integrationtest/inspection/enable-config-rule-by-cli/result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"issues": [ | ||
{ | ||
"rule": { | ||
"name": "aws_db_instance_with_default_config_example", | ||
"severity": "warning", | ||
"link": "" | ||
}, | ||
"message": "DB name is staging, config=default", | ||
"range": { | ||
"filename": "main.tf", | ||
"start": { | ||
"line": 2, | ||
"column": 10 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 19 | ||
} | ||
}, | ||
"callers": [] | ||
} | ||
], | ||
"errors": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugin "testing" { | ||
enabled = true | ||
} | ||
|
||
rule "aws_db_instance_with_default_config_example" { | ||
enabled = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resource "aws_db_instance" "main" { | ||
name = "staging" | ||
} |
25 changes: 25 additions & 0 deletions
25
integrationtest/inspection/rule-optional-config/result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"issues": [ | ||
{ | ||
"rule": { | ||
"name": "aws_db_instance_with_default_config_example", | ||
"severity": "warning", | ||
"link": "" | ||
}, | ||
"message": "DB name is staging, config=default", | ||
"range": { | ||
"filename": "main.tf", | ||
"start": { | ||
"line": 2, | ||
"column": 10 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 19 | ||
} | ||
}, | ||
"callers": [] | ||
} | ||
], | ||
"errors": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugin "testing" { | ||
enabled = true | ||
} | ||
|
||
rule "aws_s3_bucket_with_config_example" { | ||
enabled = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"issues": [], | ||
"errors": [ | ||
{ | ||
"message": "Failed to check ruleset; Failed to check `aws_s3_bucket_with_config_example` rule: .tflint.hcl:5,42-42: Missing required argument; The argument \"name\" is required, but no definition was found.", | ||
"severity": "error" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
plugin/stub-generator/sources/testing/rules/aws_db_instance_with_default_config_example.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package rules | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-linters/tflint-plugin-sdk/hclext" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
) | ||
|
||
// AwsDBInstanceWithDefaultConfigExampleRule checks whether ... | ||
type AwsDBInstanceWithDefaultConfigExampleRule struct { | ||
tflint.DefaultRule | ||
} | ||
|
||
type awsDBInstanceWithDefaultConfigExampleRule struct { | ||
Name string `hclext:"name,optional"` | ||
} | ||
|
||
// NewAwsDBInstanceWithDefaultConfigExampleRule returns a new rule | ||
func NewAwsDBInstanceWithDefaultConfigExampleRule() *AwsDBInstanceWithDefaultConfigExampleRule { | ||
return &AwsDBInstanceWithDefaultConfigExampleRule{} | ||
} | ||
|
||
// Name returns the rule name | ||
func (r *AwsDBInstanceWithDefaultConfigExampleRule) Name() string { | ||
return "aws_db_instance_with_default_config_example" | ||
} | ||
|
||
// Enabled returns whether the rule is enabled by default | ||
func (r *AwsDBInstanceWithDefaultConfigExampleRule) Enabled() bool { | ||
return true | ||
} | ||
|
||
// Severity returns the rule severity | ||
func (r *AwsDBInstanceWithDefaultConfigExampleRule) Severity() tflint.Severity { | ||
return tflint.WARNING | ||
} | ||
|
||
// Link returns the rule reference link | ||
func (r *AwsDBInstanceWithDefaultConfigExampleRule) Link() string { | ||
return "" | ||
} | ||
|
||
// Check checks whether ... | ||
func (r *AwsDBInstanceWithDefaultConfigExampleRule) Check(runner tflint.Runner) error { | ||
config := awsDBInstanceWithDefaultConfigExampleRule{Name: "default"} | ||
if err := runner.DecodeRuleConfig(r.Name(), &config); err != nil { | ||
return err | ||
} | ||
|
||
resources, err := runner.GetResourceContent("aws_db_instance", &hclext.BodySchema{ | ||
Attributes: []hclext.AttributeSchema{{Name: "name"}}, | ||
}, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, resource := range resources.Blocks { | ||
attribute, exists := resource.Body.Attributes["name"] | ||
if !exists { | ||
continue | ||
} | ||
|
||
var name string | ||
err := runner.EvaluateExpr(attribute.Expr, &name, nil) | ||
|
||
err = runner.EnsureNoError(err, func() error { | ||
return runner.EmitIssue( | ||
r, | ||
fmt.Sprintf("DB name is %s, config=%s", name, config.Name), | ||
attribute.Expr.Range(), | ||
) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters