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.
nywilken/hcp_all_errors_at_once
Lucas Bajolet 4 years ago committed by Wilken Rivera
parent 4bb6e52044
commit c983230548

@ -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
}

@ -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"
}

Loading…
Cancel
Save