Skip to content

Commit

Permalink
fix recursive variable reference resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed Apr 19, 2021
1 parent 2033718 commit 3b6a7c9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/iac-providers/terraform/commons/variable-references.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ func (r *RefResolver) ResolveVarRefFromParentModuleCall(varRef string) interface
if reflect.TypeOf(val).Kind() == reflect.String {
valStr := val.(string)
resolvedVal := strings.Replace(varRef, varExpr, valStr, 1)
if varRef == resolvedVal {
if strings.Contains(valStr, varExpr) {
zap.S().Debugf("resolved str variable ref refers to self: '%v'", varRef)
return varRef
return resolvedVal
}
zap.S().Debugf("resolved str variable ref: '%v', value: '%v'", varRef, string(resolvedVal))
return r.ResolveStrRef(resolvedVal)
Expand Down
7 changes: 7 additions & 0 deletions pkg/iac-providers/terraform/v14/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func TestLoadIacDir(t *testing.T) {
tfv14: TfV14{},
wantErr: nil,
},
{
name: "recursive loop while resolving variables",
tfConfigDir: filepath.Join(testDataDir, "recursive-loop"),
tfJSONFile: filepath.Join(tfJSONDir, "recursive-loop.json"),
tfv14: TfV14{},
wantErr: nil,
},
}

for _, tt := range table2 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "filename" {
type = string
}

module "dummy" {
source = "./dummy"

filename = "${path.module}/${var.filename}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
variable "filename" {
type = string
}

resource "null_resource" "example" {
container_definitions = templatefile(
var.filename,
{
foo = "bar"
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"null_resource": [
{
"id": "null_resource.example",
"name": "example",
"source": "dummy/main.tf",
"line": 5,
"type": "null_resource",
"config": {
"container_definitions": "${templatefile(\n ${path.module}/${var.filename},\n {\n foo = \"bar\"\n }\n )}"
},
"skip_rules": null
}
]
}

0 comments on commit 3b6a7c9

Please sign in to comment.