From a08725f7c5b76cf7f99f79f71e545bffb0f15944 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sun, 1 Mar 2015 00:00:45 +1100 Subject: [PATCH 1/3] Add auto discovery of AZ based on the subnet --- builder/amazon/chroot/builder.go | 11 +++++++++++ builder/amazon/ebs/builder.go | 11 +++++++++++ builder/amazon/instance/builder.go | 11 +++++++++++ 3 files changed, 33 insertions(+) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 832a26022..0cea71a93 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -165,6 +165,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ec2conn := ec2.New(auth, region) + // If the subnet is specified but not the AZ, try to determine the AZ automatically + if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { + log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) + resp, err := ec2conn.DescribeSubnets([]string{b.config.SubnetId}, nil) + if err != nil { + return nil, err + } + b.config.AvailabilityZone = resp.Subnets[0].AvailabilityZone + log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) + } + wrappedCommand := func(command string) (string, error) { return b.config.tpl.Process( b.config.CommandWrapper, &wrappedCommandTemplate{ diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 889cc7b60..04e432a49 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -75,6 +75,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ec2conn := ec2.New(auth, region) + // If the subnet is specified but not the AZ, try to determine the AZ automatically + if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { + log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) + resp, err := ec2conn.DescribeSubnets([]string{b.config.SubnetId}, nil) + if err != nil { + return nil, err + } + b.config.AvailabilityZone = resp.Subnets[0].AvailabilityZone + log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) + } + // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) state.Put("config", b.config) diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 63b8442ac..b7cf0c694 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -180,6 +180,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ec2conn := ec2.New(auth, region) + // If the subnet is specified but not the AZ, try to determine the AZ automatically + if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { + log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) + resp, err := ec2conn.DescribeSubnets([]string{b.config.SubnetId}, nil) + if err != nil { + return nil, err + } + b.config.AvailabilityZone = resp.Subnets[0].AvailabilityZone + log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) + } + // Setup the state bag and initial state for the steps state := new(multistep.BasicStateBag) state.Put("config", &b.config) From 9385a277c6effc95f97f13fcfa7b9476416f61ce Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Wed, 6 Jan 2016 08:02:29 +0100 Subject: [PATCH 2/3] Fix API calls and remove az detection in chroot --- builder/amazon/chroot/builder.go | 11 ----------- builder/amazon/ebs/builder.go | 4 ++-- builder/amazon/instance/builder.go | 4 ++-- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 867f963c9..637b1e40f 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -135,17 +135,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe session := session.New(config) ec2conn := ec2.New(session) - // If the subnet is specified but not the AZ, try to determine the AZ automatically - if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { - log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) - resp, err := ec2conn.DescribeSubnets([]string{b.config.SubnetId}, nil) - if err != nil { - return nil, err - } - b.config.AvailabilityZone = resp.Subnets[0].AvailabilityZone - log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) - } - wrappedCommand := func(command string) (string, error) { ctx := b.config.ctx ctx.Data = &wrappedCommandTemplate{Command: command} diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index bf55d988c..be6152401 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -75,11 +75,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // If the subnet is specified but not the AZ, try to determine the AZ automatically if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) - resp, err := ec2conn.DescribeSubnets([]string{b.config.SubnetId}, nil) + resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []string{b.config.SubnetId}}) if err != nil { return nil, err } - b.config.AvailabilityZone = resp.Subnets[0].AvailabilityZone + b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) } diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index ee7719e31..c9c1f2012 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -166,11 +166,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // If the subnet is specified but not the AZ, try to determine the AZ automatically if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) - resp, err := ec2conn.DescribeSubnets([]string{b.config.SubnetId}, nil) + resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []string{b.config.SubnetId}}) if err != nil { return nil, err } - b.config.AvailabilityZone = resp.Subnets[0].AvailabilityZone + b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) } From 48d242a8f86c0461f2e64073879d62d6232c8977 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Wed, 6 Jan 2016 08:12:20 +0100 Subject: [PATCH 3/3] Use pointers in the AWS API --- builder/amazon/ebs/builder.go | 2 +- builder/amazon/instance/builder.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index be6152401..50274ef87 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -75,7 +75,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // If the subnet is specified but not the AZ, try to determine the AZ automatically if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) - resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []string{b.config.SubnetId}}) + resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}}) if err != nil { return nil, err } diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index c9c1f2012..96b8fa9cc 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -166,7 +166,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // If the subnet is specified but not the AZ, try to determine the AZ automatically if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) - resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []string{b.config.SubnetId}}) + resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}}) if err != nil { return nil, err }