Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component_type optional field #11872

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions datasource/hcp-packer-image/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/stable/2021-04-30/models"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand All @@ -33,6 +34,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 @@ -144,7 +148,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 @@ -163,6 +167,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 -->
JenGoldstrich marked this conversation as resolved.
Show resolved Hide resolved

- `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; -->