registry: add function to detect explicit HCP

The HCP_PACKER_REGISTRY environment variable had its behaviour changed
recently, as prior versions of Packer expected the attribute to be
forcefully set to a value that is neither "off" nor "0" in order to
get HCP integration to work, or that a "hcp_packer_registry" block was
defined in an HCL template. Now, this environment variable defaults to
not explicitely enable, but instead to explicitely disable HCP
integration, and the feature switch fall upon HCP_PACKER_BUCKET_NAME.

As an extra feature, we keep the prior behaviour alive when it is
explicitely defined as a value to enable it. That way we can report
errors if the rest is not defined, rather than silently ignore it.

This function we add to env is the first stone to enable this behaviour.
This commit is contained in:
Lucas Bajolet 2022-10-04 17:20:21 -04:00 committed by Lucas Bajolet
parent 3395d84fbc
commit ae15ed339c
2 changed files with 11 additions and 1 deletions

View file

@ -63,6 +63,10 @@ func setupRegistryForPackerConfig(pc *hcl2template.PackerConfig) hcl.Diagnostics
hasHCP = true
}
if env.IsHCPExplicitelyEnabled() {
hasHCP = true
}
if !hasHCP {
return nil
}
@ -270,7 +274,7 @@ func setupRegistryForPackerCore(cfg *CoreWrapper) hcl.Diagnostics {
return nil
}
if !env.HasPackerRegistryBucket() {
if !env.HasPackerRegistryBucket() && !env.IsHCPExplicitelyEnabled() {
return nil
}

View file

@ -44,3 +44,9 @@ func IsHCPDisabled() bool {
hcp, ok := os.LookupEnv(HCPPackerRegistry)
return ok && strings.ToLower(hcp) == "off" || hcp == "0"
}
// IsHCPExplicitelyEnabled returns true if the client enabled HCP_PACKER_REGISTRY explicitely, i.e. it is defined and not 0 or off
func IsHCPExplicitelyEnabled() bool {
hcp, ok := os.LookupEnv(HCPPackerRegistry)
return ok && strings.ToLower(hcp) != "off" && hcp != "0"
}