Skip to content

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
merged 6 commits into from
Feb 11, 2021
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
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ These rules warn of possible errors that can occur at `terraform apply`. Rules m
|aws_alb_invalid_security_group|✔|
|aws_alb_invalid_subnet|✔|
|aws_db_instance_invalid_db_subnet_group|✔|
|[aws_db_instance_invalid_engine](aws_db_instance_invalid_engine.md)||
|aws_db_instance_invalid_option_group|✔|
|aws_db_instance_invalid_parameter_group|✔|
|aws_db_instance_invalid_type||
Expand Down
38 changes: 38 additions & 0 deletions docs/rules/aws_db_instance_invalid_engine.md
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)
80 changes: 80 additions & 0 deletions rules/aws_db_instance_invalid_engine.go
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())
Copy link
Member

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... 😓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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?

}

// 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
})
})
}
55 changes: 55 additions & 0 deletions rules/aws_db_instance_invalid_engine_test.go
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)
}
}
1 change: 1 addition & 0 deletions rules/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
// Rules is a list of all rules
var Rules = append([]tflint.Rule{
NewAwsDBInstanceDefaultParameterGroupRule(),
NewAwsDBInstanceInvalidEngineRule(),
NewAwsDBInstanceInvalidTypeRule(),
NewAwsDBInstancePreviousTypeRule(),
NewAwsDynamoDBTableInvalidStreamViewTypeRule(),
Expand Down