generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathaws_instance_invalid_ami.go
132 lines (114 loc) · 3.14 KB
/
aws_instance_invalid_ami.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package api
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/smithy-go"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/logger"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-aws/aws"
)
// AwsInstanceInvalidAMIRule checks whether "aws_instance" has invalid AMI ID
type AwsInstanceInvalidAMIRule struct {
tflint.DefaultRule
resourceType string
attributeName string
amiIDs map[string]bool
}
// NewAwsInstanceInvalidAMIRule returns new rule with default attributes
func NewAwsInstanceInvalidAMIRule() *AwsInstanceInvalidAMIRule {
return &AwsInstanceInvalidAMIRule{
resourceType: "aws_instance",
attributeName: "ami",
amiIDs: map[string]bool{},
}
}
// Name returns the rule name
func (r *AwsInstanceInvalidAMIRule) Name() string {
return "aws_instance_invalid_ami"
}
// Enabled returns whether the rule is enabled by default
func (r *AwsInstanceInvalidAMIRule) Enabled() bool {
return true
}
// Severity returns the rule severity
func (r *AwsInstanceInvalidAMIRule) Severity() tflint.Severity {
return tflint.ERROR
}
// Link returns the rule reference link
func (r *AwsInstanceInvalidAMIRule) Link() string {
return ""
}
// Metadata returns the metadata about deep checking
func (r *AwsInstanceInvalidAMIRule) Metadata() interface{} {
return map[string]bool{"deep": true}
}
// Check checks whether "aws_instance" has invalid AMI ID
func (r *AwsInstanceInvalidAMIRule) Check(rr tflint.Runner) error {
runner := rr.(*aws.Runner)
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{
{Name: r.attributeName},
{Name: "provider"},
},
}, nil)
if err != nil {
return err
}
for _, resource := range resources.Blocks {
attribute, exists := resource.Body.Attributes[r.attributeName]
if !exists {
continue
}
awsClient, err := runner.AwsClient(resource.Body.Attributes)
if err != nil {
return err
}
err = runner.EvaluateExpr(attribute.Expr, func(ami string) error {
if !r.amiIDs[ami] {
logger.Debug("Fetch AMI images: %s", ami)
resp, err := awsClient.DescribeImages(&ec2.DescribeImagesInput{
ImageIds: []string{ami},
})
if err != nil {
var aerr smithy.APIError
if errors.As(err, &aerr) {
switch aerr.ErrorCode() {
case "InvalidAMIID.Malformed":
fallthrough
case "InvalidAMIID.NotFound":
fallthrough
case "InvalidAMIID.Unavailable":
runner.EmitIssue(
r,
fmt.Sprintf("\"%s\" is invalid AMI ID.", ami),
attribute.Expr.Range(),
)
return nil
}
}
err := fmt.Errorf("An error occurred while describing images; %w", err)
logger.Error("%s", err)
return err
}
if len(resp) != 0 {
for imageID, exists := range resp {
r.amiIDs[imageID] = exists
}
} else {
runner.EmitIssue(
r,
fmt.Sprintf("\"%s\" is invalid AMI ID.", ami),
attribute.Expr.Range(),
)
}
}
return nil
}, nil)
if err != nil {
return err
}
}
return nil
}