packer/hcl2template
Wilken Rivera 57cbe4e203
core: Update validation options for undeclared variables (#12104)
* Update validation options for undeclared variables

In an effort to help users move from JSON to HCL2 templates the support for
variable definitions files are being updated to ignore undeclared
variable warnings on build execution. For legacy JSON templates builds no
warnings are displayed when var-files contain undeclared variables.

Since preferred mode HCL2 templates is to be explicit with variable
declarations - they must be declared to be used - validation for
undeclared variables still warns when running `packer validate`. A new
flag has been added to the validate command that can be used to disable
undeclared variable warnings.

* Update validation test for unused variables

Example Run
```
~>  go run . validate -no-warn-undeclared-var -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
The configuration is valid.

~>  go run . validate -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
Warning: Undefined variable

The variable "unused" was set but was not declared as an input variable.
To declare variable "unused" place this block in one of your .pkr.hcl
files,
such as variables.pkr.hcl

variable "unused" {
  type    = string
  default = null

}

The configuration is valid.

~>  go run . build -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
file.chocolate: output will be in this color.

Build 'file.chocolate' finished after 744 microseconds.

==> Wait completed after 798 microseconds

==> Builds finished. The artifacts of successful builds are:
--> file.chocolate: Stored file: chocolate.txt
```

* Rename Strict field to WarnOnUndeclaredVar

The field name Strict is a bit vague since it is only used for
checking against undeclared variables within a var-file definition.
To mitigate against potential overloading of this field it is
being renamed to be more explicit on its usage.

* command/build: Add warn-on-undeclared-var flag

Now that the default behaviour is to not display warnings for undeclared variables
an optional flag has been added to toggle the old behaviour.

```
~>  go run . build -warn-on-undeclared-var -var-file command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
Warning: Undefined variable

The variable "unused" was set but was not declared as an input variable.
To declare variable "unused" place this block in one of your .pkr.hcl files,
such as variables.pkr.hcl

variable "unused" {
  type    = string
  default = null

}

file.chocolate: output will be in this color.

Build 'file.chocolate' finished after 762 microseconds.

==> Wait completed after 799 microseconds

==> Builds finished. The artifacts of successful builds are:
--> file.chocolate: Stored file: chocolate.txt
```
2022-11-14 17:06:45 -05:00
..
addrs addrs: remove Plugin.ForDisplay func, the String one does the job 2021-02-15 15:32:42 +01:00
fixtures hcl2: fix crash on malformed overrides (#11881) 2022-07-20 14:30:49 -04:00
function hcl2_upgrade: special case: vsphere fix (#11216) 2021-08-25 14:20:08 +02:00
internal Use packer-sdc in packer + remove mapstructure-to-hcl2 & struct-markdown (#10913) 2021-04-16 11:52:03 +02:00
repl packer console for HCL2 (#9359) 2020-06-05 17:23:54 +02:00
shim Use packer-sdc in packer + remove mapstructure-to-hcl2 & struct-markdown (#10913) 2021-04-16 11:52:03 +02:00
testdata hcl2: report error on build without sources 2022-09-30 15:39:27 -04:00
common_test.go internal: reorganise registry/HCP code 2022-11-14 13:31:35 -05:00
components.go Implicit required_plugin blocks (#10732) 2021-03-24 11:31:39 +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
formatter.go fix: print unchanged formatted file when using stdin (#11047) 2021-06-03 14:02:42 +02:00
formatter_test.go un-remove tests 2021-03-15 12:00:19 +01:00
functions.go hcl2_upgrade: special case: vsphere fix (#11216) 2021-08-25 14:20:08 +02:00
parser.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
plugin.go Allow to have dynamic blocks in a build block + tests (#10825) 2021-03-30 15:53:04 +02:00
types.build.from.go build using HCL2 (#8423) 2019-12-17 11:25:56 +01:00
types.build.go hcl2: report error on build without sources 2022-09-30 15:39:27 -04:00
types.build.hcp_packer_registry.go internal: reorganise registry/HCP code 2022-11-14 13:31:35 -05:00
types.build.hcp_packer_registry_test.go test: use T.Setenv to set env vars in tests 2022-09-20 14:54:20 -04:00
types.build.post-processor.go Move to using ectx to preload build names (#11432) 2021-12-07 12:24:12 +01:00
types.build.provisioners.go hcl2: fix crash on malformed overrides (#11881) 2022-07-20 14:30:49 -04:00
types.build.provisioners_test.go hcl2: fix crash on malformed overrides (#11881) 2022-07-20 14:30:49 -04:00
types.build_test.go hcl2: report error on build without sources 2022-09-30 15:39:27 -04:00
types.datasource.go remove unnecessary complexity 2021-09-07 09:21:00 -07:00
types.datasource_test.go hcl2: report error on build without sources 2022-09-30 15:39:27 -04:00
types.hcl_post-processor.go Allow to use build variables in a post processor (#11323) 2021-10-11 10:17:34 -04:00
types.hcl_provisioner.go create util function in hcl2 template that will load config values into hcl values without panicing if it finds something it cannot handle 2021-01-25 12:25:41 -08: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 core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
types.packer_config_test.go add tests, and simplify code 2021-10-11 10:29:36 -07:00
types.required_plugins.go Implicit required_plugin blocks (#10732) 2021-03-24 11:31:39 +01:00
types.required_plugins_test.go Implicit required_plugin blocks (#10732) 2021-03-24 11:31:39 +01:00
types.source.go add tests, and simplify code 2021-10-11 10:29:36 -07:00
types.source_test.go hcl2: report error on build without sources 2022-09-30 15:39:27 -04:00
types.variables.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
types.variables_test.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
utils.go Simplify error message when config file can't be "stat'd" (#10763) 2021-03-15 14:07:07 +01:00
version.go regenerate 2021-01-15 11:59:36 -08:00
version_required.go refactor packer version out of hcltemplate code. 2020-11-09 12:29:53 -08:00