Skip to content

Commit

Permalink
Add component_type optional field (#11872)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
smaeda-ks authored Jul 14, 2022
1 parent 819cc50 commit 9f28ced
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
18 changes: 15 additions & 3 deletions datasource/hcp-packer-image/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions datasource/hcp-packer-image/data.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions website/content/docs/datasources/hcp/hcp-packer-image.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
@@ -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; -->

0 comments on commit 9f28ced

Please sign in to comment.