From 3c213e6eaf0537993bf63fb4b3b24aac44c8a663 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 17 Feb 2020 18:05:15 +0100 Subject: [PATCH] continue and realise and unused undefaulted value triggers an error --- hcl2template/testdata/complete/build.pkr.hcl | 2 +- .../testdata/complete/variables.pkr.hcl | 4 -- hcl2template/types.packer_config_test.go | 21 +++++++--- hcl2template/types.variables.go | 15 ++++++-- hcl2template/types.variables_test.go | 38 ++++++++++++------- 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/hcl2template/testdata/complete/build.pkr.hcl b/hcl2template/testdata/complete/build.pkr.hcl index 6a01c01bd..64cf16c2e 100644 --- a/hcl2template/testdata/complete/build.pkr.hcl +++ b/hcl2template/testdata/complete/build.pkr.hcl @@ -8,7 +8,7 @@ build { provisioner "shell" { name = "provisioner that does something" not_squashed = var.foo - string = "string${var.proxmox_username}" + string = "string" int = "${41 + 1}" int64 = "${42 + 1}" bool = "true" diff --git a/hcl2template/testdata/complete/variables.pkr.hcl b/hcl2template/testdata/complete/variables.pkr.hcl index db0b68df5..d144cc1cf 100644 --- a/hcl2template/testdata/complete/variables.pkr.hcl +++ b/hcl2template/testdata/complete/variables.pkr.hcl @@ -10,10 +10,6 @@ variable "image_id" { default = "image-id-default" } -variable "proxmox_username" { - type = string -} - variable "port" { type = number default = 42 diff --git a/hcl2template/types.packer_config_test.go b/hcl2template/types.packer_config_test.go index f5019258f..76156ffa6 100644 --- a/hcl2template/types.packer_config_test.go +++ b/hcl2template/types.packer_config_test.go @@ -21,14 +21,23 @@ func TestParser_complete(t *testing.T) { &PackerConfig{ Basedir: "testdata/complete", InputVariables: Variables{ - "foo": &Variable{}, - "image_id": &Variable{}, - "port": &Variable{}, - "availability_zone_names": &Variable{}, - "proxmox_username": &Variable{}, + "foo": &Variable{ + Name: "foo", + }, + "image_id": &Variable{ + Name: "image_id", + }, + "port": &Variable{ + Name: "port", + }, + "availability_zone_names": &Variable{ + Name: "availability_zone_names", + }, }, LocalVariables: Variables{ - "feefoo": &Variable{}, + "feefoo": &Variable{ + Name: "feefoo", + }, }, Sources: map[SourceRef]*SourceBlock{ refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"}, diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index cb74ff602..29112cda7 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -31,6 +31,8 @@ type Variable struct { // declaration, the type of the default variable will be used. This will // allow to ensure that users set this variable correctly. Type cty.Type + // Common name of the variable + Name string // Description of the variable Description string // When Sensitive is set to true Packer will try it best to hide/obfuscate @@ -59,7 +61,7 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) { return cty.UnknownVal(v.Type), &hcl.Diagnostic{ Severity: hcl.DiagError, - Summary: "Unset variable", + Summary: fmt.Sprintf("Unset variable %q", v.Name), Detail: "A used variable must be set or have a default value; see " + "https://packer.io/docs/configuration/from-1.5/syntax.html for details.", Context: v.block.DefRange.Ptr(), @@ -74,7 +76,9 @@ func (variables Variables) Values() (map[string]cty.Value, hcl.Diagnostics) { for k, v := range variables { var diag *hcl.Diagnostic res[k], diag = v.Value() - diags = append(diags, diag) + if diag != nil { + diags = append(diags, diag) + } } return res, diags } @@ -107,8 +111,10 @@ func (variables *Variables) decodeConfigMap(block *hcl.Block, ectx *hcl.EvalCont continue } (*variables)[key] = &Variable{ + Name: key, DefaultValue: value, Type: value.Type(), + block: block, } } @@ -142,7 +148,10 @@ func (variables *Variables) decodeConfig(block *hcl.Block, ectx *hcl.EvalContext return diags } + name := block.Labels[0] + res := &Variable{ + Name: name, Description: b.Description, Sensitive: b.Sensitive, block: block, @@ -200,7 +209,7 @@ func (variables *Variables) decodeConfig(block *hcl.Block, ectx *hcl.EvalContext }) } - (*variables)[block.Labels[0]] = res + (*variables)[name] = res return diags } diff --git a/hcl2template/types.variables_test.go b/hcl2template/types.variables_test.go index c54c48978..687f8f5a4 100644 --- a/hcl2template/types.variables_test.go +++ b/hcl2template/types.variables_test.go @@ -23,15 +23,17 @@ func TestParse_variables(t *testing.T) { &PackerConfig{ Basedir: filepath.Join("testdata", "variables"), InputVariables: Variables{ - "image_name": &Variable{}, - "key": &Variable{}, - "my_secret": &Variable{}, - "image_id": &Variable{}, - "port": &Variable{}, + "image_name": &Variable{Name: "image_name"}, + "key": &Variable{Name: "key"}, + "my_secret": &Variable{Name: "my_secret"}, + "image_id": &Variable{Name: "image_id"}, + "port": &Variable{Name: "port"}, "availability_zone_names": &Variable{ + Name: "availability_zone_names", Description: fmt.Sprintln("Describing is awesome ;D"), }, "super_secret_password": &Variable{ + Name: "super_secret_password", Sensitive: true, Description: fmt.Sprintln("Handle with care plz"), }, @@ -51,7 +53,9 @@ func TestParse_variables(t *testing.T) { &PackerConfig{ Basedir: filepath.Join("testdata", "variables"), InputVariables: Variables{ - "boolean_value": &Variable{}, + "boolean_value": &Variable{ + Name: "boolean_value", + }, }, }, true, true, @@ -64,7 +68,9 @@ func TestParse_variables(t *testing.T) { &PackerConfig{ Basedir: filepath.Join("testdata", "variables"), InputVariables: Variables{ - "boolean_value": &Variable{}, + "boolean_value": &Variable{ + Name: "boolean_value", + }, }, }, true, true, @@ -77,23 +83,27 @@ func TestParse_variables(t *testing.T) { &PackerConfig{ Basedir: filepath.Join("testdata", "variables"), InputVariables: Variables{ - "broken_type": &Variable{}, + "broken_type": &Variable{ + Name: "broken_type", + }, }, }, true, true, []packer.Build{}, false, }, - {"invalid default type", + {"unknown key", defaultParser, parseTestArgs{"testdata/variables/unknown_key.pkr.hcl", nil}, &PackerConfig{ Basedir: filepath.Join("testdata", "variables"), InputVariables: Variables{ - "broken_type": &Variable{}, + "broken_type": &Variable{ + Name: "broken_type", + }, }, }, - true, false, + true, true, []packer.Build{}, false, }, @@ -103,7 +113,9 @@ func TestParse_variables(t *testing.T) { &PackerConfig{ Basedir: filepath.Join("testdata", "variables"), InputVariables: Variables{ - "foo": &Variable{}, + "foo": &Variable{ + Name: "foo", + }, }, Sources: map[SourceRef]*SourceBlock{ SourceRef{"null", "null-builder"}: &SourceBlock{ @@ -117,7 +129,7 @@ func TestParse_variables(t *testing.T) { }, }, }, - false, false, + true, true, []packer.Build{}, true, },