upgrade variables with other variables

This commit is contained in:
sylviamoss 2021-02-22 17:14:01 +01:00
parent 60017822e0
commit a1a5cf0113
5 changed files with 71 additions and 25 deletions

View File

@ -436,6 +436,10 @@ func variableTransposeTemplatingCalls(s []byte) (isLocal bool, body []byte) {
isLocal = true
return ""
}
funcMap["user"] = func(a ...string) string {
isLocal = true
return ""
}
tpl, err := texttemplate.New("hcl2_upgrade").
Funcs(funcMap).

View File

@ -27,6 +27,7 @@ func Test_hcl2_upgrade(t *testing.T) {
{folder: "error-cleanup-provisioner", flags: []string{"-with-annotations"}},
{folder: "aws-access-config", flags: []string{}},
{folder: "variables-only", flags: []string{}},
{folder: "variables-with-variables", flags: []string{}},
}
for _, tc := range tc {

View File

@ -0,0 +1,25 @@
variable "aws_access_key" {
type = string
default = ""
sensitive = true
}
variable "aws_region" {
type = string
}
variable "aws_secret_key" {
type = string
default = ""
sensitive = true
}
local "password" {
sensitive = true
expression = "${var.aws_secret_key}-${var.aws_access_key}"
}
locals {
aws_secondary_region = "${var.aws_region}"
}

View File

@ -0,0 +1,14 @@
{
"variables": {
"aws_region": null,
"aws_secondary_region": "{{ user `aws_region` }}",
"aws_secret_key": "",
"aws_access_key": "",
"password": "{{ user `aws_secret_key` }}-{{ user `aws_access_key` }}"
},
"sensitive-variables": [
"aws_secret_key",
"aws_access_key",
"password"
]
}

View File

@ -26,31 +26,6 @@ $ packer hcl2_upgrade my-template.json
Successfully created my-template.json.pkr.hcl
```
## User variables using other user variables
Packer JSON recently started allowing using user variables from variables. In
HCL2, input variables cannot use functions nor other variables and are
virtually static, local variables must be used instead to craft more dynamic
variables. For that reason `hcl2_upgrade` cannot decide for you what local
variables to create and the `hcl2_upgrade` command will simply output all seen
variables as an input variable, it is now up to you to create a local variable.
Here is an example of a local variable using a string input variables:
```hcl
variable "foo" {
default = "Hello,"
}
variable "bar" {
default = "World!"
}
locals {
baz = "${var.foo} ${var.bar}"
}
```
## Upgrading variables file
From **v1.7.1**, the `hcl2_upgrade` command can upgrade a variables file.
@ -130,3 +105,30 @@ working on improving this part of the transformer.
- `-output-file` - File where to put the hcl2 generated config. Defaults to
JSON_TEMPLATE.pkr.hcl
- `-with-annotations` - Adds helper annotations with information about the generated HCL2 blocks.
## User variables using other user variables
Packer JSON recently started allowing using user variables from variables. In
HCL2, input variables cannot use functions nor other variables and are
virtually static, local variables must be used instead to craft more dynamic
variables.
For v1.7.0 and lower, `hcl2_upgrade` doesn't upgrade variables to local variables,
and it is up to you to upgrade them manually. Upgrade to **v1.7.1** to let the command do it
automatically for you.
Here is an example of a local variable using a string input variables:
```hcl
variable "foo" {
default = "Hello,"
}
variable "bar" {
default = "World!"
}
locals {
baz = "${var.foo} ${var.bar}"
}
```