packer-cn/hcl2template
Wilken Rivera 9403b48a52
hcl2template/types.variables: Update logic for parsing literal value variables (#8834)
* hcl2template/types.variables: Update logic for parsing literal value variables

In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default
value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified.

The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the
CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :)

Build results before the change
```
⇶  PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' .
Error: Variables not allowed

  on <value for var.foo from arguments> line 1:
  (source code not available)

Variables may not be used here.

==> Builds finished but no artifacts were created.

```

Build results after the change
```
⇶  PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' .
null: output will be in this color.

==> null: Running local shell script: /tmp/packer-shell885249462
    null: two
    null: home
Build 'null' finished.

==> Builds finished. The artifacts of successful builds are:
--> null: Did not export anything. This is the null builder

⇶  ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' .
null: output will be in this color.

==> null: Running local shell script: /tmp/packer-shell123467506
    null: another variable
    null: home
Build 'null' finished.

==> Builds finished. The artifacts of successful builds are:
--> null: Did not export anything. This is the null builder

```

* tests/hcl2template/types.variables: Update test to use Bool

Turns out a string value won't actually complain if it's given a non
string looking value. It will just covert the value to a string literal
so using a type Bool which should fail if given anything that is not
true or false.

* tests/hcl2template/types.variables: Update unit tests

During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value.
This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly.

* Add a simple cty.Bool test case

* tests/hcl2template/types.variables:  Enable quoted_string test case

* Update hcl2template/types.variables.go

space

Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 15:12:26 +01:00
..
function Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
internal HCL2: add support for dynamic blocks, document for loops and splat expressions (#8720) 2020-02-20 10:51:34 +01:00
testdata Fix HCL2 local variables decoding to allow local usage within another local in the same locals block (#8755) 2020-02-21 12:12:30 +01:00
common_test.go Fix HCL2 local variables decoding to allow local usage within another local in the same locals block (#8755) 2020-02-21 12:12:30 +01:00
decode.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
docs.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
functions.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
parser.go Fix HCL2 local variables decoding to allow local usage within another local in the same locals block (#8755) 2020-02-21 12:12:30 +01:00
types.build.from.go build using HCL2 (#8423) 2019-12-17 11:25:56 +01:00
types.build.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
types.build.post-processor.go Avoid calling CoreBuild.Prepare(...) for HCL2 templates (#8742) 2020-02-14 17:39:32 +01:00
types.build.provisioners.go Avoid calling CoreBuild.Prepare(...) for HCL2 templates (#8742) 2020-02-14 17:39:32 +01:00
types.build_test.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
types.hcl_ref.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
types.packer_config.go Fix HCL2 local variables decoding to allow local usage within another local in the same locals block (#8755) 2020-02-21 12:12:30 +01:00
types.packer_config_test.go Fix HCL2 local variables decoding to allow local usage within another local in the same locals block (#8755) 2020-02-21 12:12:30 +01:00
types.source.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
types.source_test.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
types.variables.go hcl2template/types.variables: Update logic for parsing literal value variables (#8834) 2020-03-06 15:12:26 +01:00
types.variables_test.go hcl2template/types.variables: Update logic for parsing literal value variables (#8834) 2020-03-06 15:12:26 +01:00
utils.go Hcl2 input variables, local variables and functions (#8588) 2020-02-06 11:49:21 +01:00
zz_retrocompat.go add hcl2template pkg 2019-10-15 12:56:42 +02:00