1+ /* Package image allows for the management of image metadata that can be stored in a HCP Packer registry.
2+ */
13package image
24
35import (
79 "github.com/hashicorp/packer-plugin-sdk/packer"
810)
911
10- // ArtifactStateURI represents the key used by Packer when querying an packersdk.Artifact
12+ // ArtifactStateURI represents the key used by Packer when querying a packersdk.Artifact
1113// for Image metadata that a particular component would like to have stored on the HCP Packer Registry.
1214const ArtifactStateURI = "par.artifact.metadata"
1315
@@ -19,20 +21,30 @@ type ArtifactOverrideFunc func(*Image) error
1921type Image struct {
2022 // ImageID is a unique reference identifier stored on the HCP Packer registry
2123 // that can be used to get back the built artifact of a builder or post-processor.
22- ImageID string
24+ ImageID string `mapstructure:"image_id"`
2325 // ProviderName represents the name of the top level cloud or service where the built artifact resides.
2426 // For example "aws, azure, docker, gcp, and vsphere".
27+ ProviderName string `mapstructure:"provider_name"`
2528 // ProviderRegion represents the location of the built artifact.
2629 // For cloud providers region usually maps to a cloud region or zone, but for things like the file builder,
2730 // S3 bucket or vsphere cluster region can represent a path on the upstream datastore, or cluster.
28- ProviderName , ProviderRegion string
31+ ProviderRegion string `mapstructure:"provider_region"`
2932 // Labels represents additional details about an image that a builder or post-processor may with to provide for a given build.
3033 // Any additional metadata will be made available as build labels within a HCP Packer registry iteration.
3134 Labels map [string ]string `mapstructure:"labels"`
3235}
3336
34- func New () * Image {
35- return & Image {}
37+ // Validate checks that the Image i contains a non-empty ImageID and ProviderName.
38+ func (i * Image ) Validate () error {
39+ if i .ImageID == "" {
40+ return errors .New ("error registry image does not contain a valid ImageId" )
41+ }
42+
43+ if i .ProviderName == "" {
44+ return errors .New ("error registry image does not contain a valid ProviderName" )
45+ }
46+
47+ return nil
3648}
3749
3850// FromMappedData calls f sequentially for each key and value present in mappedData to create a []*Image
@@ -63,29 +75,28 @@ func FromMappedData(mappedData interface{}, f func(key, value interface{}) (*Ima
6375}
6476
6577// FromArtifact returns an *Image that can be used by Packer core for publishing to the HCP Packer Registry.
66- // By default FromArtifact will use the a.BuilderID as the Image Provider , and the a.Id() as the ImageID that
67- // should be tracked within the HCP Packer Registry. No Region is selected by default as region varies per build .
78+ // By default FromArtifact will use the a.BuilderID() as the ProviderName , and the a.Id() as the ImageID that
79+ // should be tracked within the HCP Packer Registry. No Region is selected by default as region varies per builder .
6880// The use of one or more ArtifactOverrideFunc can be used to override any of the defaults used.
6981func FromArtifact (a packer.Artifact , opts ... ArtifactOverrideFunc ) (* Image , error ) {
7082 if a == nil {
7183 return nil , errors .New ("unable to create Image from nil artifact" )
7284 }
7385
74- img := & Image {
86+ img := Image {
7587 ProviderName : a .BuilderId (),
7688 ImageID : a .Id (),
7789 Labels : make (map [string ]string ),
7890 }
7991
80- // Let's grab some state data
8192 for _ , opt := range opts {
82- err := opt (img )
93+ err := opt (& img )
8394 if err != nil {
8495 return nil , err
8596 }
8697 }
8798
88- return img , nil
99+ return & img , nil
89100}
90101
91102// WithProvider takes a name, and returns a ArtifactOverrideFunc that can be
0 commit comments