use the hcl2template.KVFilter where we can

This commit is contained in:
Adrien Delorme 2020-03-13 17:17:46 +01:00
parent b4efd13a4d
commit 260dc59500
4 changed files with 65 additions and 50 deletions

View File

@ -253,6 +253,12 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
var errs *packer.MultiError
var warns []string
for _, preparer := range []interface{ Prepare() error }{
&b.config.SourceAmiFilter,
} {
errs = packer.MultiErrorAppend(errs, preparer.Prepare())
}
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs,
b.config.AMIConfig.Prepare(&b.config.AccessConfig, &b.config.ctx)...)

View File

@ -12,6 +12,7 @@ import (
"time"
"github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/hcl2template"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/template/interpolate"
)
@ -19,9 +20,9 @@ import (
var reShutdownBehavior = regexp.MustCompile("^(stop|terminate)$")
type AmiFilterOptions struct {
Filters map[string]string
Owners []string
MostRecent bool `mapstructure:"most_recent"`
hcl2template.KVFilter `mapstructure:",squash"`
Owners []string
MostRecent bool `mapstructure:"most_recent"`
}
func (d *AmiFilterOptions) GetOwners() []*string {
@ -34,7 +35,7 @@ func (d *AmiFilterOptions) GetOwners() []*string {
}
func (d *AmiFilterOptions) Empty() bool {
return len(d.Owners) == 0 && len(d.Filters) == 0
return len(d.Owners) == 0 && d.KVFilter.Empty()
}
func (d *AmiFilterOptions) NoOwner() bool {
@ -42,17 +43,13 @@ func (d *AmiFilterOptions) NoOwner() bool {
}
type SubnetFilterOptions struct {
Filters map[string]string
MostFree bool `mapstructure:"most_free"`
Random bool `mapstructure:"random"`
}
func (d *SubnetFilterOptions) Empty() bool {
return len(d.Filters) == 0
hcl2template.KVFilter `mapstructure:",squash"`
MostFree bool `mapstructure:"most_free"`
Random bool `mapstructure:"random"`
}
type VpcFilterOptions struct {
Filters map[string]string
hcl2template.KVFilter `mapstructure:",squash"`
}
type Statement struct {
@ -66,16 +63,8 @@ type PolicyDocument struct {
Statement []Statement
}
func (d *VpcFilterOptions) Empty() bool {
return len(d.Filters) == 0
}
type SecurityGroupFilterOptions struct {
Filters map[string]string
}
func (d *SecurityGroupFilterOptions) Empty() bool {
return len(d.Filters) == 0
hcl2template.KVFilter `mapstructure:",squash"`
}
// RunConfig contains configuration for running an instance from a source
@ -423,6 +412,17 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
// Validation
errs := c.Comm.Prepare(ctx)
for _, preparer := range []interface{ Prepare() error }{
&c.SourceAmiFilter,
&c.SecurityGroupFilter,
&c.SubnetFilter,
&c.VpcFilter,
} {
if err := preparer.Prepare(); err != nil {
errs = append(errs, err)
}
}
// Validating ssh_interface
if c.SSHInterface != "public_ip" &&
c.SSHInterface != "private_ip" &&

View File

@ -3,15 +3,17 @@ package common
import (
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/hcl2template"
"github.com/zclconf/go-cty/cty"
)
// FlatAmiFilterOptions is an auto-generated flat version of AmiFilterOptions.
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
type FlatAmiFilterOptions struct {
Filters map[string]string `cty:"filters"`
Owners []string `cty:"owners"`
MostRecent *bool `mapstructure:"most_recent" cty:"most_recent"`
Filters map[string]string `cty:"filters"`
Filter []hcl2template.FlatKeyValue `cty:"filter"`
Owners []string `cty:"owners"`
MostRecent *bool `mapstructure:"most_recent" cty:"most_recent"`
}
// FlatMapstructure returns a new FlatAmiFilterOptions.
@ -27,6 +29,7 @@ func (*AmiFilterOptions) FlatMapstructure() interface{ HCL2Spec() map[string]hcl
func (*FlatAmiFilterOptions) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{
"filters": &hcldec.BlockAttrsSpec{TypeName: "filters", ElementType: cty.String, Required: false},
"filter": &hcldec.BlockListSpec{TypeName: "filter", Nested: hcldec.ObjectSpec((*hcl2template.FlatKeyValue)(nil).HCL2Spec())},
"owners": &hcldec.AttrSpec{Name: "owners", Type: cty.List(cty.String), Required: false},
"most_recent": &hcldec.AttrSpec{Name: "most_recent", Type: cty.Bool, Required: false},
}
@ -61,7 +64,8 @@ func (*FlatPolicyDocument) HCL2Spec() map[string]hcldec.Spec {
// FlatSecurityGroupFilterOptions is an auto-generated flat version of SecurityGroupFilterOptions.
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
type FlatSecurityGroupFilterOptions struct {
Filters map[string]string `cty:"filters"`
Filters map[string]string `cty:"filters"`
Filter []hcl2template.FlatKeyValue `cty:"filter"`
}
// FlatMapstructure returns a new FlatSecurityGroupFilterOptions.
@ -77,6 +81,7 @@ func (*SecurityGroupFilterOptions) FlatMapstructure() interface{ HCL2Spec() map[
func (*FlatSecurityGroupFilterOptions) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{
"filters": &hcldec.BlockAttrsSpec{TypeName: "filters", ElementType: cty.String, Required: false},
"filter": &hcldec.BlockListSpec{TypeName: "filter", Nested: hcldec.ObjectSpec((*hcl2template.FlatKeyValue)(nil).HCL2Spec())},
}
return s
}
@ -111,9 +116,10 @@ func (*FlatStatement) HCL2Spec() map[string]hcldec.Spec {
// FlatSubnetFilterOptions is an auto-generated flat version of SubnetFilterOptions.
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
type FlatSubnetFilterOptions struct {
Filters map[string]string `cty:"filters"`
MostFree *bool `mapstructure:"most_free" cty:"most_free"`
Random *bool `mapstructure:"random" cty:"random"`
Filters map[string]string `cty:"filters"`
Filter []hcl2template.FlatKeyValue `cty:"filter"`
MostFree *bool `mapstructure:"most_free" cty:"most_free"`
Random *bool `mapstructure:"random" cty:"random"`
}
// FlatMapstructure returns a new FlatSubnetFilterOptions.
@ -129,6 +135,7 @@ func (*SubnetFilterOptions) FlatMapstructure() interface{ HCL2Spec() map[string]
func (*FlatSubnetFilterOptions) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{
"filters": &hcldec.BlockAttrsSpec{TypeName: "filters", ElementType: cty.String, Required: false},
"filter": &hcldec.BlockListSpec{TypeName: "filter", Nested: hcldec.ObjectSpec((*hcl2template.FlatKeyValue)(nil).HCL2Spec())},
"most_free": &hcldec.AttrSpec{Name: "most_free", Type: cty.Bool, Required: false},
"random": &hcldec.AttrSpec{Name: "random", Type: cty.Bool, Required: false},
}
@ -138,7 +145,8 @@ func (*FlatSubnetFilterOptions) HCL2Spec() map[string]hcldec.Spec {
// FlatVpcFilterOptions is an auto-generated flat version of VpcFilterOptions.
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
type FlatVpcFilterOptions struct {
Filters map[string]string `cty:"filters"`
Filters map[string]string `cty:"filters"`
Filter []hcl2template.FlatKeyValue `cty:"filter"`
}
// FlatMapstructure returns a new FlatVpcFilterOptions.
@ -154,6 +162,7 @@ func (*VpcFilterOptions) FlatMapstructure() interface{ HCL2Spec() map[string]hcl
func (*FlatVpcFilterOptions) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{
"filters": &hcldec.BlockAttrsSpec{TypeName: "filters", ElementType: cty.String, Required: false},
"filter": &hcldec.BlockListSpec{TypeName: "filter", Nested: hcldec.ObjectSpec((*hcl2template.FlatKeyValue)(nil).HCL2Spec())},
}
return s
}

View File

@ -11,6 +11,7 @@ import (
"time"
"github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/hcl2template"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/template/interpolate"
)
@ -18,13 +19,13 @@ import (
var reShutdownBehavior = regexp.MustCompile("^(stop|terminate)$")
type OmiFilterOptions struct {
Filters map[string]string
Owners []string
MostRecent bool `mapstructure:"most_recent"`
hcl2template.KVFilter `mapstructure:",squash"`
Owners []string
MostRecent bool `mapstructure:"most_recent"`
}
func (d *OmiFilterOptions) Empty() bool {
return len(d.Owners) == 0 && len(d.Filters) == 0
return len(d.Owners) == 0 && d.KVFilter.Empty()
}
func (d *OmiFilterOptions) NoOwner() bool {
@ -32,29 +33,17 @@ func (d *OmiFilterOptions) NoOwner() bool {
}
type SubnetFilterOptions struct {
Filters map[string]string
MostFree bool `mapstructure:"most_free"`
Random bool `mapstructure:"random"`
}
func (d *SubnetFilterOptions) Empty() bool {
return len(d.Filters) == 0
hcl2template.KVFilter `mapstructure:",squash"`
MostFree bool `mapstructure:"most_free"`
Random bool `mapstructure:"random"`
}
type NetFilterOptions struct {
Filters map[string]string
}
func (d *NetFilterOptions) Empty() bool {
return len(d.Filters) == 0
hcl2template.KVFilter `mapstructure:",squash"`
}
type SecurityGroupFilterOptions struct {
Filters map[string]string
}
func (d *SecurityGroupFilterOptions) Empty() bool {
return len(d.Filters) == 0
hcl2template.KVFilter `mapstructure:",squash"`
}
// RunConfig contains configuration for running an vm from a source
@ -115,6 +104,17 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
// Validation
errs := c.Comm.Prepare(ctx)
for _, preparer := range []interface{ Prepare() error }{
&c.SourceOmiFilter,
&c.SecurityGroupFilter,
&c.SubnetFilter,
&c.NetFilter,
} {
if err := preparer.Prepare(); err != nil {
errs = append(errs, err)
}
}
// Validating ssh_interface
if c.SSHInterface != "public_ip" &&
c.SSHInterface != "private_ip" &&