From 86c8902b819c1da6cbcee530b68f8a651981fb34 Mon Sep 17 00:00:00 2001 From: Martin Grogan Date: Tue, 11 Feb 2025 12:20:00 -0500 Subject: [PATCH] hcl: add packer_registry at root schema --- hcl2template/parser.go | 36 +++++++++++++++++++++-------- hcl2template/types.packer_config.go | 3 +++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/hcl2template/parser.go b/hcl2template/parser.go index d6ae4ecb9..d63a64ed7 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -20,15 +20,16 @@ import ( ) const ( - packerLabel = "packer" - sourceLabel = "source" - variablesLabel = "variables" - variableLabel = "variable" - localsLabel = "locals" - localLabel = "local" - dataSourceLabel = "data" - buildLabel = "build" - communicatorLabel = "communicator" + packerLabel = "packer" + sourceLabel = "source" + variablesLabel = "variables" + variableLabel = "variable" + localsLabel = "locals" + localLabel = "local" + dataSourceLabel = "data" + buildLabel = "build" + hcpPackerRegistryLabel = "hcp_packer_registry" + communicatorLabel = "communicator" ) var configSchema = &hcl.BodySchema{ @@ -41,6 +42,7 @@ var configSchema = &hcl.BodySchema{ {Type: localLabel, LabelNames: []string{"name"}}, {Type: dataSourceLabel, LabelNames: []string{"type", "name"}}, {Type: buildLabel}, + {Type: hcpPackerRegistryLabel}, {Type: communicatorLabel, LabelNames: []string{"type", "name"}}, }, } @@ -549,6 +551,22 @@ func (p *Parser) parseConfig(f *hcl.File, cfg *PackerConfig) hcl.Diagnostics { for _, block := range content.Blocks { switch block.Type { + + case buildHCPPackerRegistryLabel: + if cfg.HCPPackerRegistry != nil { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: fmt.Sprintf("Only one " + buildHCPPackerRegistryLabel + " is allowed"), + Subject: block.DefRange.Ptr(), + }) + continue + } + hcpPackerRegistry, moreDiags := p.decodeHCPRegistry(block, cfg) + diags = append(diags, moreDiags...) + if moreDiags.HasErrors() { + continue + } + cfg.HCPPackerRegistry = hcpPackerRegistry case sourceLabel: source, moreDiags := p.decodeSource(block) diags = append(diags, moreDiags...) diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index 6bcc047d1..1b89256f5 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -54,6 +54,9 @@ type PackerConfig struct { // Builds is the list of Build blocks defined in the config files. Builds Builds + // HCPPackerRegistry contains the configuration for publishing the artifacts to the HCP Packer Registry. + HCPPackerRegistry *HCPPackerRegistryBlock + // HCPVars is the list of HCP-set variables for use later in a template HCPVars map[string]cty.Value