|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/hashicorp/packer-plugin-sdk/packer" |
| 8 | +) |
| 9 | + |
| 10 | +// ArtifactStateURI represents the key used by Packer when querying an packersdk.Artifact |
| 11 | +// for Image metadata that a particular component would like to have stored on the HCP Packer Registry. |
| 12 | +const ArtifactStateURI = "par.artifact.metadata" |
| 13 | + |
| 14 | +// ArtifactOverrideFunc represents a transformation func that can be applied to a |
| 15 | +// non-nil *Image. See WithID, WithRegion functions for examples. |
| 16 | +type ArtifactOverrideFunc func(*Image) error |
| 17 | + |
| 18 | +// Image represents the metadata for some Artifact in the HCP Packer Registry. |
| 19 | +type Image struct { |
| 20 | + // ImageID is a unique reference identifier stored on the HCP Packer registry |
| 21 | + // that can be used to get back the built artifact of a builder or post-processor. |
| 22 | + ImageID string |
| 23 | + // ProviderName represents the name of the top level cloud or service where the built artifact resides. |
| 24 | + // For example "aws, azure, docker, gcp, and vsphere". |
| 25 | + // ProviderRegion represents the location of the built artifact. |
| 26 | + // For cloud providers region usually maps to a cloud region or zone, but for things like the file builder, |
| 27 | + // S3 bucket or vsphere cluster region can represent a path on the upstream datastore, or cluster. |
| 28 | + ProviderName, ProviderRegion string |
| 29 | + // Labels represents additional details about an image that a builder or post-processor may with to provide for a given build. |
| 30 | + // Any additional metadata will be made available as build labels within a HCP Packer registry iteration. |
| 31 | + Labels map[string]string `mapstructure:"labels"` |
| 32 | +} |
| 33 | + |
| 34 | +func New() *Image { |
| 35 | + return &Image{} |
| 36 | +} |
| 37 | + |
| 38 | +// FromMappedData calls f sequentially for each key and value present in mappedData to create a []*Image |
| 39 | +// as the final return value. If there is an error in calling f, FromMappedData will stop processing mapped items |
| 40 | +// and result in a nil slice, with the said error. |
| 41 | +// |
| 42 | +// FromMappedData will make its best attempt to convert the input map into map[interface{}]interface{} before |
| 43 | +// calling f(k,v). The func f is responsible for type asserting the expected type for the key and value before |
| 44 | +// trying to create an Image from it. |
| 45 | +func FromMappedData(mappedData interface{}, f func(key, value interface{}) (*Image, error)) ([]*Image, error) { |
| 46 | + |
| 47 | + mapValue := reflect.ValueOf(mappedData) |
| 48 | + if mapValue.Kind() != reflect.Map { |
| 49 | + return nil, errors.New("error the incoming mappedData does not appear to be a map; found type to be" + mapValue.Kind().String()) |
| 50 | + } |
| 51 | + |
| 52 | + keys := mapValue.MapKeys() |
| 53 | + var images []*Image |
| 54 | + for _, k := range keys { |
| 55 | + v := mapValue.MapIndex(k) |
| 56 | + i, err := f(k.Interface(), v.Interface()) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + images = append(images, i) |
| 61 | + } |
| 62 | + return images, nil |
| 63 | +} |
| 64 | + |
| 65 | +// 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. |
| 68 | +// The use of one or more ArtifactOverrideFunc can be used to override any of the defaults used. |
| 69 | +func FromArtifact(a packer.Artifact, opts ...ArtifactOverrideFunc) (*Image, error) { |
| 70 | + if a == nil { |
| 71 | + return nil, errors.New("unable to create Image from nil artifact") |
| 72 | + } |
| 73 | + |
| 74 | + img := &Image{ |
| 75 | + ProviderName: a.BuilderId(), |
| 76 | + ImageID: a.Id(), |
| 77 | + Labels: make(map[string]string), |
| 78 | + } |
| 79 | + |
| 80 | + // Let's grab some state data |
| 81 | + for _, opt := range opts { |
| 82 | + err := opt(img) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return img, nil |
| 89 | +} |
| 90 | + |
| 91 | +// WithProvider takes a name, and returns a ArtifactOverrideFunc that can be |
| 92 | +// used to override the ProviderName for an existing Image. |
| 93 | +func WithProvider(name string) func(*Image) error { |
| 94 | + return func(img *Image) error { |
| 95 | + if img == nil { |
| 96 | + return errors.New("no go on empty image") |
| 97 | + } |
| 98 | + |
| 99 | + img.ProviderName = name |
| 100 | + return nil |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// WithID takes a id, and returns a ArtifactOverrideFunc that can be |
| 105 | +// used to override the ImageId for an existing Image. |
| 106 | +func WithID(id string) func(*Image) error { |
| 107 | + return func(img *Image) error { |
| 108 | + if img == nil { |
| 109 | + return errors.New("no go on empty image") |
| 110 | + } |
| 111 | + |
| 112 | + img.ImageID = id |
| 113 | + return nil |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// WithRegion takes a region, and returns a ArtifactOverrideFunc that can be |
| 118 | +// used to override the ProviderRegion for an existing Image. |
| 119 | +func WithRegion(region string) func(*Image) error { |
| 120 | + return func(img *Image) error { |
| 121 | + if img == nil { |
| 122 | + return errors.New("no go on empty image") |
| 123 | + } |
| 124 | + |
| 125 | + img.ProviderRegion = region |
| 126 | + return nil |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// SetLabels takes metadata, and returns a ArtifactOverrideFunc that can be |
| 131 | +// used to set metadata for an existing Image. The incoming metadata `md` |
| 132 | +// will be filtered only for keys whose values are of type string. |
| 133 | +// If you wish to override this behavior you may create your own ArtifactOverrideFunc |
| 134 | +// for manipulating and setting Image metadata. |
| 135 | +func SetLabels(md map[string]interface{}) func(*Image) error { |
| 136 | + return func(img *Image) error { |
| 137 | + for k, v := range md { |
| 138 | + v, ok := v.(string) |
| 139 | + if !ok { |
| 140 | + continue |
| 141 | + } |
| 142 | + img.Labels[k] = v |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | + } |
| 147 | +} |
0 commit comments