Skip to content

Commit c90afb7

Browse files
committed
Add lint for db_instance engine
1 parent 6a45222 commit c90afb7

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package rules
2+
3+
import (
4+
"fmt"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
8+
"github.com/terraform-linters/tflint-ruleset-aws/project"
9+
)
10+
11+
// AwsDBInstanceInvalidEngineRule checks whether "aws_db_instance" has invalid engine.
12+
type AwsDBInstanceInvalidEngineRule struct {
13+
resource string
14+
attributeName string
15+
engines map[string]bool
16+
}
17+
18+
// NewAwsDBInstanceInvalidEngineRule returns new rule with default attributes
19+
func NewAwsDBInstanceInvalidEngineRule() *AwsDBInstanceInvalidEngineRule {
20+
return &AwsDBInstanceInvalidEngineRule{
21+
resource: "aws_db_instance",
22+
attributeName: "instance_class",
23+
engines: map[string]bool{
24+
// https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-engine-versions.html#options
25+
"aurora": true,
26+
"aurora-mysql": true,
27+
"aurora-postgresql": true,
28+
"mariadb": true,
29+
"mysql": true,
30+
"oracle-ee": true,
31+
"oracle-se2": true,
32+
"oracle-se1": true,
33+
"oracle-se": true,
34+
"postgres": true,
35+
"sqlserver-ee": true,
36+
"sqlserver-se": true,
37+
"sqlserver-ex": true,
38+
"sqlserver-web": true,
39+
},
40+
}
41+
}
42+
43+
// Name returns the rule name
44+
func (r *AwsDBInstanceInvalidEngineRule) Name() string {
45+
return "aws_db_instance_invalid_type"
46+
}
47+
48+
// Enabled returns whether the rule is enabled by default
49+
func (r *AwsDBInstanceInvalidEngineRule) Enabled() bool {
50+
return true
51+
}
52+
53+
// Severity returns the rule severity
54+
func (r *AwsDBInstanceInvalidEngineRule) Severity() string {
55+
return tflint.ERROR
56+
}
57+
58+
// Link returns the rule reference link
59+
func (r *AwsDBInstanceInvalidEngineRule) Link() string {
60+
return project.ReferenceLink(r.Name())
61+
}
62+
63+
// Check checks whether "aws_db_instance" has invalid engine.
64+
func (r *AwsDBInstanceInvalidEngineRule) Check(runner tflint.Runner) error {
65+
return runner.WalkResourceAttributes(r.resource, r.attributeName, func(attribute *hcl.Attribute) error {
66+
var engine string
67+
err := runner.EvaluateExpr(attribute.Expr, &engine, nil)
68+
69+
return runner.EnsureNoError(err, func() error {
70+
if !r.engines[engine] {
71+
runner.EmitIssueOnExpr(
72+
r,
73+
fmt.Sprintf("\"%s\" is invalid engine.", engine),
74+
attribute.Expr,
75+
)
76+
}
77+
return nil
78+
})
79+
})
80+
}

0 commit comments

Comments
 (0)