From 9f28ced11c740c5261d16bed63ce49d27b42f0c1 Mon Sep 17 00:00:00 2001 From: Shohei Maeda <11495867+smaeda-ks@users.noreply.github.com> Date: Fri, 15 Jul 2022 02:33:10 +0900 Subject: [PATCH] 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 --- datasource/hcp-packer-image/data.go | 18 +++++++++++++++--- datasource/hcp-packer-image/data.hcl2spec.go | 2 ++ .../docs/datasources/hcp/hcp-packer-image.mdx | 7 +++++-- .../hcp-packer-image/Config-not-required.mdx | 6 ++++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 website/content/partials/datasource/hcp-packer-image/Config-not-required.mdx diff --git a/datasource/hcp-packer-image/data.go b/datasource/hcp-packer-image/data.go index 36ca8b5eaa8..7e36fca3405 100644 --- a/datasource/hcp-packer-image/data.go +++ b/datasource/hcp-packer-image/data.go @@ -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 } diff --git a/datasource/hcp-packer-image/data.hcl2spec.go b/datasource/hcp-packer-image/data.hcl2spec.go index 3e7f6fa89f9..8cfe1f62070 100644 --- a/datasource/hcp-packer-image/data.hcl2spec.go +++ b/datasource/hcp-packer-image/data.hcl2spec.go @@ -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 } diff --git a/website/content/docs/datasources/hcp/hcp-packer-image.mdx b/website/content/docs/datasources/hcp/hcp-packer-image.mdx index 86b36ed9c61..de5e60100b6 100644 --- a/website/content/docs/datasources/hcp/hcp-packer-image.mdx +++ b/website/content/docs/datasources/hcp/hcp-packer-image.mdx @@ -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: diff --git a/website/content/partials/datasource/hcp-packer-image/Config-not-required.mdx b/website/content/partials/datasource/hcp-packer-image/Config-not-required.mdx new file mode 100644 index 00000000000..70e7cd0d00d --- /dev/null +++ b/website/content/partials/datasource/hcp-packer-image/Config-not-required.mdx @@ -0,0 +1,6 @@ + + +- `component_type` (string) - The specific Packer builder used to create the image. + For example, "amazon-ebs.example" + +