Skip to content

Commit

Permalink
fix: prevent runtime error when instanceType/nodeType have no dot (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumelecerf authored Jan 19, 2024
1 parent 4227f38 commit c4bf2f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
7 changes: 6 additions & 1 deletion rules/aws_db_instance_previous_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ func (r *AwsDBInstancePreviousTypeRule) Check(runner tflint.Runner) error {
}

err := runner.EvaluateExpr(attribute.Expr, func(instanceType string) error {
if r.previousInstanceTypes[strings.Split(instanceType, ".")[1]] {
parts := strings.Split(instanceType, ".")
if len(parts) < 3 {
return nil
}

if r.previousInstanceTypes[parts[1]] {
runner.EmitIssue(
r,
fmt.Sprintf("\"%s\" is previous generation instance type.", instanceType),
Expand Down
8 changes: 8 additions & 0 deletions rules/aws_db_instance_previous_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ resource "aws_db_instance" "mysql" {
Content: `
resource "aws_db_instance" "mysql" {
instance_class = "db.t2.micro"
}`,
Expected: helper.Issues{},
},
{
Name: "test is not previous type",
Content: `
resource "aws_db_instance" "mysql" {
instance_class = "test"
}`,
Expected: helper.Issues{},
},
Expand Down
7 changes: 6 additions & 1 deletion rules/aws_elasticache_replication_group_previous_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ func (r *AwsElastiCacheReplicationGroupPreviousTypeRule) Check(runner tflint.Run
}

err := runner.EvaluateExpr(attribute.Expr, func(nodeType string) error {
if previousElastiCacheNodeTypes[strings.Split(nodeType, ".")[1]] {
parts := strings.Split(nodeType, ".")
if len(parts) != 3 {
return nil
}

if previousElastiCacheNodeTypes[parts[1]] {
runner.EmitIssue(
r,
fmt.Sprintf("\"%s\" is previous generation node type.", nodeType),
Expand Down
8 changes: 8 additions & 0 deletions rules/aws_elasticache_replication_group_previous_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ resource "aws_elasticache_replication_group" "redis" {
Content: `
resource "aws_elasticache_replication_group" "redis" {
node_type = "cache.t2.micro"
}`,
Expected: helper.Issues{},
},
{
Name: "test is not previous type",
Content: `
resource "aws_elasticache_replication_group" "redis" {
node_type = "test"
}`,
Expected: helper.Issues{},
},
Expand Down

0 comments on commit c4bf2f0

Please sign in to comment.