You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
packer/internal/hcp/registry/json.go

72 lines
1.7 KiB

package registry
import (
"context"
"path/filepath"
"github.com/hashicorp/hcl/v2"
sdkpacker "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer"
)
// JSONMetadataRegistry is a HCP handler made to process legacy JSON templates
type JSONMetadataRegistry struct {
configuration *packer.Core
bucket *Bucket
}
func NewJSONMetadataRegistry(config *packer.Core) (*JSONMetadataRegistry, hcl.Diagnostics) {
bucket, diags := createConfiguredBucket(
filepath.Dir(config.Template.Path),
withPackerEnvConfiguration,
)
if diags.HasErrors() {
return nil, diags
}
for _, b := range config.Template.Builders {
// Get all builds slated within config ignoring any only or exclude flags.
bucket.RegisterBuildForComponent(packer.HCPName(b))
}
return &JSONMetadataRegistry{
configuration: config,
bucket: bucket,
}, nil
}
// PopulateIteration creates the metadata on HCP for a build
func (h *JSONMetadataRegistry) PopulateIteration(ctx context.Context) error {
err := h.bucket.Validate()
if err != nil {
return err
}
err = h.bucket.Initialize(ctx)
if err != nil {
return err
}
err = h.bucket.populateIteration(ctx)
if err != nil {
return err
}
return nil
}
// StartBuild is invoked when one build for the configuration is starting to be processed
func (h *JSONMetadataRegistry) StartBuild(ctx context.Context, buildName string) error {
return h.bucket.startBuild(ctx, buildName)
}
// CompleteBuild is invoked when one build for the configuration has finished
func (h *JSONMetadataRegistry) CompleteBuild(
ctx context.Context,
buildName string,
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
}