generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 75
Add lint for db_instance's engine attribute #61
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
Merged
wata727
merged 6 commits into
terraform-linters:master
from
chaspy:chaspy/lint-rds-engine
Feb 11, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,38 @@ | ||
# aws_db_instance_invalid_engine | ||
|
||
Disallow using invalid engine name. | ||
|
||
## Example | ||
|
||
```hcl | ||
resource "aws_db_instance" "default" { | ||
allocated_storage = 20 | ||
engine = "mysql57" // invalid engine name! | ||
engine_version = "5.7" | ||
instance_class = "db.t2.micro" | ||
name = "mydb" | ||
username = "foo" | ||
password = "bar" | ||
db_subnet_group_name = "my_database_subnet_group" | ||
parameter_group_name = "default.mysql5.6" | ||
} | ||
``` | ||
|
||
``` | ||
$ tflint | ||
1 issue(s) found: | ||
|
||
Warning: "mysql57" is invalid engine. (aws_db_invalid_engine) | ||
|
||
on template.tf line 3: | ||
3: engine = "mysql57" // invalid engine name! | ||
|
||
``` | ||
|
||
## Why | ||
|
||
Apply will fail. (Plan will succeed with the invalid value though) | ||
|
||
## How To Fix | ||
|
||
Select valid engine name according to the [document](https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-engine-versions.html#options) |
This file contains hidden or 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" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
"github.com/terraform-linters/tflint-ruleset-aws/project" | ||
) | ||
|
||
// AwsDBInstanceInvalidEngineRule checks whether "aws_db_instance" has invalid engine. | ||
type AwsDBInstanceInvalidEngineRule struct { | ||
resource string | ||
attributeName string | ||
engines map[string]bool | ||
} | ||
|
||
// NewAwsDBInstanceInvalidEngineRule returns new rule with default attributes | ||
func NewAwsDBInstanceInvalidEngineRule() *AwsDBInstanceInvalidEngineRule { | ||
return &AwsDBInstanceInvalidEngineRule{ | ||
resource: "aws_db_instance", | ||
attributeName: "engine", | ||
engines: map[string]bool{ | ||
// https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-engine-versions.html#options | ||
"aurora": true, | ||
"aurora-mysql": true, | ||
"aurora-postgresql": true, | ||
"mariadb": true, | ||
"mysql": true, | ||
"oracle-ee": true, | ||
"oracle-se2": true, | ||
"oracle-se1": true, | ||
"oracle-se": true, | ||
"postgres": true, | ||
"sqlserver-ee": true, | ||
"sqlserver-se": true, | ||
"sqlserver-ex": true, | ||
"sqlserver-web": true, | ||
}, | ||
} | ||
} | ||
|
||
// Name returns the rule name | ||
func (r *AwsDBInstanceInvalidEngineRule) Name() string { | ||
return "aws_db_instance_invalid_engine" | ||
} | ||
|
||
// Enabled returns whether the rule is enabled by default | ||
func (r *AwsDBInstanceInvalidEngineRule) Enabled() bool { | ||
return true | ||
} | ||
|
||
// Severity returns the rule severity | ||
func (r *AwsDBInstanceInvalidEngineRule) Severity() string { | ||
return tflint.ERROR | ||
} | ||
|
||
// Link returns the rule reference link | ||
func (r *AwsDBInstanceInvalidEngineRule) Link() string { | ||
return project.ReferenceLink(r.Name()) | ||
} | ||
|
||
// Check checks whether "aws_db_instance" has invalid engine. | ||
func (r *AwsDBInstanceInvalidEngineRule) Check(runner tflint.Runner) error { | ||
return runner.WalkResourceAttributes(r.resource, r.attributeName, func(attribute *hcl.Attribute) error { | ||
var engine string | ||
err := runner.EvaluateExpr(attribute.Expr, &engine, nil) | ||
|
||
return runner.EnsureNoError(err, func() error { | ||
if !r.engines[engine] { | ||
runner.EmitIssueOnExpr( | ||
r, | ||
fmt.Sprintf("\"%s\" is invalid engine.", engine), | ||
attribute.Expr, | ||
) | ||
} | ||
return nil | ||
}) | ||
}) | ||
} |
This file contains hidden or 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,55 @@ | ||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/helper" | ||
) | ||
|
||
func Test_AwsDBInstanceInvalidEngine(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Content string | ||
Expected helper.Issues | ||
}{ | ||
{ | ||
Name: "aurora-postgres is invalid", | ||
Content: ` | ||
resource "aws_db_instance" "aurora_postgresql" { | ||
engine = "aurora-postgres" | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewAwsDBInstanceInvalidEngineRule(), | ||
Message: "\"aurora-postgres\" is invalid engine.", | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 3, Column: 14}, | ||
End: hcl.Pos{Line: 3, Column: 31}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "aurora-postgresql is valid", | ||
Content: ` | ||
resource "aws_db_instance" "aurora_postgresql" { | ||
engine = "aurora-postgresql" | ||
}`, | ||
Expected: helper.Issues{}, | ||
}, | ||
} | ||
|
||
rule := NewAwsDBInstanceInvalidEngineRule() | ||
|
||
for _, tc := range cases { | ||
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) | ||
|
||
if err := rule.Check(runner); err != nil { | ||
t.Fatalf("Unexpected error occurred: %s", err) | ||
} | ||
|
||
helper.AssertIssues(t, tc.Expected, runner.Issues) | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rule doesn't have documentation, so it's better to return an empty string instead.
By the way, the
aws_db_instance_invalid_type
rule also returns a link, but since there is no documentation, it seems that this should also return an empty string... 😓There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understand, cool 🆒
https://github.com/terraform-linters/tflint-ruleset-aws/blob/master/project/main.go#L10
https://github.com/terraform-linters/tflint-ruleset-aws/tree/v0.2.1/docs/rules
I will add a document for the rule 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed 👍 6a9575c
How about this?