Skip to content

Commit 20e2f1b

Browse files
authored
Add aws_api_gateway_model_invalid_name rule (#101)
1 parent faa7621 commit 20e2f1b

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ These rules warn of possible errors that can occur at `terraform apply`. Rules m
1010
| --- | --- | --- | --- |
1111
|aws_alb_invalid_security_group|Disallow using invalid security groups|||
1212
|aws_alb_invalid_subnet|Disallow using invalid subnets|||
13+
|aws_api_gateway_model_invalid_name|Disallow using invalid name|||
1314
|aws_db_instance_invalid_db_subnet_group|Disallow using invalid subnet group name|||
1415
|[aws_db_instance_invalid_engine](aws_db_instance_invalid_engine.md)|Disallow using invalid engine name|||
1516
|aws_db_instance_invalid_option_group|Disallow using invalid option group|||

docs/rules/README.md.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ These rules warn of possible errors that can occur at `terraform apply`. Rules m
1010
| --- | --- | --- | --- |
1111
|aws_alb_invalid_security_group|Disallow using invalid security groups|✔|✔|
1212
|aws_alb_invalid_subnet|Disallow using invalid subnets|✔|✔|
13+
|aws_api_gateway_model_invalid_name|Disallow using invalid name||✔|
1314
|aws_db_instance_invalid_db_subnet_group|Disallow using invalid subnet group name|✔|✔|
1415
|[aws_db_instance_invalid_engine](aws_db_instance_invalid_engine.md)|Disallow using invalid engine name||✔|
1516
|aws_db_instance_invalid_option_group|Disallow using invalid option group|✔|✔|
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package rules
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
7+
hcl "github.com/hashicorp/hcl/v2"
8+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
9+
)
10+
11+
// AwsAPIGatewayModelInvalidNameRule checks the name is alphanumeric
12+
type AwsAPIGatewayModelInvalidNameRule struct {
13+
resourceType string
14+
attributeName string
15+
pattern *regexp.Regexp
16+
}
17+
18+
// NewAwsAPIGatewayModelInvalidNameRule returns new rule with default attributes
19+
func NewAwsAPIGatewayModelInvalidNameRule() *AwsAPIGatewayModelInvalidNameRule {
20+
return &AwsAPIGatewayModelInvalidNameRule{
21+
resourceType: "aws_api_gateway_model",
22+
attributeName: "name",
23+
pattern: regexp.MustCompile("^[a-zA-Z0-9]+$"),
24+
}
25+
}
26+
27+
// Name returns the rule name
28+
func (r *AwsAPIGatewayModelInvalidNameRule) Name() string {
29+
return "aws_api_gateway_model_invalid_name"
30+
}
31+
32+
// Enabled returns whether the rule is enabled by default
33+
func (r *AwsAPIGatewayModelInvalidNameRule) Enabled() bool {
34+
return true
35+
}
36+
37+
// Severity returns the rule severity
38+
func (r *AwsAPIGatewayModelInvalidNameRule) Severity() string {
39+
return tflint.ERROR
40+
}
41+
42+
// Link returns the rule reference link
43+
func (r *AwsAPIGatewayModelInvalidNameRule) Link() string {
44+
return ""
45+
}
46+
47+
// Check checks the name attributes is matched with ^[a-zA-Z0-9]+$ regexp
48+
func (r *AwsAPIGatewayModelInvalidNameRule) Check(runner tflint.Runner) error {
49+
return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
50+
var val string
51+
err := runner.EvaluateExpr(attribute.Expr, &val, nil)
52+
53+
return runner.EnsureNoError(err, func() error {
54+
if !r.pattern.MatchString(val) {
55+
runner.EmitIssueOnExpr(
56+
r,
57+
fmt.Sprintf(`%s does not match valid pattern %s`, val, `^[a-zA-Z0-9]+$`),
58+
attribute.Expr,
59+
)
60+
}
61+
return nil
62+
})
63+
})
64+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
8+
)
9+
10+
func Test_AwsAPIGatewayModelInvalidName(t *testing.T) {
11+
cases := []struct {
12+
Name string
13+
Content string
14+
Expected helper.Issues
15+
}{
16+
{
17+
Name: "underscore",
18+
Content: `
19+
resource "aws_api_gateway_model" "demo" {
20+
name = "demo_model"
21+
}
22+
`,
23+
Expected: helper.Issues{
24+
{
25+
Rule: NewAwsAPIGatewayModelInvalidNameRule(),
26+
Message: `demo_model does not match valid pattern ^[a-zA-Z0-9]+$`,
27+
Range: hcl.Range{
28+
Filename: "resource.tf",
29+
Start: hcl.Pos{Line: 3, Column: 10},
30+
End: hcl.Pos{Line: 3, Column: 22},
31+
},
32+
},
33+
},
34+
},
35+
{
36+
Name: "alphanumeric",
37+
Content: `
38+
resource "aws_api_gateway_model" "demo" {
39+
name = "user"
40+
}
41+
`,
42+
Expected: helper.Issues{},
43+
},
44+
}
45+
46+
rule := NewAwsAPIGatewayModelInvalidNameRule()
47+
48+
for _, tc := range cases {
49+
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
50+
51+
if err := rule.Check(runner); err != nil {
52+
t.Fatalf("Unexpected error occurred: %s", err)
53+
}
54+
55+
helper.AssertIssues(t, tc.Expected, runner.Issues)
56+
}
57+
}

rules/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ var Rules = append([]tflint.Rule{
2828
NewAwsS3BucketInvalidRegionRule(),
2929
NewAwsS3BucketNameRule(),
3030
NewAwsSpotFleetRequestInvalidExcessCapacityTerminationPolicyRule(),
31+
NewAwsAPIGatewayModelInvalidNameRule(),
3132
}, models.Rules...)

0 commit comments

Comments
 (0)