Skip to content

Commit

Permalink
hcl2template: don't error on empty bucket slug
Browse files Browse the repository at this point in the history
When a user defines a `hcp_packer_registry` block in their `build`
without a `bucket_name`, but they define it in their environment, Packer
should not report the bucket_name being wrong.
  • Loading branch information
lbajolet-hashicorp committed Dec 12, 2024
1 parent 5fa87f1 commit ac899c4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
11 changes: 11 additions & 0 deletions hcl2template/testdata/hcp_par/empty_bucket.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source "null" "test" {
communicator = "none"
}

build {
name = "bucket-slug"
hcp_packer_registry {
}

sources = ["null.test"]
}
7 changes: 6 additions & 1 deletion hcl2template/types.build.hcp_packer_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ func (p *Parser) decodeHCPRegistry(block *hcl.Block, cfg *PackerConfig) (*HCPPac
return nil, diags
}

if !bucketNameRegexp.MatchString(b.Slug) {
// No need to check the bucket name here if it's empty, since it can
// be set through the `HCP_PACKER_BUCKET_NAME` environment var.
//
// If both are unset, creating the build on HCP Packer will fail, and
// so will the packer build command.
if b.Slug != "" && !bucketNameRegexp.MatchString(b.Slug) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("%s.bucket_name can only contain between 3 and 36 ASCII letters, numbers and hyphens", buildHCPPackerRegistryLabel),
Expand Down
41 changes: 41 additions & 0 deletions hcl2template/types.build.hcp_packer_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,47 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) {
defaultParser := getBasicParser()

tests := []parseTest{
{"bucket_name left empty",
defaultParser,
parseTestArgs{"testdata/hcp_par/empty_bucket.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "hcp_par"),
Sources: map[SourceRef]SourceBlock{
refNull: {
Type: "null",
Name: "test",
block: &hcl.Block{
Type: "source",
},
},
},
Builds: Builds{
{
Name: "bucket-slug",
HCPPackerRegistry: &HCPPackerRegistryBlock{Slug: ""},
Sources: []SourceUseBlock{
{
SourceRef: refNull,
},
},
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
BuildName: "bucket-slug",
Type: "null.test",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Prepared: true,
BuilderType: "null",
},
},
false,
},
{"bucket_name as variable",
defaultParser,
parseTestArgs{"testdata/hcp_par/variable-for-bucket_name.pkr.hcl", nil, nil},
Expand Down

0 comments on commit ac899c4

Please sign in to comment.