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

[HPR-837] Update validation options for undeclared variables #12104

Merged
merged 3 commits into from
Nov 14, 2022

Conversation

nywilken
Copy link
Contributor

@nywilken nywilken commented Nov 9, 2022

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 template builds no warnings are displayed when var-files contain undeclared variables. So the behavior is sort of the same.

Since the preferred mode for 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.

Closes: #12109
Related to: #9822

Example packer validate 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.

Example packer build runs

~>  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

~>  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

@nywilken nywilken requested a review from a team as a code owner November 9, 2022 23:00
@nywilken nywilken force-pushed the nywilken/unused-variables-warning branch from 92bf805 to 9f1dfc4 Compare November 10, 2022 02:00
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

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

Overall LGTM, I do have some concerns relative to the naming of both the option and in the code, but the code looks good to me.

command/validate.go Outdated Show resolved Hide resolved
hcl2template/types.variables.go Outdated Show resolved Hide resolved
command/cli.go Outdated Show resolved Hide resolved
"variable %[1]q {\n"+
" type = string\n"+
" type = string\n"+
" default = null\n"+
Copy link
Contributor

Choose a reason for hiding this comment

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

More a question than anything, is null a valid default value for a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this does work. We could set the default value to an empty string but that may not immediately imply to the user that the variable is optional. We have the use of default = null documented as the preferred way to make a value of any type optional. So I went with that.

I've considered adding a comment to the example block to indicate that both type and the default value should be changed to reflect the actual value in the var-file. But that might be too much info. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

No I think you're right, it may be a bit much, just stating they need to declare a variable block to make their template valid is good enough imo, that was just me asking a question out of curiosity, I didn't know that null was accepted for any type and was the preferred way to make a variable optional.
If that's documented and known, no issue in proposing null to be the default value, users can choose whatever else suits their need should they want to.
Besides it's just an example of what they need to add to their template, they'll have to change its contents 90% of the time because the default value is not required, different, or because the type is not string, so I don't think we need to worry too much about what's in there.

@nywilken nywilken force-pushed the nywilken/unused-variables-warning branch 2 times, most recently from cc5c185 to 988218c Compare November 11, 2022 15:11
@nywilken
Copy link
Contributor Author

Overall LGTM, I do have some concerns relative to the naming of both the option and in the code, but the code looks good to me.

I've re-rolled to account for the provided feedback. Feel free to review the updates when ready.

@nywilken nywilken added enhancement core Core components of Packer command/validate labels Nov 11, 2022
@nywilken nywilken added this to the 1.8.5 milestone Nov 11, 2022
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

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

Just a last thing I left a comment for, other than that, LGTM!

"variable %[1]q {\n"+
" type = string\n"+
" type = string\n"+
" default = null\n"+
Copy link
Contributor

Choose a reason for hiding this comment

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

No I think you're right, it may be a bit much, just stating they need to declare a variable block to make their template valid is good enough imo, that was just me asking a question out of curiosity, I didn't know that null was accepted for any type and was the preferred way to make a variable optional.
If that's documented and known, no issue in proposing null to be the default value, users can choose whatever else suits their need should they want to.
Besides it's just an example of what they need to add to their template, they'll have to change its contents 90% of the time because the default value is not required, different, or because the type is not string, so I don't think we need to worry too much about what's in there.

command/cli.go Outdated Show resolved Hide resolved
@nywilken nywilken force-pushed the nywilken/unused-variables-warning branch from 37a119c to ae73139 Compare November 14, 2022 17:06
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

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

LGTM

nywilken and others added 3 commits November 14, 2022 15:48
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
```
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.
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
```
@nywilken nywilken force-pushed the nywilken/unused-variables-warning branch from ae73139 to 7820bd2 Compare November 14, 2022 20:55
@nywilken nywilken merged commit 57cbe4e into main Nov 14, 2022
@nywilken nywilken deleted the nywilken/unused-variables-warning branch November 14, 2022 22:06
@nywilken nywilken changed the title Update validation options for undeclared variables [HPR-837] Update validation options for undeclared variables Nov 17, 2022
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hcl2: Provide an option for disable warnings on undeclared variables within a var-file
2 participants