github.com/hashicorp/packer@v1.14.3/internal/hcp/registry/registry.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  // Package registry provides access to the HCP registry.
     5  package registry
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/hashicorp/hcl/v2"
    11  	sdkpacker "github.com/hashicorp/packer-plugin-sdk/packer"
    12  	"github.com/hashicorp/packer/hcl2template"
    13  	"github.com/hashicorp/packer/packer"
    14  )
    15  
    16  // Registry is an entity capable to orchestrate a Packer build and upload metadata to HCP
    17  type Registry interface {
    18  	PopulateVersion(context.Context) error
    19  	StartBuild(context.Context, *packer.CoreBuild) error
    20  	CompleteBuild(ctx context.Context, build *packer.CoreBuild, artifacts []sdkpacker.Artifact, buildErr error) ([]sdkpacker.Artifact, error)
    21  	VersionStatusSummary()
    22  	Metadata() Metadata
    23  }
    24  
    25  // New instantiates the appropriate registry for the Packer configuration template type.
    26  // A nullRegistry is returned for non-HCP Packer registry enabled templates.
    27  func New(cfg packer.Handler, ui sdkpacker.Ui) (Registry, hcl.Diagnostics) {
    28  	if !IsHCPEnabled(cfg) {
    29  		return &nullRegistry{}, nil
    30  	}
    31  
    32  	switch config := cfg.(type) {
    33  	case *hcl2template.PackerConfig:
    34  		// Maybe rename to what it represents....
    35  		return NewHCLRegistry(config, ui)
    36  	case *packer.Core:
    37  		return NewJSONRegistry(config, ui)
    38  	}
    39  
    40  	return nil, hcl.Diagnostics{
    41  		&hcl.Diagnostic{
    42  			Severity: hcl.DiagError,
    43  			Summary:  "Unknown Config type",
    44  			Detail: "The config type %s does not match a Packer-known template type. " +
    45  				"This is a Packer error and should be brought up to the Packer " +
    46  				"team via a GitHub Issue.",
    47  		},
    48  	}
    49  }