generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 70
/
aws_dynamodb_table_invalid_stream_view_type.go
91 lines (78 loc) · 2.17 KB
/
aws_dynamodb_table_invalid_stream_view_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
package rules
import (
"fmt"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)
// AwsDynamoDBTableInvalidStreamViewTypeRule checks the pattern is valid
type AwsDynamoDBTableInvalidStreamViewTypeRule struct {
tflint.DefaultRule
resourceType string
attributeName string
enum []string
}
// NewAwsDynamoDBTableInvalidStreamViewTypeRule returns new rule with default attributes
func NewAwsDynamoDBTableInvalidStreamViewTypeRule() *AwsDynamoDBTableInvalidStreamViewTypeRule {
return &AwsDynamoDBTableInvalidStreamViewTypeRule{
resourceType: "aws_dynamodb_table",
attributeName: "stream_view_type",
enum: []string{
"",
"NEW_IMAGE",
"OLD_IMAGE",
"NEW_AND_OLD_IMAGES",
"KEYS_ONLY",
},
}
}
// Name returns the rule name
func (r *AwsDynamoDBTableInvalidStreamViewTypeRule) Name() string {
return "aws_dynamodb_table_invalid_stream_view_type"
}
// Enabled returns whether the rule is enabled by default
func (r *AwsDynamoDBTableInvalidStreamViewTypeRule) Enabled() bool {
return true
}
// Severity returns the rule severity
func (r *AwsDynamoDBTableInvalidStreamViewTypeRule) Severity() tflint.Severity {
return tflint.ERROR
}
// Link returns the rule reference link
func (r *AwsDynamoDBTableInvalidStreamViewTypeRule) Link() string {
return ""
}
// Check checks the pattern is valid
func (r *AwsDynamoDBTableInvalidStreamViewTypeRule) 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(val string) error {
found := false
for _, item := range r.enum {
if item == val {
found = true
}
}
if !found {
runner.EmitIssue(
r,
fmt.Sprintf(`"%s" is an invalid value as stream_view_type`, val),
attribute.Expr.Range(),
)
}
return nil
}, nil)
if err != nil {
return err
}
}
return nil
}