Add `component_type` optional field (#11872)

* Add `component_type` optional field

to support specifying the exact build image when multiple images exist in the same provider and region for a given iteration

* update docs

* update docs
pull/11880/head
Shohei Maeda 4 years ago committed by GitHub
parent 819cc50f31
commit 9f28ced11c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -48,6 +48,9 @@ type Config struct {
CloudProvider string `mapstructure:"cloud_provider" required:"true"`
// The name of the cloud region your image is in. For example "us-east-1".
Region string `mapstructure:"region" required:"true"`
// The specific Packer builder used to create the image.
// For example, "amazon-ebs.example"
ComponentType string `mapstructure:"component_type" required:"false"`
// TODO: Version string `mapstructure:"version"`
// TODO: Fingerprint string `mapstructure:"fingerprint"`
// TODO: Label string `mapstructure:"label"`
@ -190,7 +193,7 @@ func (d *Datasource) Execute() (cty.Value, error) {
}
for _, image := range build.Images {
cloudAndRegions[build.CloudProvider] = append(cloudAndRegions[build.CloudProvider], image.Region)
if image.Region == d.config.Region {
if image.Region == d.config.Region && filterBuildByComponentType(build, d.config.ComponentType) {
// This is the desired image.
output = DatasourceOutput{
CloudProvider: build.CloudProvider,
@ -209,6 +212,15 @@ func (d *Datasource) Execute() (cty.Value, error) {
}
return cty.NullVal(cty.EmptyObject), fmt.Errorf("could not find a build result matching "+
"region (%q) and cloud provider (%q). Available: %v ",
d.config.Region, d.config.CloudProvider, cloudAndRegions)
"[region=%q, cloud_provider=%q, component_type=%q]. Available: %v ",
d.config.Region, d.config.CloudProvider, d.config.ComponentType, cloudAndRegions)
}
func filterBuildByComponentType(build *models.HashicorpCloudPackerBuild, componentType string) bool {
// optional field is not specified, passthrough
if componentType == "" {
return true
}
// if specified, only the matched image metadata is returned by this effect
return build.ComponentType == componentType
}

@ -23,6 +23,7 @@ type FlatConfig struct {
IterationID *string `mapstructure:"iteration_id" required:"true" cty:"iteration_id" hcl:"iteration_id"`
CloudProvider *string `mapstructure:"cloud_provider" required:"true" cty:"cloud_provider" hcl:"cloud_provider"`
Region *string `mapstructure:"region" required:"true" cty:"region" hcl:"region"`
ComponentType *string `mapstructure:"component_type" required:"false" cty:"component_type" hcl:"component_type"`
}
// FlatMapstructure returns a new FlatConfig.
@ -50,6 +51,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"iteration_id": &hcldec.AttrSpec{Name: "iteration_id", Type: cty.String, Required: false},
"cloud_provider": &hcldec.AttrSpec{Name: "cloud_provider", Type: cty.String, Required: false},
"region": &hcldec.AttrSpec{Name: "region", Type: cty.String, Required: false},
"component_type": &hcldec.AttrSpec{Name: "component_type", Type: cty.String, Required: false},
}
return s
}

@ -87,8 +87,11 @@ described.
@include 'datasource/hcp-packer-image/Config-required.mdx'
There are currently no optional fields for this datasource, though we intend
to add filtering fields in the future.
### Optional:
~> **Note:** This data source only returns the first found image's metadata filtered by the given options, from the returned list of images associated with the specified iteration. Therefore, if multiple images exist in the same region, it will only pick one of them. In this case, you can filter images by a source build name (Ex: `amazon-ebs.example`) using the `component_type` option.
@include 'datasource/hcp-packer-image/Config-not-required.mdx'
### Output Fields:

@ -0,0 +1,6 @@
<!-- Code generated from the comments of the Config struct in datasource/hcp-packer-image/data.go; DO NOT EDIT MANUALLY -->
- `component_type` (string) - The specific Packer builder used to create the image.
For example, "amazon-ebs.example"
<!-- End of code generated from the comments of the Config struct in datasource/hcp-packer-image/data.go; -->
Loading…
Cancel
Save