From a70164f489f57c7428da50ddeb08b5a704cc1c71 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 2 Sep 2020 16:26:50 +0200 Subject: [PATCH] HCL2: when the type of a variable is not known evaluate setting as a litteral. (#9863) * tests * docs --- command/console_test.go | 4 ++++ command/inspect_test.go | 11 +++++++++-- .../test-fixtures/hcl/inspect/fruit_string.pkr.hcl | 5 +++++ .../hcl/variables/list_of_string/var.pkr.hcl | 4 ++++ .../variables/untyped_var/settings.auto.pkrvars.hcl | 2 ++ .../hcl/variables/untyped_var/var.pkr.hcl | 3 +++ hcl2template/types.variables.go | 4 +++- website/pages/docs/from-1.5/variables.mdx | 7 +++++++ 8 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 command/test-fixtures/hcl/variables/list_of_string/var.pkr.hcl create mode 100644 command/test-fixtures/hcl/variables/untyped_var/settings.auto.pkrvars.hcl create mode 100644 command/test-fixtures/hcl/variables/untyped_var/var.pkr.hcl diff --git a/command/console_test.go b/command/console_test.go index 23d56296f..3988c648e 100644 --- a/command/console_test.go +++ b/command/console_test.go @@ -32,6 +32,10 @@ func Test_console(t *testing.T) { {"var.images", []string{"console", filepath.Join(testFixture("var-arg"), "map.pkr.hcl")}, nil, "{\n" + ` "key" = "value"` + "\n}\n"}, {"path.cwd", []string{"console", filepath.Join(testFixture("var-arg"), "map.pkr.hcl")}, nil, strings.ReplaceAll(cwd, `\`, `/`) + "\n"}, {"path.root", []string{"console", filepath.Join(testFixture("var-arg"), "map.pkr.hcl")}, nil, strings.ReplaceAll(testFixture("var-arg"), `\`, `/`) + "\n"}, + {"var.list_of_string[0]", []string{"console", `-var=list_of_string=["first"]`, filepath.Join(testFixture("hcl", "variables", "list_of_string"))}, nil, "first\n"}, + {"var.untyped[2]", []string{"console", filepath.Join(testFixture("hcl", "variables", "untyped_var"))}, nil, "strings\n"}, + {"var.untyped", []string{"console", `-var=untyped=just_a_string`, filepath.Join(testFixture("hcl", "variables", "untyped_var"))}, nil, "just_a_string\n"}, + {"var.untyped", []string{"console", filepath.Join(testFixture("hcl", "variables", "untyped_var", "var.pkr.hcl"))}, []string{"PKR_VAR_untyped=just_a_string"}, "just_a_string\n"}, } for _, tc := range tc { diff --git a/command/inspect_test.go b/command/inspect_test.go index 73d93b1c2..50cf31b06 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -69,13 +69,19 @@ local.fruit: "banana" `}, - {[]string{"inspect", "-var=fruit=peach", "-var=unknown_string=also_peach", `-var=unknown_unknown="peach_too"`, filepath.Join(testFixture("hcl"), "inspect", "fruit_string.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode + {[]string{"inspect", "-var=fruit=peach", + "-var=unknown_string=also_peach", + `-var=unknown_unknown=["peach_too"]`, + `-var=unknown_list_of_string=["first_peach", "second_peach"]`, + filepath.Join(testFixture("hcl"), "inspect", "fruit_string.pkr.hcl")}, nil, + `Packer Inspect: HCL2 mode > input-variables: var.fruit: "peach" +var.unknown_list_of_string: "[\n \"first_peach\",\n \"second_peach\",\n]" var.unknown_string: "also_peach" -var.unknown_unknown: "peach_too" +var.unknown_unknown: "[\"peach_too\"]" > local-variables: @@ -88,6 +94,7 @@ var.unknown_unknown: "peach_too" > input-variables: var.fruit: "peach" +var.unknown_list_of_string: "" var.unknown_string: "" var.unknown_unknown: "" diff --git a/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl b/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl index fb285b422..fb904c368 100644 --- a/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl +++ b/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl @@ -8,5 +8,10 @@ variable "unknown_string" { type = string } + +variable "unknown_list_of_string" { + type = list(string) +} + variable "unknown_unknown" { } diff --git a/command/test-fixtures/hcl/variables/list_of_string/var.pkr.hcl b/command/test-fixtures/hcl/variables/list_of_string/var.pkr.hcl new file mode 100644 index 000000000..eb1d199f3 --- /dev/null +++ b/command/test-fixtures/hcl/variables/list_of_string/var.pkr.hcl @@ -0,0 +1,4 @@ + +variable "list_of_string" { + type = list(string) +} diff --git a/command/test-fixtures/hcl/variables/untyped_var/settings.auto.pkrvars.hcl b/command/test-fixtures/hcl/variables/untyped_var/settings.auto.pkrvars.hcl new file mode 100644 index 000000000..48cf27fb1 --- /dev/null +++ b/command/test-fixtures/hcl/variables/untyped_var/settings.auto.pkrvars.hcl @@ -0,0 +1,2 @@ + +untyped = ["slice", "of", "strings"] diff --git a/command/test-fixtures/hcl/variables/untyped_var/var.pkr.hcl b/command/test-fixtures/hcl/variables/untyped_var/var.pkr.hcl new file mode 100644 index 000000000..9d69f7fb8 --- /dev/null +++ b/command/test-fixtures/hcl/variables/untyped_var/var.pkr.hcl @@ -0,0 +1,3 @@ + +variable "untyped" { +} diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index 11f8ba7dc..35818bcbe 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -426,7 +426,9 @@ func (cfg *PackerConfig) collectInputVariableValues(env []string, files []*hcl.F // The specified filename is to identify the source of where value originated from in the diagnostics report, if there is an error. func expressionFromVariableDefinition(filename string, value string, variableType cty.Type) (hclsyntax.Expression, hcl.Diagnostics) { switch variableType { - case cty.String, cty.Number: + case cty.String, cty.Number, cty.NilType: + // when the type is nil (not set in a variable block) we default to + // interpreting everything as a string literal. return &hclsyntax.LiteralValueExpr{Val: cty.StringVal(value)}, nil default: return hclsyntax.ParseExpression([]byte(value), filename, hcl.Pos{Line: 1, Column: 1}) diff --git a/website/pages/docs/from-1.5/variables.mdx b/website/pages/docs/from-1.5/variables.mdx index c83c28720..934f87447 100644 --- a/website/pages/docs/from-1.5/variables.mdx +++ b/website/pages/docs/from-1.5/variables.mdx @@ -132,6 +132,13 @@ Constraints](https://www.terraform.io/docs/configuration/types.html). If both the `type` and `default` arguments are specified, the given default value must be convertible to the specified type. +If only `default` is specified, the type of the default value will be used. + +When the `type` and `default` are both *not* specified and you try to set a +variable [from env vars](#environment-variables) or [from the command +line](#variables-on-the-command-line), the variable will always be interpreted +as a string. + ## Input Variable Documentation Because the input variables of a build are part of its user interface, you can