mirror of https://github.com/hashicorp/packer
Add two new data sources -- hcp-packer-iteration and hcp-packer-image. These data sources together will allow users to query hcp_packer for the image_ids they need to use as source images to their builds, with a simple UI and clear outputs.
add quick and dirty acceptance test for hcp packer image iteration and hcp packer image data sources PR review lintingpull/11247/head
parent
27d89ac8d4
commit
fd80f8da8c
@ -0,0 +1,147 @@
|
||||
//go:generate packer-sdc struct-markdown
|
||||
//go:generate packer-sdc mapstructure-to-hcl2 -type DatasourceOutput,Config
|
||||
package hcp_packer_image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
"github.com/hashicorp/packer-plugin-sdk/common"
|
||||
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
|
||||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
||||
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
||||
packerregistry "github.com/hashicorp/packer/internal/packer_registry"
|
||||
)
|
||||
|
||||
type Datasource struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
// The name of the bucket your image is in.
|
||||
Bucket string `mapstructure:"bucket_name" required:"true"`
|
||||
// The name of the iteration Id to use when retrieving your image
|
||||
IterationID string `mapstructure:"iteration_id" required:"true"`
|
||||
// The name of the cloud provider that your image is for. For example,
|
||||
// "aws" or "gce".
|
||||
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"`
|
||||
// TODO: Version string `mapstructure:"version"`
|
||||
// TODO: Fingerprint string `mapstructure:"fingerprint"`
|
||||
// TODO: Label string `mapstructure:"label"`
|
||||
}
|
||||
|
||||
func (d *Datasource) ConfigSpec() hcldec.ObjectSpec {
|
||||
return d.config.FlatMapstructure().HCL2Spec()
|
||||
}
|
||||
|
||||
func (d *Datasource) Configure(raws ...interface{}) error {
|
||||
err := config.Decode(&d.config, nil, raws...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var errs *packersdk.MultiError
|
||||
|
||||
if d.config.Bucket == "" {
|
||||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("The `bucket_name` must be specified"))
|
||||
}
|
||||
if d.config.IterationID == "" {
|
||||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("The `iteration_id`"+
|
||||
" must be specified. If you do not know your iteration_id, you "+
|
||||
"can retrieve it using the bucket name and desired channel using"+
|
||||
" the hcp-packer-iteration data source."))
|
||||
}
|
||||
if d.config.Region == "" {
|
||||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("`region` is "+
|
||||
"currently a required field."))
|
||||
}
|
||||
if d.config.CloudProvider == "" {
|
||||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("`cloud_provider` is "+
|
||||
"currently a required field."))
|
||||
}
|
||||
|
||||
if errs != nil && len(errs.Errors) > 0 {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Information from []*models.HashicorpCloudPackerImage with some information
|
||||
// from the parent []*models.HashicorpCloudPackerBuild included where it seemed
|
||||
// like it might be relevant. Need to copy so we can generate
|
||||
type DatasourceOutput struct {
|
||||
// The name of the cloud provider that the image exists in. For example,
|
||||
// "aws", "azure", or "gce".
|
||||
CloudProvider string `mapstructure:"cloud_provider"`
|
||||
// The specific Packer builder or post-processor used to create the image.
|
||||
ComponentType string `mapstructure:"component_type"`
|
||||
// The date and time at which the image was created.
|
||||
CreatedAt string `mapstructure:"created_at"`
|
||||
// The id of the build that created the image. This is a ULID, which is a
|
||||
// unique identifier similar to a UUID. It is created by the HCP Packer
|
||||
// Registry when an build is first created, and is unique to this build.
|
||||
BuildID string `mapstructure:"build_id"`
|
||||
// The iteration id. This is a ULID, which is a unique identifier similar
|
||||
// to a UUID. It is created by the HCP Packer Registry when an iteration is
|
||||
// first created, and is unique to this iteration.
|
||||
IterationID string `mapstructure:"iteration_id"`
|
||||
// The UUID associated with the Packer run that created this image.
|
||||
PackerRunUUID string `mapstructure:"packer_run_uuid"`
|
||||
// ID or URL of the remote cloud image as given by a build.
|
||||
ID string `mapstructure:"id"`
|
||||
// The cloud region as given by `packer build`. eg. "ap-east-1".
|
||||
// For locally managed clouds, this may map instead to a cluster, server
|
||||
// or datastore.
|
||||
Region string `mapstructure:"region"`
|
||||
}
|
||||
|
||||
func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
|
||||
return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec()
|
||||
}
|
||||
|
||||
func (d *Datasource) Execute() (cty.Value, error) {
|
||||
cli, err := packerregistry.NewClient()
|
||||
if err != nil {
|
||||
return cty.NullVal(cty.EmptyObject), err
|
||||
}
|
||||
// Load channel.
|
||||
log.Printf("[INFO] Reading info from HCP Packer registry (%s) [project_id=%s, organization_id=%s, iteration_id=%s]",
|
||||
d.config.Bucket, cli.ProjectID, cli.OrganizationID, d.config.IterationID)
|
||||
|
||||
iteration, err := packerregistry.GetIterationFromId(context.TODO(), cli, d.config.Bucket, d.config.IterationID)
|
||||
if err != nil {
|
||||
return cty.NullVal(cty.EmptyObject), fmt.Errorf("error retrieving "+
|
||||
"image iteration from HCP Packer registry: %s", err.Error())
|
||||
}
|
||||
|
||||
output := DatasourceOutput{}
|
||||
|
||||
for _, build := range iteration.Builds {
|
||||
if build.CloudProvider == d.config.CloudProvider {
|
||||
for _, image := range build.Images {
|
||||
if image.Region == d.config.Region {
|
||||
// This is the desired image.
|
||||
output = DatasourceOutput{
|
||||
CloudProvider: build.CloudProvider,
|
||||
ComponentType: build.ComponentType,
|
||||
CreatedAt: image.CreatedAt.String(),
|
||||
BuildID: build.ID,
|
||||
IterationID: build.IterationID,
|
||||
PackerRunUUID: build.PackerRunUUID,
|
||||
ID: image.ImageID,
|
||||
Region: image.Region,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
// Code generated by "packer-sdc mapstructure-to-hcl2"; DO NOT EDIT.
|
||||
|
||||
package hcp_packer_image
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
// FlatConfig is an auto-generated flat version of Config.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatConfig struct {
|
||||
PackerBuildName *string `mapstructure:"packer_build_name" cty:"packer_build_name" hcl:"packer_build_name"`
|
||||
PackerBuilderType *string `mapstructure:"packer_builder_type" cty:"packer_builder_type" hcl:"packer_builder_type"`
|
||||
PackerCoreVersion *string `mapstructure:"packer_core_version" cty:"packer_core_version" hcl:"packer_core_version"`
|
||||
PackerDebug *bool `mapstructure:"packer_debug" cty:"packer_debug" hcl:"packer_debug"`
|
||||
PackerForce *bool `mapstructure:"packer_force" cty:"packer_force" hcl:"packer_force"`
|
||||
PackerOnError *string `mapstructure:"packer_on_error" cty:"packer_on_error" hcl:"packer_on_error"`
|
||||
PackerUserVars map[string]string `mapstructure:"packer_user_variables" cty:"packer_user_variables" hcl:"packer_user_variables"`
|
||||
PackerSensitiveVars []string `mapstructure:"packer_sensitive_variables" cty:"packer_sensitive_variables" hcl:"packer_sensitive_variables"`
|
||||
Bucket *string `mapstructure:"bucket_name" required:"true" cty:"bucket_name" hcl:"bucket_name"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatConfig.
|
||||
// FlatConfig is an auto-generated flat version of Config.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*Config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatConfig)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a Config.
|
||||
// This spec is used by HCL to read the fields of Config.
|
||||
// The decoded values from this spec will then be applied to a FlatConfig.
|
||||
func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"packer_build_name": &hcldec.AttrSpec{Name: "packer_build_name", Type: cty.String, Required: false},
|
||||
"packer_builder_type": &hcldec.AttrSpec{Name: "packer_builder_type", Type: cty.String, Required: false},
|
||||
"packer_core_version": &hcldec.AttrSpec{Name: "packer_core_version", Type: cty.String, Required: false},
|
||||
"packer_debug": &hcldec.AttrSpec{Name: "packer_debug", Type: cty.Bool, Required: false},
|
||||
"packer_force": &hcldec.AttrSpec{Name: "packer_force", Type: cty.Bool, Required: false},
|
||||
"packer_on_error": &hcldec.AttrSpec{Name: "packer_on_error", Type: cty.String, Required: false},
|
||||
"packer_user_variables": &hcldec.AttrSpec{Name: "packer_user_variables", Type: cty.Map(cty.String), Required: false},
|
||||
"packer_sensitive_variables": &hcldec.AttrSpec{Name: "packer_sensitive_variables", Type: cty.List(cty.String), Required: false},
|
||||
"bucket_name": &hcldec.AttrSpec{Name: "bucket_name", Type: cty.String, Required: false},
|
||||
"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},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// FlatDatasourceOutput is an auto-generated flat version of DatasourceOutput.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatDatasourceOutput struct {
|
||||
CloudProvider *string `mapstructure:"cloud_provider" cty:"cloud_provider" hcl:"cloud_provider"`
|
||||
ComponentType *string `mapstructure:"component_type" cty:"component_type" hcl:"component_type"`
|
||||
CreatedAt *string `mapstructure:"created_at" cty:"created_at" hcl:"created_at"`
|
||||
BuildID *string `mapstructure:"build_id" cty:"build_id" hcl:"build_id"`
|
||||
IterationID *string `mapstructure:"iteration_id" cty:"iteration_id" hcl:"iteration_id"`
|
||||
PackerRunUUID *string `mapstructure:"packer_run_uuid" cty:"packer_run_uuid" hcl:"packer_run_uuid"`
|
||||
ID *string `mapstructure:"id" cty:"id" hcl:"id"`
|
||||
Region *string `mapstructure:"region" cty:"region" hcl:"region"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatDatasourceOutput.
|
||||
// FlatDatasourceOutput is an auto-generated flat version of DatasourceOutput.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*DatasourceOutput) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatDatasourceOutput)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a DatasourceOutput.
|
||||
// This spec is used by HCL to read the fields of DatasourceOutput.
|
||||
// The decoded values from this spec will then be applied to a FlatDatasourceOutput.
|
||||
func (*FlatDatasourceOutput) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"cloud_provider": &hcldec.AttrSpec{Name: "cloud_provider", Type: cty.String, Required: false},
|
||||
"component_type": &hcldec.AttrSpec{Name: "component_type", Type: cty.String, Required: false},
|
||||
"created_at": &hcldec.AttrSpec{Name: "created_at", Type: cty.String, Required: false},
|
||||
"build_id": &hcldec.AttrSpec{Name: "build_id", Type: cty.String, Required: false},
|
||||
"iteration_id": &hcldec.AttrSpec{Name: "iteration_id", Type: cty.String, Required: false},
|
||||
"packer_run_uuid": &hcldec.AttrSpec{Name: "packer_run_uuid", Type: cty.String, Required: false},
|
||||
"id": &hcldec.AttrSpec{Name: "id", Type: cty.String, Required: false},
|
||||
"region": &hcldec.AttrSpec{Name: "region", Type: cty.String, Required: false},
|
||||
}
|
||||
return s
|
||||
}
|
||||
@ -0,0 +1,122 @@
|
||||
//go:generate packer-sdc struct-markdown
|
||||
//go:generate packer-sdc mapstructure-to-hcl2 -type DatasourceOutput,Config
|
||||
package hcp_packer_iteration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
"github.com/hashicorp/packer-plugin-sdk/common"
|
||||
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
|
||||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
||||
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
||||
packerregistry "github.com/hashicorp/packer/internal/packer_registry"
|
||||
)
|
||||
|
||||
type Datasource struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
// The name of the bucket your image is in.
|
||||
Bucket string `mapstructure:"bucket_name" required:"true"`
|
||||
// The name of the channel to use when retrieving your image
|
||||
Channel string `mapstructure:"channel" required:"true"`
|
||||
// TODO: Version string `mapstructure:"version"`
|
||||
// TODO: Fingerprint string `mapstructure:"fingerprint"`
|
||||
// TODO: Label string `mapstructure:"label"`
|
||||
}
|
||||
|
||||
func (d *Datasource) ConfigSpec() hcldec.ObjectSpec {
|
||||
return d.config.FlatMapstructure().HCL2Spec()
|
||||
}
|
||||
|
||||
func (d *Datasource) Configure(raws ...interface{}) error {
|
||||
err := config.Decode(&d.config, nil, raws...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var errs *packersdk.MultiError
|
||||
|
||||
if d.config.Bucket == "" {
|
||||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("The `bucket_name` must be specified"))
|
||||
}
|
||||
if d.config.Channel == "" {
|
||||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("`channel` is currently a required field."))
|
||||
}
|
||||
|
||||
if errs != nil && len(errs.Errors) > 0 {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Essentially a copy of []*models.HashicorpCloudPackerIteration, but without the
|
||||
// []Builds or ancestor id.
|
||||
type DatasourceOutput struct {
|
||||
// who created the iteration
|
||||
AuthorID string `mapstructure:"author_id"`
|
||||
// Name of the bucket that the iteration was retrieved from
|
||||
BucketName string `mapstructure:"bucket_name"`
|
||||
// If true, this iteration is considered "ready to use" and will be
|
||||
// returned even if the include_incomplete flag is "false" in the
|
||||
// list iterations request. Note that if you are retrieving an iteration
|
||||
// using a channel, this will always be "true"; channels cannot be assigned
|
||||
// to incomplete iterations.
|
||||
Complete bool `mapstructure:"complete"`
|
||||
// The date the iteration was created.
|
||||
CreatedAt string `mapstructure:"created_at"`
|
||||
// The fingerprint of the build; this could be a git sha or other unique
|
||||
// identifier as set by the Packer build that created this iteration.
|
||||
Fingerprint string `mapstructure:"fingerprint"`
|
||||
// The iteration id. This is a ULID, which is a unique identifier similar
|
||||
// to a UUID. It is created by the HCP Packer Registry when an iteration is
|
||||
// first created, and is unique to this iteration.
|
||||
ID string `mapstructure:"id"`
|
||||
// The version number assigned to an iteration. This number is an integer,
|
||||
// and is created by the HCP Packer Registry once an iteration is
|
||||
// marked "complete". If a new iteration is marked "complete", the version
|
||||
// that HCP Packer assigns to it will always be the highest previous
|
||||
// iteration version plus one.
|
||||
IncrementalVersion int32 `mapstructure:"incremental_version"`
|
||||
// The date when this iteration was last updated.
|
||||
UpdatedAt string `mapstructure:"updated_at"`
|
||||
}
|
||||
|
||||
func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
|
||||
return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec()
|
||||
}
|
||||
|
||||
func (d *Datasource) Execute() (cty.Value, error) {
|
||||
cli, err := packerregistry.NewClient()
|
||||
if err != nil {
|
||||
return cty.NullVal(cty.EmptyObject), err
|
||||
}
|
||||
// Load channel.
|
||||
log.Printf("[INFO] Reading iteration info from HCP Packer registry (%s) [project_id=%s, organization_id=%s, channel=%s]",
|
||||
d.config.Bucket, cli.ProjectID, cli.OrganizationID, d.config.Channel)
|
||||
|
||||
iteration, err := packerregistry.GetIterationFromChannel(context.TODO(), cli, d.config.Bucket, d.config.Channel)
|
||||
if err != nil {
|
||||
return cty.NullVal(cty.EmptyObject), fmt.Errorf("error retrieving "+
|
||||
"iteration from HCP Packer registry: %s", err.Error())
|
||||
}
|
||||
output := DatasourceOutput{
|
||||
AuthorID: iteration.AuthorID,
|
||||
BucketName: iteration.BucketSlug,
|
||||
Complete: iteration.Complete,
|
||||
CreatedAt: iteration.CreatedAt.String(),
|
||||
Fingerprint: iteration.Fingerprint,
|
||||
ID: iteration.ID,
|
||||
IncrementalVersion: iteration.IncrementalVersion,
|
||||
UpdatedAt: iteration.UpdatedAt.String(),
|
||||
}
|
||||
|
||||
return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
|
||||
}
|
||||
@ -0,0 +1,86 @@
|
||||
// Code generated by "packer-sdc mapstructure-to-hcl2"; DO NOT EDIT.
|
||||
|
||||
package hcp_packer_iteration
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
// FlatConfig is an auto-generated flat version of Config.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatConfig struct {
|
||||
PackerBuildName *string `mapstructure:"packer_build_name" cty:"packer_build_name" hcl:"packer_build_name"`
|
||||
PackerBuilderType *string `mapstructure:"packer_builder_type" cty:"packer_builder_type" hcl:"packer_builder_type"`
|
||||
PackerCoreVersion *string `mapstructure:"packer_core_version" cty:"packer_core_version" hcl:"packer_core_version"`
|
||||
PackerDebug *bool `mapstructure:"packer_debug" cty:"packer_debug" hcl:"packer_debug"`
|
||||
PackerForce *bool `mapstructure:"packer_force" cty:"packer_force" hcl:"packer_force"`
|
||||
PackerOnError *string `mapstructure:"packer_on_error" cty:"packer_on_error" hcl:"packer_on_error"`
|
||||
PackerUserVars map[string]string `mapstructure:"packer_user_variables" cty:"packer_user_variables" hcl:"packer_user_variables"`
|
||||
PackerSensitiveVars []string `mapstructure:"packer_sensitive_variables" cty:"packer_sensitive_variables" hcl:"packer_sensitive_variables"`
|
||||
Bucket *string `mapstructure:"bucket_name" required:"true" cty:"bucket_name" hcl:"bucket_name"`
|
||||
Channel *string `mapstructure:"channel" required:"true" cty:"channel" hcl:"channel"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatConfig.
|
||||
// FlatConfig is an auto-generated flat version of Config.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*Config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatConfig)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a Config.
|
||||
// This spec is used by HCL to read the fields of Config.
|
||||
// The decoded values from this spec will then be applied to a FlatConfig.
|
||||
func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"packer_build_name": &hcldec.AttrSpec{Name: "packer_build_name", Type: cty.String, Required: false},
|
||||
"packer_builder_type": &hcldec.AttrSpec{Name: "packer_builder_type", Type: cty.String, Required: false},
|
||||
"packer_core_version": &hcldec.AttrSpec{Name: "packer_core_version", Type: cty.String, Required: false},
|
||||
"packer_debug": &hcldec.AttrSpec{Name: "packer_debug", Type: cty.Bool, Required: false},
|
||||
"packer_force": &hcldec.AttrSpec{Name: "packer_force", Type: cty.Bool, Required: false},
|
||||
"packer_on_error": &hcldec.AttrSpec{Name: "packer_on_error", Type: cty.String, Required: false},
|
||||
"packer_user_variables": &hcldec.AttrSpec{Name: "packer_user_variables", Type: cty.Map(cty.String), Required: false},
|
||||
"packer_sensitive_variables": &hcldec.AttrSpec{Name: "packer_sensitive_variables", Type: cty.List(cty.String), Required: false},
|
||||
"bucket_name": &hcldec.AttrSpec{Name: "bucket_name", Type: cty.String, Required: false},
|
||||
"channel": &hcldec.AttrSpec{Name: "channel", Type: cty.String, Required: false},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// FlatDatasourceOutput is an auto-generated flat version of DatasourceOutput.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatDatasourceOutput struct {
|
||||
AuthorID *string `mapstructure:"author_id" cty:"author_id" hcl:"author_id"`
|
||||
BucketName *string `mapstructure:"bucket_name" cty:"bucket_name" hcl:"bucket_name"`
|
||||
Complete *bool `mapstructure:"complete" cty:"complete" hcl:"complete"`
|
||||
CreatedAt *string `mapstructure:"created_at" cty:"created_at" hcl:"created_at"`
|
||||
Fingerprint *string `mapstructure:"fingerprint" cty:"fingerprint" hcl:"fingerprint"`
|
||||
ID *string `mapstructure:"id" cty:"id" hcl:"id"`
|
||||
IncrementalVersion *int32 `mapstructure:"incremental_version" cty:"incremental_version" hcl:"incremental_version"`
|
||||
UpdatedAt *string `mapstructure:"updated_at" cty:"updated_at" hcl:"updated_at"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatDatasourceOutput.
|
||||
// FlatDatasourceOutput is an auto-generated flat version of DatasourceOutput.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*DatasourceOutput) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatDatasourceOutput)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a DatasourceOutput.
|
||||
// This spec is used by HCL to read the fields of DatasourceOutput.
|
||||
// The decoded values from this spec will then be applied to a FlatDatasourceOutput.
|
||||
func (*FlatDatasourceOutput) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"author_id": &hcldec.AttrSpec{Name: "author_id", Type: cty.String, Required: false},
|
||||
"bucket_name": &hcldec.AttrSpec{Name: "bucket_name", Type: cty.String, Required: false},
|
||||
"complete": &hcldec.AttrSpec{Name: "complete", Type: cty.Bool, Required: false},
|
||||
"created_at": &hcldec.AttrSpec{Name: "created_at", Type: cty.String, Required: false},
|
||||
"fingerprint": &hcldec.AttrSpec{Name: "fingerprint", Type: cty.String, Required: false},
|
||||
"id": &hcldec.AttrSpec{Name: "id", Type: cty.String, Required: false},
|
||||
"incremental_version": &hcldec.AttrSpec{Name: "incremental_version", Type: cty.Number, Required: false},
|
||||
"updated_at": &hcldec.AttrSpec{Name: "updated_at", Type: cty.String, Required: false},
|
||||
}
|
||||
return s
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package hcp_packer_iteration
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/packer-plugin-sdk/acctest"
|
||||
)
|
||||
|
||||
//go:embed test-fixtures/template.pkr.hcl
|
||||
var testDatasourceBasic string
|
||||
|
||||
// Acceptance tests for data sources.
|
||||
//
|
||||
// To be successful, the HCP project you're providing credentials for must
|
||||
// contain a bucket named "hardened-ubuntu-16-04", with a channel named
|
||||
// "packer-acc-test". It must contain a build that references an image in AWS
|
||||
// region "us-east-1". Your HCP credentials must be provided through your
|
||||
// runtime environment because the template this test uses does not set them.
|
||||
//
|
||||
// TODO: update this acceptance to create and clean up the HCP resources this
|
||||
// data source queries, to prevent plugin developers from having to have images
|
||||
// as defined above.
|
||||
|
||||
func TestAccDatasource_HCPPackerIteration(t *testing.T) {
|
||||
testCase := &acctest.PluginTestCase{
|
||||
Name: "hcp_packer_iteration_datasource_basic_test",
|
||||
Template: testDatasourceBasic,
|
||||
// TODO have acc test write iteration id to a file and check it to make
|
||||
// sure it isn't empty.
|
||||
Check: func(buildCommand *exec.Cmd, logfile string) error {
|
||||
if buildCommand.ProcessState != nil {
|
||||
if buildCommand.ProcessState.ExitCode() != 0 {
|
||||
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
acctest.TestPlugin(t, testCase)
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
source "null" "example" {
|
||||
communicator = "none"
|
||||
}
|
||||
|
||||
data "hcp-packer-iteration" "hardened-source" {
|
||||
bucket_name = "hardened-ubuntu-16-04"
|
||||
channel = "packer-acc-test"
|
||||
}
|
||||
|
||||
data "hcp-packer-image" "aws" {
|
||||
bucket_name = "hardened-ubuntu-16-04"
|
||||
iteration_id = "${data.hcp-packer-iteration.hardened-source.id}"
|
||||
cloud_provider = "aws"
|
||||
region = "us-east-1"
|
||||
}
|
||||
|
||||
locals {
|
||||
foo = "${data.hcp-packer-iteration.hardened-source.id}"
|
||||
bar = "${data.hcp-packer-image.aws.id}"
|
||||
}
|
||||
|
||||
build {
|
||||
name = "mybuild"
|
||||
sources = [
|
||||
"source.null.example"
|
||||
]
|
||||
provisioner "shell-local" {
|
||||
inline = [
|
||||
"echo data is ${local.foo}",
|
||||
"echo data is ${local.bar}"
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
<!-- Code generated from the comments of the Config struct in datasource/hcp-packer-image/data.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `bucket_name` (string) - The name of the bucket your image is in.
|
||||
|
||||
- `iteration_id` (string) - The name of the iteration Id to use when retrieving your image
|
||||
|
||||
- `cloud_provider` (string) - The name of the cloud provider that your image is for. For example,
|
||||
"aws" or "gce".
|
||||
|
||||
- `region` (string) - The name of the cloud region your image is in. For example "us-east-1".
|
||||
|
||||
<!-- End of code generated from the comments of the Config struct in datasource/hcp-packer-image/data.go; -->
|
||||
@ -0,0 +1,26 @@
|
||||
<!-- Code generated from the comments of the DatasourceOutput struct in datasource/hcp-packer-image/data.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `cloud_provider` (string) - The name of the cloud provider that the image exists in. For example,
|
||||
"aws", "azure", or "gce".
|
||||
|
||||
- `component_type` (string) - The specific Packer builder or post-processor used to create the image.
|
||||
|
||||
- `created_at` (string) - The date and time at which the image was created.
|
||||
|
||||
- `build_id` (string) - The id of the build that created the image. This is a ULID, which is a
|
||||
unique identifier similar to a UUID. It is created by the HCP Packer
|
||||
Registry when an build is first created, and is unique to this build.
|
||||
|
||||
- `iteration_id` (string) - The iteration id. This is a ULID, which is a unique identifier similar
|
||||
to a UUID. It is created by the HCP Packer Registry when an iteration is
|
||||
first created, and is unique to this iteration.
|
||||
|
||||
- `packer_run_uuid` (string) - The UUID associated with the Packer run that created this image.
|
||||
|
||||
- `id` (string) - ID or URL of the remote cloud image as given by a build.
|
||||
|
||||
- `region` (string) - The cloud region as given by `packer build`. eg. "ap-east-1".
|
||||
For locally managed clouds, this may map instead to a cluster, server
|
||||
or datastore.
|
||||
|
||||
<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/hcp-packer-image/data.go; -->
|
||||
@ -0,0 +1,7 @@
|
||||
<!-- Code generated from the comments of the Config struct in datasource/hcp-packer-iteration/data.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `bucket_name` (string) - The name of the bucket your image is in.
|
||||
|
||||
- `channel` (string) - The name of the channel to use when retrieving your image
|
||||
|
||||
<!-- End of code generated from the comments of the Config struct in datasource/hcp-packer-iteration/data.go; -->
|
||||
@ -0,0 +1,30 @@
|
||||
<!-- Code generated from the comments of the DatasourceOutput struct in datasource/hcp-packer-iteration/data.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `author_id` (string) - who created the iteration
|
||||
|
||||
- `bucket_name` (string) - Name of the bucket that the iteration was retrieved from
|
||||
|
||||
- `complete` (bool) - If true, this iteration is considered "ready to use" and will be
|
||||
returned even if the include_incomplete flag is "false" in the
|
||||
list iterations request. Note that if you are retrieving an iteration
|
||||
using a channel, this will always be "true"; channels cannot be assigned
|
||||
to incomplete iterations.
|
||||
|
||||
- `created_at` (string) - The date the iteration was created.
|
||||
|
||||
- `fingerprint` (string) - The fingerprint of the build; this could be a git sha or other unique
|
||||
identifier as set by the Packer build that created this iteration.
|
||||
|
||||
- `id` (string) - The iteration id. This is a ULID, which is a unique identifier similar
|
||||
to a UUID. It is created by the HCP Packer Registry when an iteration is
|
||||
first created, and is unique to this iteration.
|
||||
|
||||
- `incremental_version` (int32) - The version number assigned to an iteration. This number is an integer,
|
||||
and is created by the HCP Packer Registry once an iteration is
|
||||
marked "complete". If a new iteration is marked "complete", the version
|
||||
that HCP Packer assigns to it will always be the highest previous
|
||||
iteration version plus one.
|
||||
|
||||
- `updated_at` (string) - The date when this iteration was last updated.
|
||||
|
||||
<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/hcp-packer-iteration/data.go; -->
|
||||
Loading…
Reference in new issue