Skip to content

Commit

Permalink
1. fix panic for list variables
Browse files Browse the repository at this point in the history
2. test for list variables
  • Loading branch information
patilpankaj212 committed Jan 14, 2021
1 parent 5a19951 commit c36691f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/iac-providers/terraform/v12/cty-converters.go
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,22 @@ 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
if strings.Contains(ctyVal.Type().FriendlyName(), "list") {
var allErrs error
if len(ctyVal.AsValueSlice()) > 0 {
for _, v := range ctyVal.AsValueSlice() {
for _, converter := range ctyNativeConverterFuncs {
resolved, err := converter(v)
if err == nil {
val = append(val, resolved)
return val, nil
}
allErrs = errors.Wrap(allErrs, err.Error())
}
}
}
}
return val, fmt.Errorf("list doesn't contain any elements")
}

// ctyToMap tries to converts the incoming cty.Value into map[string]cty.Value
Expand Down
10 changes: 10 additions & 0 deletions pkg/iac-providers/terraform/v12/load-dir_test.go
Expand Up @@ -138,10 +138,20 @@ 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 {
t.Run(tt.name, func(t *testing.T) {
if tt.name != "variables of list type" {
t.Skip()
}
got, gotErr := tt.tfv12.LoadIacDir(tt.tfConfigDir)
if !reflect.DeepEqual(gotErr, tt.wantErr) {
t.Errorf("unexpected error; gotErr: '%v', wantErr: '%v'", gotErr, tt.wantErr)
Expand Down
@@ -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
}
@@ -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"
}
}
@@ -0,0 +1,27 @@
{
"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
],
"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 c36691f

Please sign in to comment.