hcl2template: locals evaluation returns a variable

Evaluating local variables used to be directly written to the
PackerConfig while each variable was created.

This was somewhat of an issue with testing, as we have a bunch of tests
that relied on `PackerConfig.Variables` being set only when we actually
write something.

This is not really a concern for normal use, just for testing, but to
limit the number of changes to the tests in hcl2template, I opted to
change how variables' values are retained, so that evaluating a single
variable returns a Variable in addition to hcl.Diagnostics, so we can
reify the approach and only create the map of variables if there's
something evaluated.
pull/13197/head
Lucas Bajolet 2 years ago committed by Lucas Bajolet
parent d28982c29a
commit 8d6b8da996

@ -464,7 +464,19 @@ func (cfg *PackerConfig) evaluateBuildPrereqs(skipDatasources bool) hcl.Diagnost
case *DatasourceBlock: case *DatasourceBlock:
diags = cfg.evaluateDatasource(*bl, skipDatasources) diags = cfg.evaluateDatasource(*bl, skipDatasources)
case *LocalBlock: case *LocalBlock:
diags = cfg.evaluateLocalVariable(bl) var val *Variable
if cfg.LocalVariables == nil {
cfg.LocalVariables = make(Variables)
}
val, diags = cfg.evaluateLocalVariable(bl)
// Note: clumsy a bit, but we won't add the variable as `nil` here
// unless no errors have been reported during evaluation.
//
// This prevents Packer from panicking down the line, as initialisation
// doesn't stop if there are diags, so if `val` is nil, it crashes.
if !diags.HasErrors() {
cfg.LocalVariables[bl.LocalName] = val
}
default: default:
diags = diags.Append(&hcl.Diagnostic{ diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError, Severity: hcl.DiagError,
@ -476,11 +488,11 @@ func (cfg *PackerConfig) evaluateBuildPrereqs(skipDatasources bool) hcl.Diagnost
}) })
} }
return nil if diags.HasErrors() {
} return diags
}
if cfg.LocalVariables == nil { return nil
cfg.LocalVariables = Variables{}
} }
for _, vtx := range graph.ReverseTopologicalOrder() { for _, vtx := range graph.ReverseTopologicalOrder() {

@ -114,6 +114,10 @@ func TestParse_datasource(t *testing.T) {
}: { }: {
Type: "null", Type: "null",
DSName: "baz", DSName: "baz",
Dependencies: []refString{
{"data", "null", "foo"},
{"data", "null", "bar"},
},
}, },
{ {
Type: "null", Type: "null",
@ -121,6 +125,9 @@ func TestParse_datasource(t *testing.T) {
}: { }: {
Type: "null", Type: "null",
DSName: "bang", DSName: "bang",
Dependencies: []refString{
{"data", "null", "baz"},
},
}, },
{ {
Type: "null", Type: "null",
@ -128,6 +135,9 @@ func TestParse_datasource(t *testing.T) {
}: { }: {
Type: "null", Type: "null",
DSName: "yummy", DSName: "yummy",
Dependencies: []refString{
{"data", "null", "bang"},
},
}, },
}, },
}, },
@ -219,6 +229,9 @@ func TestParse_datasource(t *testing.T) {
}: { }: {
Type: "null", Type: "null",
DSName: "gummy", DSName: "gummy",
Dependencies: []refString{
{"data", "null", "bear"},
},
}, },
{ {
Type: "null", Type: "null",
@ -226,6 +239,9 @@ func TestParse_datasource(t *testing.T) {
}: { }: {
Type: "null", Type: "null",
DSName: "bear", DSName: "bear",
Dependencies: []refString{
{"data", "null", "gummy"},
},
}, },
}, },
}, },

@ -341,10 +341,15 @@ func (c *PackerConfig) recursivelyEvaluateLocalVariable(local *LocalBlock, depth
diags = diags.Extend(localDiags) diags = diags.Extend(localDiags)
} }
return diags.Extend(c.evaluateLocalVariable(local)) val, locDiags := c.evaluateLocalVariable(local)
if !locDiags.HasErrors() {
c.LocalVariables[local.LocalName] = val
}
return diags.Extend(locDiags)
} }
func (cfg *PackerConfig) evaluateLocalVariable(local *LocalBlock) hcl.Diagnostics { func (cfg *PackerConfig) evaluateLocalVariable(local *LocalBlock) (*Variable, hcl.Diagnostics) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
value, moreDiags := local.Expr.Value(cfg.EvalContext(LocalContext, nil)) value, moreDiags := local.Expr.Value(cfg.EvalContext(LocalContext, nil))
@ -353,9 +358,9 @@ func (cfg *PackerConfig) evaluateLocalVariable(local *LocalBlock) hcl.Diagnostic
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
return diags return nil, diags
} }
cfg.LocalVariables[local.LocalName] = &Variable{ return &Variable{
Name: local.LocalName, Name: local.LocalName,
Sensitive: local.Sensitive, Sensitive: local.Sensitive,
Values: []VariableAssignment{{ Values: []VariableAssignment{{
@ -364,9 +369,7 @@ func (cfg *PackerConfig) evaluateLocalVariable(local *LocalBlock) hcl.Diagnostic
From: "default", From: "default",
}}, }},
Type: value.Type(), Type: value.Type(),
} }, diags
return diags
} }
func (cfg *PackerConfig) evaluateDatasources(skipExecution bool) hcl.Diagnostics { func (cfg *PackerConfig) evaluateDatasources(skipExecution bool) hcl.Diagnostics {

@ -396,7 +396,7 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{ &PackerConfig{
CorePackerVersionString: lockedVersion, CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"), Basedir: filepath.Join("testdata", "variables"),
LocalVariables: Variables{}, LocalVariables: nil,
}, },
true, true, true, true,
[]packersdk.Build{}, []packersdk.Build{},

Loading…
Cancel
Save