Skip to content

Commit

Permalink
registry: don't use datasource output for ancestry
Browse files Browse the repository at this point in the history
The ancestry inferral code relied on HCP datasource outputs for deciding
what would be the ancestry for an image built with HCP.

This brings a dependency into the hcp package, which is not really
necessary as we only need a few information from those entities, hence
this commit removes the full dependency on the structures, in favour of
more focused data structures where we only cherry pick what is necessary
for this code.
  • Loading branch information
lbajolet-hashicorp committed Nov 14, 2022
1 parent 606e6c4 commit 719c868
Showing 1 changed file with 28 additions and 55 deletions.
83 changes: 28 additions & 55 deletions internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/hashicorp/hcl/v2"
sdkpacker "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/hcl2template"
hcppackerimagedatasource "github.com/hashicorp/packer/internal/hcp/datasource/hcp-packer-image"
hcppackeriterationdatasource "github.com/hashicorp/packer/internal/hcp/datasource/hcp-packer-iteration"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
)
Expand Down Expand Up @@ -123,69 +121,44 @@ func NewHCLMetadataRegistry(config *hcl2template.PackerConfig) (*HCLMetadataRegi
}, nil
}

func imageValueToDSOutput(imageVal map[string]cty.Value) hcppackerimagedatasource.DatasourceOutput {
dso := hcppackerimagedatasource.DatasourceOutput{}
type hcpImage struct {
ID string
ChannelID string
IterationID string
}

func imageValueToDSOutput(imageVal map[string]cty.Value) hcpImage {
image := hcpImage{}
for k, v := range imageVal {
switch k {
case "id":
dso.ID = v.AsString()
case "region":
dso.Region = v.AsString()
case "labels":
labels := map[string]string{}
lbls := v.AsValueMap()
for k, v := range lbls {
labels[k] = v.AsString()
}
dso.Labels = labels
case "packer_run_uuid":
dso.PackerRunUUID = v.AsString()
image.ID = v.AsString()
case "channel_id":
dso.ChannelID = v.AsString()
image.ChannelID = v.AsString()
case "iteration_id":
dso.IterationID = v.AsString()
case "build_id":
dso.BuildID = v.AsString()
case "created_at":
dso.CreatedAt = v.AsString()
case "component_type":
dso.ComponentType = v.AsString()
case "cloud_provider":
dso.CloudProvider = v.AsString()
image.IterationID = v.AsString()
}
}

return dso
return image
}

type hcpIteration struct {
ID string
ChannelID string
}

func iterValueToDSOutput(iterVal map[string]cty.Value) hcppackeriterationdatasource.DatasourceOutput {
dso := hcppackeriterationdatasource.DatasourceOutput{}
func iterValueToDSOutput(iterVal map[string]cty.Value) hcpIteration {
iter := hcpIteration{}
for k, v := range iterVal {
switch k {
case "author_id":
dso.AuthorID = v.AsString()
case "bucket_name":
dso.BucketName = v.AsString()
case "complete":
// For all intents and purposes, cty.Value.True() acts
// like a AsBool() would.
dso.Complete = v.True()
case "created_at":
dso.CreatedAt = v.AsString()
case "fingerprint":
dso.Fingerprint = v.AsString()
case "id":
dso.ID = v.AsString()
case "incremental_version":
// Maybe when cty provides a good way to AsInt() a cty.Value
// we can consider implementing this.
case "updated_at":
dso.UpdatedAt = v.AsString()
iter.ID = v.AsString()
case "channel_id":
dso.ChannelID = v.AsString()
iter.ChannelID = v.AsString()
}
}
return dso
return iter
}

func withDatasourceConfiguration(vals map[string]cty.Value) bucketConfigurationOpts {
Expand All @@ -199,7 +172,7 @@ func withDatasourceConfiguration(vals map[string]cty.Value) bucketConfigurationO
return nil
}

iterations := map[string]hcppackeriterationdatasource.DatasourceOutput{}
iterations := map[string]hcpIteration{}

var err error
if iterOK {
Expand All @@ -216,12 +189,12 @@ func withDatasourceConfiguration(vals map[string]cty.Value) bucketConfigurationO

for k, v := range hcpData {
iterVals := v.AsValueMap()
dso := iterValueToDSOutput(iterVals)
iterations[k] = dso
iter := iterValueToDSOutput(iterVals)
iterations[k] = iter
}
}

images := map[string]hcppackerimagedatasource.DatasourceOutput{}
images := map[string]hcpImage{}

if imageOK {
hcpData := map[string]cty.Value{}
Expand All @@ -237,8 +210,8 @@ func withDatasourceConfiguration(vals map[string]cty.Value) bucketConfigurationO

for k, v := range hcpData {
imageVals := v.AsValueMap()
dso := imageValueToDSOutput(imageVals)
images[k] = dso
img := imageValueToDSOutput(imageVals)
images[k] = img
}
}

Expand Down

0 comments on commit 719c868

Please sign in to comment.