Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix recursive variable reference resolution #677

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
]
}