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