Skip to content

Commit c4bf2f0

Browse files
fix: prevent runtime error when instanceType/nodeType have no dot (#600)
1 parent 4227f38 commit c4bf2f0

4 files changed

+28
-2
lines changed

rules/aws_db_instance_previous_type.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ func (r *AwsDBInstancePreviousTypeRule) Check(runner tflint.Runner) error {
7070
}
7171

7272
err := runner.EvaluateExpr(attribute.Expr, func(instanceType string) error {
73-
if r.previousInstanceTypes[strings.Split(instanceType, ".")[1]] {
73+
parts := strings.Split(instanceType, ".")
74+
if len(parts) < 3 {
75+
return nil
76+
}
77+
78+
if r.previousInstanceTypes[parts[1]] {
7479
runner.EmitIssue(
7580
r,
7681
fmt.Sprintf("\"%s\" is previous generation instance type.", instanceType),

rules/aws_db_instance_previous_type_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ resource "aws_db_instance" "mysql" {
3636
Content: `
3737
resource "aws_db_instance" "mysql" {
3838
instance_class = "db.t2.micro"
39+
}`,
40+
Expected: helper.Issues{},
41+
},
42+
{
43+
Name: "test is not previous type",
44+
Content: `
45+
resource "aws_db_instance" "mysql" {
46+
instance_class = "test"
3947
}`,
4048
Expected: helper.Issues{},
4149
},

rules/aws_elasticache_replication_group_previous_type.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ func (r *AwsElastiCacheReplicationGroupPreviousTypeRule) Check(runner tflint.Run
6161
}
6262

6363
err := runner.EvaluateExpr(attribute.Expr, func(nodeType string) error {
64-
if previousElastiCacheNodeTypes[strings.Split(nodeType, ".")[1]] {
64+
parts := strings.Split(nodeType, ".")
65+
if len(parts) != 3 {
66+
return nil
67+
}
68+
69+
if previousElastiCacheNodeTypes[parts[1]] {
6570
runner.EmitIssue(
6671
r,
6772
fmt.Sprintf("\"%s\" is previous generation node type.", nodeType),

rules/aws_elasticache_replication_group_previous_type_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ resource "aws_elasticache_replication_group" "redis" {
3636
Content: `
3737
resource "aws_elasticache_replication_group" "redis" {
3838
node_type = "cache.t2.micro"
39+
}`,
40+
Expected: helper.Issues{},
41+
},
42+
{
43+
Name: "test is not previous type",
44+
Content: `
45+
resource "aws_elasticache_replication_group" "redis" {
46+
node_type = "test"
3947
}`,
4048
Expected: helper.Issues{},
4149
},

0 commit comments

Comments
 (0)