8000 fix(provisioner/terraform/tfparse): skip evaluation of unrelated parameters by johnstcn · Pull Request #16023 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix(provisioner/terraform/tfparse): skip evaluation of unrelated parameters #16023

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

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fallback to JSON-encoding default variable values
  • Loading branch information
johnstcn committed Jan 3, 2025
commit 222fd902aaa11bdaed1cd52d335ab580cbf60bb3
8 changes: 6 additions & 2 deletions provisioner/terraform/tfparse/tfparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,11 @@ func interfaceToString(i interface{}) (string, error) {
return strconv.FormatFloat(v, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(v), nil
default:
return "", xerrors.Errorf("unsupported type %T", v)
default: // just try to JSON-encode it.
var sb strings.Builder
if err := json.NewEncoder(&sb).Encode(i); err != nil {
return "", xerrors.Errorf("convert %T: %w", v, err)
}
return strings.TrimSpace(sb.String()), nil
}
}
50 changes: 16 additions & 34 deletions provisioner/terraform/tfparse/tfparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = bool
default = true
}
variable "listvar" {
type = list(string)
default = ["a"]
}
variable "mapvar" {
type = map(string)
default = {"a": "b"}
}
data "coder_parameter" "stringparam" {
name = "stringparam"
type = "string"
Expand All @@ -487,6 +495,8 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
"stringvar" = var.stringvar
"numvar" = var.numvar
"boolvar" = var.boolvar
"listvar" = var.listvar
"mapvar" = var.mapvar
"stringparam" = data.coder_parameter.stringparam.value
"numparam" = data.coder_parameter.numparam.value
"boolparam" = data.coder_parameter.boolparam.value
Expand All @@ -498,47 +508,15 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
"stringvar": "a",
"numvar": "1",
"boolvar": "true",
"listvar": `["a"]`,
"mapvar": `{"a":"b"}`,
"stringparam": "a",
"numparam": "1",
"boolparam": "true",
"listparam": `["a", "b"]`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: this ends up being allowed due to the default of coder_parameter being a string.

},
expectError: ``,
},
{
name: "unsupported list variable",
files: map[string]string{
"main.tf": `
variable "listvar" {
type = list(string)
default = ["a"]
}
data "coder_workspace_tags" "tags" {
tags = {
listvar = var.listvar
}
}`,
},
expectTags: nil,
expectError: `can't convert variable default value to string: unsupported type []interface {}`,
},
{
name: "unsupported map variable",
files: map[string]string{
"main.tf": `
variable "mapvar" {
type = map(string)
default = {"a": "b"}
}
data "coder_workspace_tags" "tags" {
tags = {
mapvar = var.mapvar
}
}`,
},
expectTags: nil,
expectError: `can't convert variable default value to string: unsupported type map[string]interface {}`,
},
{
name: "overlapping var name",
files: map[string]string{
Expand All @@ -547,6 +525,10 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "1"
}
variable "unused" {
type = map(string)
default = {"a" : "b"}
}
variable "ab" {
description = "This is a variable of type string"
type = string
Expand Down
Loading
0