make HCL2 respect the new builder-returned generated vars list

This commit is contained in:
Megan Marsh 2019-12-17 09:42:15 -08:00
parent 50fb1148b4
commit 25711ed7d0
3 changed files with 31 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package hcl2template
import ( import (
"fmt" "fmt"
"github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
@ -31,7 +32,7 @@ func (p *Parser) decodeProvisioner(block *hcl.Block) (*ProvisionerBlock, hcl.Dia
return provisioner, diags return provisioner, diags
} }
func (p *Parser) StartProvisioner(pb *ProvisionerBlock) (packer.Provisioner, hcl.Diagnostics) { func (p *Parser) StartProvisioner(pb *ProvisionerBlock, generatedVars []string) (packer.Provisioner, hcl.Diagnostics) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
provisioner, err := p.ProvisionersSchemas.Start(pb.PType) provisioner, err := p.ProvisionersSchemas.Start(pb.PType)
@ -43,12 +44,30 @@ func (p *Parser) StartProvisioner(pb *ProvisionerBlock) (packer.Provisioner, hcl
}) })
return nil, diags return nil, diags
} }
flatProvisinerCfg, moreDiags := decodeHCL2Spec(pb.block, nil, provisioner) flatProvisionerCfg, moreDiags := decodeHCL2Spec(pb.block, nil, provisioner)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if diags.HasErrors() { if diags.HasErrors() {
return nil, diags return nil, diags
} }
err = provisioner.Prepare(flatProvisinerCfg) // manipulate generatedVars from builder to add to the interfaces being
// passed to the provisioner Prepare()
// If the builder has provided a list of to-be-generated variables that
// should be made accessible to provisioners, pass that list into
// the provisioner prepare() so that the provisioner can appropriately
// validate user input against what will become available. Otherwise,
// only pass the default variables, using the basic placeholder data.
generatedPlaceholderMap := packer.BasicPlaceholderData()
if generatedVars != nil {
for _, k := range generatedVars {
generatedPlaceholderMap[k] = fmt.Sprintf("Generated_%s. "+
common.PlaceholderMsg, k)
}
}
// configs := make([]interface{}, 2)
// configs = append(, flatProvisionerCfg)
// configs = append(configs, generatedPlaceholderMap)
err = provisioner.Prepare(flatProvisionerCfg, generatedPlaceholderMap)
if err != nil { if err != nil {
diags = append(diags, &hcl.Diagnostic{ diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError, Severity: hcl.DiagError,

View File

@ -14,11 +14,11 @@ type PackerConfig struct {
Builds Builds Builds Builds
} }
func (p *Parser) CoreBuildProvisioners(blocks []*ProvisionerBlock) ([]packer.CoreBuildProvisioner, hcl.Diagnostics) { func (p *Parser) CoreBuildProvisioners(blocks []*ProvisionerBlock, generatedVars []string) ([]packer.CoreBuildProvisioner, hcl.Diagnostics) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
res := []packer.CoreBuildProvisioner{} res := []packer.CoreBuildProvisioner{}
for _, pb := range blocks { for _, pb := range blocks {
provisioner, moreDiags := p.StartProvisioner(pb) provisioner, moreDiags := p.StartProvisioner(pb, generatedVars)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
continue continue
@ -63,12 +63,12 @@ func (p *Parser) getBuilds(cfg *PackerConfig) ([]packer.Build, hcl.Diagnostics)
}) })
continue continue
} }
builder, moreDiags := p.StartBuilder(src) builder, moreDiags, generatedVars := p.StartBuilder(src)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
continue continue
} }
provisioners, moreDiags := p.CoreBuildProvisioners(build.ProvisionerBlocks) provisioners, moreDiags := p.CoreBuildProvisioners(build.ProvisionerBlocks, generatedVars)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
continue continue

View File

@ -39,7 +39,7 @@ func (p *Parser) decodeSource(block *hcl.Block) (*Source, hcl.Diagnostics) {
return source, diags return source, diags
} }
func (p *Parser) StartBuilder(source *Source) (packer.Builder, hcl.Diagnostics) { func (p *Parser) StartBuilder(source *Source) (packer.Builder, hcl.Diagnostics, []string) {
var diags hcl.Diagnostics var diags hcl.Diagnostics
// calling BuilderSchemas will start a new builder plugin to ask about // calling BuilderSchemas will start a new builder plugin to ask about
@ -53,19 +53,19 @@ func (p *Parser) StartBuilder(source *Source) (packer.Builder, hcl.Diagnostics)
Detail: err.Error(), Detail: err.Error(),
Subject: &source.block.LabelRanges[0], Subject: &source.block.LabelRanges[0],
}) })
return builder, diags return builder, diags, nil
} }
decoded, moreDiags := decodeHCL2Spec(source.block, nil, builder) decoded, moreDiags := decodeHCL2Spec(source.block, nil, builder)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
if moreDiags.HasErrors() { if moreDiags.HasErrors() {
return nil, diags return nil, diags, nil
} }
_, warning, err := builder.Prepare(decoded) generatedVars, warning, err := builder.Prepare(decoded)
moreDiags = warningErrorsToDiags(source.block, warning, err) moreDiags = warningErrorsToDiags(source.block, warning, err)
diags = append(diags, moreDiags...) diags = append(diags, moreDiags...)
return builder, diags return builder, diags, generatedVars
} }
func (source *Source) Ref() SourceRef { func (source *Source) Ref() SourceRef {