builder.amazon.common: remove pointer (*) from []*string and map[*string]*string types

because they make hcl2/cty decoding panic
This commit is contained in:
Adrien Delorme 2019-10-14 15:56:49 +02:00
parent 8b8c2fc138
commit 221e72e9c3
6 changed files with 24 additions and 17 deletions

View File

@ -5,12 +5,12 @@ import (
) )
// Build a slice of EC2 (AMI/Subnet/VPC) filter options from the filters provided. // Build a slice of EC2 (AMI/Subnet/VPC) filter options from the filters provided.
func buildEc2Filters(input map[*string]*string) []*ec2.Filter { func buildEc2Filters(input map[string]string) []*ec2.Filter {
var filters []*ec2.Filter var filters []*ec2.Filter
for k, v := range input { for k, v := range input {
filters = append(filters, &ec2.Filter{ filters = append(filters, &ec2.Filter{
Name: k, Name: &k,
Values: []*string{v}, Values: []*string{&v},
}) })
} }
return filters return filters

View File

@ -18,11 +18,19 @@ import (
var reShutdownBehavior = regexp.MustCompile("^(stop|terminate)$") var reShutdownBehavior = regexp.MustCompile("^(stop|terminate)$")
type AmiFilterOptions struct { type AmiFilterOptions struct {
Filters map[*string]*string Filters map[string]string
Owners []*string Owners []string
MostRecent bool `mapstructure:"most_recent"` MostRecent bool `mapstructure:"most_recent"`
} }
func (d *AmiFilterOptions) GetOwners() []*string {
res := make([]*string, 0, len(d.Owners))
for _, owner := range d.Owners {
res = append(res, &owner)
}
return res
}
func (d *AmiFilterOptions) Empty() bool { func (d *AmiFilterOptions) Empty() bool {
return len(d.Owners) == 0 && len(d.Filters) == 0 return len(d.Owners) == 0 && len(d.Filters) == 0
} }
@ -32,7 +40,7 @@ func (d *AmiFilterOptions) NoOwner() bool {
} }
type SubnetFilterOptions struct { type SubnetFilterOptions struct {
Filters map[*string]*string Filters map[string]string
MostFree bool `mapstructure:"most_free"` MostFree bool `mapstructure:"most_free"`
Random bool `mapstructure:"random"` Random bool `mapstructure:"random"`
} }
@ -42,7 +50,7 @@ func (d *SubnetFilterOptions) Empty() bool {
} }
type VpcFilterOptions struct { type VpcFilterOptions struct {
Filters map[*string]*string Filters map[string]string
} }
func (d *VpcFilterOptions) Empty() bool { func (d *VpcFilterOptions) Empty() bool {
@ -50,7 +58,7 @@ func (d *VpcFilterOptions) Empty() bool {
} }
type SecurityGroupFilterOptions struct { type SecurityGroupFilterOptions struct {
Filters map[*string]*string Filters map[string]string
} }
func (d *SecurityGroupFilterOptions) Empty() bool { func (d *SecurityGroupFilterOptions) Empty() bool {

View File

@ -73,7 +73,7 @@ func TestRunConfigPrepare_SourceAmiFilterOwnersBlank(t *testing.T) {
c := testConfigFilter() c := testConfigFilter()
filter_key := "name" filter_key := "name"
filter_value := "foo" filter_value := "foo"
c.SourceAmiFilter = AmiFilterOptions{Filters: map[*string]*string{&filter_key: &filter_value}} c.SourceAmiFilter = AmiFilterOptions{Filters: map[string]string{filter_key: filter_value}}
if err := c.Prepare(nil); len(err) != 1 { if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("Should error if Owners is not specified)") t.Fatalf("Should error if Owners is not specified)")
} }
@ -84,7 +84,7 @@ func TestRunConfigPrepare_SourceAmiFilterGood(t *testing.T) {
owner := "123" owner := "123"
filter_key := "name" filter_key := "name"
filter_value := "foo" filter_value := "foo"
goodFilter := AmiFilterOptions{Owners: []*string{&owner}, Filters: map[*string]*string{&filter_key: &filter_value}} goodFilter := AmiFilterOptions{Owners: []string{owner}, Filters: map[string]string{filter_key: filter_value}}
c.SourceAmiFilter = goodFilter c.SourceAmiFilter = goodFilter
if err := c.Prepare(nil); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)

View File

@ -7,7 +7,6 @@ import (
"math/rand" "math/rand"
"sort" "sort"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -53,7 +52,7 @@ func (s *StepNetworkInfo) Run(ctx context.Context, state multistep.StateBag) mul
if s.VpcId == "" && !s.VpcFilter.Empty() { if s.VpcId == "" && !s.VpcFilter.Empty() {
params := &ec2.DescribeVpcsInput{} params := &ec2.DescribeVpcsInput{}
params.Filters = buildEc2Filters(s.VpcFilter.Filters) params.Filters = buildEc2Filters(s.VpcFilter.Filters)
s.VpcFilter.Filters[aws.String("state")] = aws.String("available") s.VpcFilter.Filters["state"] = "available"
log.Printf("Using VPC Filters %v", params) log.Printf("Using VPC Filters %v", params)
@ -79,13 +78,13 @@ func (s *StepNetworkInfo) Run(ctx context.Context, state multistep.StateBag) mul
// Subnet // Subnet
if s.SubnetId == "" && !s.SubnetFilter.Empty() { if s.SubnetId == "" && !s.SubnetFilter.Empty() {
params := &ec2.DescribeSubnetsInput{} params := &ec2.DescribeSubnetsInput{}
s.SubnetFilter.Filters[aws.String("state")] = aws.String("available") s.SubnetFilter.Filters["state"] = "available"
if s.VpcId != "" { if s.VpcId != "" {
s.SubnetFilter.Filters[aws.String("vpc-id")] = &s.VpcId s.SubnetFilter.Filters["vpc-id"] = s.VpcId
} }
if s.AvailabilityZone != "" { if s.AvailabilityZone != "" {
s.SubnetFilter.Filters[aws.String("availability-zone")] = &s.AvailabilityZone s.SubnetFilter.Filters["availabilityZone"] = s.AvailabilityZone
} }
params.Filters = buildEc2Filters(s.SubnetFilter.Filters) params.Filters = buildEc2Filters(s.SubnetFilter.Filters)
log.Printf("Using Subnet Filters %v", params) log.Printf("Using Subnet Filters %v", params)

View File

@ -51,7 +51,7 @@ func (s *StepSecurityGroup) Run(ctx context.Context, state multistep.StateBag) m
params := &ec2.DescribeSecurityGroupsInput{} params := &ec2.DescribeSecurityGroupsInput{}
if vpcId != "" { if vpcId != "" {
s.SecurityGroupFilter.Filters[aws.String("vpc-id")] = &vpcId s.SecurityGroupFilter.Filters["vpc-id"] = vpcId
} }
params.Filters = buildEc2Filters(s.SecurityGroupFilter.Filters) params.Filters = buildEc2Filters(s.SecurityGroupFilter.Filters)

View File

@ -58,7 +58,7 @@ func (s *StepSourceAMIInfo) Run(ctx context.Context, state multistep.StateBag) m
params.Filters = buildEc2Filters(s.AmiFilters.Filters) params.Filters = buildEc2Filters(s.AmiFilters.Filters)
} }
if len(s.AmiFilters.Owners) > 0 { if len(s.AmiFilters.Owners) > 0 {
params.Owners = s.AmiFilters.Owners params.Owners = s.AmiFilters.GetOwners()
} }
log.Printf("Using AMI Filters %v", params) log.Printf("Using AMI Filters %v", params)