generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathaws_elastic_beanstalk_environment_invalid_name_format.go
82 lines (69 loc) · 2.46 KB
/
aws_elastic_beanstalk_environment_invalid_name_format.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
package rules
import (
"fmt"
"regexp"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-aws/project"
)
// AwsElasticBeanstalkEnvironmentInvalidNameFormatRule checks EB environment name matches a pattern
type AwsElasticBeanstalkEnvironmentInvalidNameFormatRule struct {
tflint.DefaultRule
resourceType string
attributeName string
pattern *regexp.Regexp
}
// NewAwsElasticBeanstalkEnvironmentInvalidNameFormatRule returns new rule with default attributes
func NewAwsElasticBeanstalkEnvironmentInvalidNameFormatRule() *AwsElasticBeanstalkEnvironmentInvalidNameFormatRule {
return &AwsElasticBeanstalkEnvironmentInvalidNameFormatRule{
resourceType: "aws_elastic_beanstalk_environment",
attributeName: "name",
pattern: regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]$"),
}
}
// Name returns the rule name
func (r *AwsElasticBeanstalkEnvironmentInvalidNameFormatRule) Name() string {
return "aws_elastic_beanstalk_environment_invalid_name_format"
}
// Enabled returns whether the rule is enabled by default
func (r *AwsElasticBeanstalkEnvironmentInvalidNameFormatRule) Enabled() bool {
return true
}
// Severity returns the rule severity
func (r *AwsElasticBeanstalkEnvironmentInvalidNameFormatRule) Severity() tflint.Severity {
return tflint.ERROR
}
// Link returns the rule reference link
func (r *AwsElasticBeanstalkEnvironmentInvalidNameFormatRule) Link() string {
return project.ReferenceLink(r.Name())
}
// Check checks the environment name matches the pattern provided
func (r *AwsElasticBeanstalkEnvironmentInvalidNameFormatRule) 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 {
if !r.pattern.MatchString(val) {
runner.EmitIssue(
r,
fmt.Sprintf(`%s does not match constraint: must contain only letters, digits, and the dash `+
`character and may not start or end with a dash (^[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]$)`, val),
attribute.Expr.Range(),
)
}
return nil
}, nil)
if err != nil {
return err
}
}
return nil
}