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-838] cmd/hcl2_upgrade: Generate variable block for all referenced user input variables #12136

Merged
merged 1 commit into from
Nov 30, 2022

Conversation

nywilken
Copy link
Contributor

@nywilken nywilken commented Nov 29, 2022

Currently the HCL2 upgrade command generates a valid variable block for all variables within the JSON variables property.

However JSON templates in Packer support variable interpolation for input variables defined within a variable definition file without it being declared within the variables property. When upgrading a JSON template to HCL2 the user variable reference gets converted to var.<var_name>. The upgraded template ultimately fails on validation or build execution with undefined variable errors. In order to improve the user experience when upgrading a JSON template to HCL2 the upgrade command will now create a variable block for all input variables referenced within the build template to ensure all required variables been defined.

Todo

  • Update HCL2 upgrade command docs with an example of this behavior. (In a separate PR)

Example Runs

Current HCL2 upgrade command experience

~>  packer hcl2_upgrade command/test-fixtures/hcl2_upgrade/undeclared-variables/input.json
Successfully created command/test-fixtures/hcl2_upgrade/undeclared-variables/input.json.pkr.hcl. Exit 0

~>  packer validate -var-file=/tmp/var-file.json command/test-fixtures/hcl2_upgrade/undeclared-variables/input.json.pkr.hcl
Warning: Undefined variable

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

variable "communicator_type" {
  type = string
}


Error: Unsupported attribute

  on command/test-fixtures/hcl2_upgrade/undeclared-variables/input.json.pkr.hcl line 8:
  (source code not available)

This object does not have an attribute named "communicator_type".

Updated HCL2 upgrade command experience

~>  packer hcl2_upgrade command/test-fixtures/hcl2_upgrade/undeclared-variables/input.json
Successfully created command/test-fixtures/hcl2_upgrade/undeclared-variables/input.json.pkr.hcl. Exit 0

~>  packer validate -var-file=/tmp/var-file.json command/test-fixtures/hcl2_upgrade/undeclared-variables/input.json.pkr.hcl
The configuration is valid.

Sorry, something went wrong.

@nywilken nywilken requested a review from a team as a code owner November 29, 2022 21:44
…ut variables

Currently the HCL2 upgrade command generates a valid variable block for all variables
within the JSON variables property. However JSON templates in Packer support variable
interpolation for input variables defined within a variable definition file without
it being declared within the variables property. When upgrading a JSON template to HCL2
the user variable reference gets converted to var.<var_name>. The upgraded template ultimately
fails on validation or build execution with undefined variable errors, with this change the upgrade
command will now create a variable block for all input variables referenced within the build template
to ensure all required variables been defined.
@nywilken nywilken force-pushed the nywilken/hcl2_upgrade-undeclared-variables branch from 148ccf4 to c115bba Compare November 29, 2022 21:54
@nywilken nywilken changed the title cmd/hcl2_upgrade: Generate variable block for all referenced user input variables [HPR-838] cmd/hcl2_upgrade: Generate variable block for all referenced user input variables Nov 29, 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.

LGTM!

@nywilken nywilken added this to the 1.8.5 milestone Nov 30, 2022
@nywilken
Copy link
Contributor Author

I'm going to merge this change in and work on the documentation in a separate PR.

@nywilken nywilken merged commit dbeeab4 into main Nov 30, 2022
@nywilken nywilken deleted the nywilken/hcl2_upgrade-undeclared-variables branch November 30, 2022 20:25
@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 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants