Skip to content

Commit

Permalink
Merge pull request #479 from patilpankaj212/bugFix
Browse files Browse the repository at this point in the history
fix panic for list variables
  • Loading branch information
Cesar Rodriguez authored Jan 15, 2021
2 parents 12f226a + c02bd36 commit 12b58f2
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
25 changes: 22 additions & 3 deletions pkg/iac-providers/terraform/v12/cty-converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package tfv12

import (
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
Expand All @@ -26,7 +28,7 @@ import (
// list of available cty to golang type converters
var (
ctyConverterFuncs = []func(cty.Value) (interface{}, error){ctyToStr, ctyToInt, ctyToBool, ctyToSlice, ctyToMap}
ctyNativeConverterFuncs = []func(cty.Value) (interface{}, error){ctyToStr, ctyToInt, ctyToBool, ctyToSlice}
ctyNativeConverterFuncs = []func(cty.Value) (interface{}, error){ctyToStr, ctyToInt, ctyToBool}
)

// ctyToStr tries to convert the given cty.Value into golang string type
Expand Down Expand Up @@ -54,8 +56,25 @@ func ctyToBool(ctyVal cty.Value) (interface{}, error) {
// interfce{}
func ctyToSlice(ctyVal cty.Value) (interface{}, error) {
var val []interface{}
err := gocty.FromCtyValue(ctyVal, &val)
return val, err
var allErrs error

if strings.Contains(ctyVal.Type().FriendlyName(), "list") {
for _, v := range ctyVal.AsValueSlice() {
for _, converter := range ctyNativeConverterFuncs {
resolved, err := converter(v)
if err == nil {
val = append(val, resolved)
break
}
allErrs = errors.Wrap(allErrs, err.Error())
}
}
if allErrs != nil {
return nil, allErrs
}
return val, nil
}
return val, fmt.Errorf("incorrect type")
}

// ctyToMap tries to converts the incoming cty.Value into map[string]cty.Value
Expand Down
7 changes: 7 additions & 0 deletions pkg/iac-providers/terraform/v12/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ func TestLoadIacDir(t *testing.T) {
tfv12: TfV12{},
wantErr: nil,
},
{
name: "variables of list type",
tfConfigDir: "./testdata/list-type-vars-test",
tfJSONFile: "./testdata/tfjson/list-vars-test.json",
tfv12: TfV12{},
wantErr: nil,
},
}

for _, tt := range table2 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
data "aws_ami" "amazon_linux" {
most_recent = true
}

resource "aws_instance" "app" {
count = var.instance_count

ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type

subnet_id = var.subnet_ids[count.index % length(var.subnet_ids)]
vpc_security_group_ids = var.security_group_ids

tags = var.tags
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
variable "instance_count" {
description = "Number of EC2 instances to deploy"
type = list(number)
default = [1,2]
}

variable "instance_type" {
description = "Type of EC2 instance to use"
type = string
default = "blah_instance"
}

variable "subnet_ids" {
description = "Subnet IDs for EC2 instances"
type = list(string)
default = ["10.0.0.0/8"]
}

variable "security_group_ids" {
description = "Security group IDs for EC2 instances"
type = list(string)
}

variable "tags" {
description = "Tags for instances"
type = map
default = {
"a" = "b"
"c" = "d"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"aws_instance": [
{
"id": "aws_instance.app",
"name": "app",
"source": "main.tf",
"line": 5,
"type": "aws_instance",
"config": {
"ami": "${data.aws_ami.amazon_linux.id}",
"count": [
1,
2
],
"instance_type": "blah_instance",
"subnet_id": [
"10.0.0.0/8"
],
"tags": {
"a": "b",
"c": "d"
},
"vpc_security_group_ids": "${var.security_group_ids}"
},
"skip_rules": null
}
]
}

0 comments on commit 12b58f2

Please sign in to comment.