generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 70
/
aws_db_instance_previous_type.go
93 lines (79 loc) · 2.33 KB
/
aws_db_instance_previous_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package rules
import (
"fmt"
"strings"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-aws/project"
)
// AwsDBInstancePreviousTypeRule checks whether the resource uses previous generation instance type
type AwsDBInstancePreviousTypeRule struct {
tflint.DefaultRule
resourceType string
attributeName string
previousInstanceTypes map[string]bool
}
// NewAwsDBInstancePreviousTypeRule returns new rule with default attributes
func NewAwsDBInstancePreviousTypeRule() *AwsDBInstancePreviousTypeRule {
return &AwsDBInstancePreviousTypeRule{
resourceType: "aws_db_instance",
attributeName: "instance_class",
previousInstanceTypes: map[string]bool{
"cr1": true,
"m1": true,
"m2": true,
"m3": true,
"r3": true,
"t1": true,
},
}
}
// Name returns the rule name
func (r *AwsDBInstancePreviousTypeRule) Name() string {
return "aws_db_instance_previous_type"
}
// Enabled returns whether the rule is enabled by default
func (r *AwsDBInstancePreviousTypeRule) Enabled() bool {
return true
}
// Severity returns the rule severity
func (r *AwsDBInstancePreviousTypeRule) Severity() tflint.Severity {
return tflint.WARNING
}
// Link returns the rule reference link
func (r *AwsDBInstancePreviousTypeRule) Link() string {
return project.ReferenceLink(r.Name())
}
// Check checks whether the resource's `instance_class` is included in the list of previous generation instance type
func (r *AwsDBInstancePreviousTypeRule) Check(runner tflint.Runner) error {
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{{Name: r.attributeName}},
}, nil)
if err != nil {
return err
}
for _, resource := range resources.Blocks {
attribute, exists := resource.Body.Attributes[r.attributeName]
if !exists {
continue
}
err := runner.EvaluateExpr(attribute.Expr, func(instanceType string) error {
parts := strings.Split(instanceType, ".")
if len(parts) < 3 {
return nil
}
if r.previousInstanceTypes[parts[1]] {
runner.EmitIssue(
r,
fmt.Sprintf("\"%s\" is previous generation instance type.", instanceType),
attribute.Expr.Range(),
)
}
return nil
}, nil)
if err != nil {
return err
}
}
return nil
}