diff --git a/command/hcl2_upgrade.go b/command/hcl2_upgrade.go index 2923d91fa..73b03284f 100644 --- a/command/hcl2_upgrade.go +++ b/command/hcl2_upgrade.go @@ -896,6 +896,10 @@ type BuildParser struct { } func (p *BuildParser) Parse(tpl *template.Template) error { + if len(p.Builders) == 0 { + return nil + } + buildContent := hclwrite.NewEmptyFile() buildBody := buildContent.Body() if tpl.Description != "" { diff --git a/command/hcl2_upgrade_test.go b/command/hcl2_upgrade_test.go index 9368a5f80..ac1ec7071 100644 --- a/command/hcl2_upgrade_test.go +++ b/command/hcl2_upgrade_test.go @@ -26,6 +26,7 @@ func Test_hcl2_upgrade(t *testing.T) { {folder: "source-name", flags: []string{"-with-annotations"}}, {folder: "error-cleanup-provisioner", flags: []string{"-with-annotations"}}, {folder: "aws-access-config", flags: []string{}}, + {folder: "variables-only", flags: []string{}}, } for _, tc := range tc { diff --git a/command/test-fixtures/hcl2_upgrade/variables-only/expected.pkr.hcl b/command/test-fixtures/hcl2_upgrade/variables-only/expected.pkr.hcl new file mode 100644 index 000000000..2b612eb57 --- /dev/null +++ b/command/test-fixtures/hcl2_upgrade/variables-only/expected.pkr.hcl @@ -0,0 +1,45 @@ + +variable "aws_access_key" { + type = string + default = "" + sensitive = true +} + +variable "aws_region" { + type = string +} + +variable "aws_secondary_region" { + type = string + default = "${env("AWS_DEFAULT_REGION")}" +} + +variable "aws_secret_key" { + type = string + default = "" + sensitive = true +} + +variable "secret_account" { + type = string + default = "🤷" + sensitive = true +} + +data "amazon-secretsmanager" "autogenerated_1" { + name = "sample/app/password" +} + +data "amazon-secretsmanager" "autogenerated_2" { + key = "api_key" + name = "sample/app/passwords" +} + +local "password" { + sensitive = true + expression = "${data.amazon-secretsmanager.autogenerated_1.value}" +} + +locals { + password_key = "MY_KEY_${data.amazon-secretsmanager.autogenerated_2.value}" +} diff --git a/command/test-fixtures/hcl2_upgrade/variables-only/input.json b/command/test-fixtures/hcl2_upgrade/variables-only/input.json new file mode 100644 index 000000000..27cefb911 --- /dev/null +++ b/command/test-fixtures/hcl2_upgrade/variables-only/input.json @@ -0,0 +1,18 @@ +{ + "variables": { + "secret_account": "🤷", + "aws_region": null, + "aws_secondary_region": "{{ env `AWS_DEFAULT_REGION` }}", + "aws_secret_key": "", + "aws_access_key": "", + "password": "{{ aws_secretsmanager `sample/app/password` }}", + "password_key": "MY_KEY_{{ aws_secretsmanager `sample/app/passwords` `api_key` }}" + }, + "sensitive-variables": [ + "aws_secret_key", + "aws_access_key", + "secret_account", + "potato", + "password" + ] +} \ No newline at end of file