diff --git a/hcl2template/testdata/hcp_par/empty_bucket.pkr.hcl b/hcl2template/testdata/hcp_par/empty_bucket.pkr.hcl new file mode 100644 index 000000000..e692a39bb --- /dev/null +++ b/hcl2template/testdata/hcp_par/empty_bucket.pkr.hcl @@ -0,0 +1,11 @@ +source "null" "test" { + communicator = "none" +} + +build { + name = "bucket-slug" + hcp_packer_registry { + } + + sources = ["null.test"] +} diff --git a/hcl2template/types.build.hcp_packer_registry.go b/hcl2template/types.build.hcp_packer_registry.go index ba8935b5d..640359aa1 100644 --- a/hcl2template/types.build.hcp_packer_registry.go +++ b/hcl2template/types.build.hcp_packer_registry.go @@ -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), diff --git a/hcl2template/types.build.hcp_packer_registry_test.go b/hcl2template/types.build.hcp_packer_registry_test.go index b43566d3e..54103f7cb 100644 --- a/hcl2template/types.build.hcp_packer_registry_test.go +++ b/hcl2template/types.build.hcp_packer_registry_test.go @@ -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},