use guard clauses
* just return early if no FloatingIP is required * move the instanceIP declaration up and store in the state bag to avoid errors in ssh.go
This commit is contained in:
parent
ea78cb62fd
commit
653fa2298f
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gophercloud/gophercloud"
|
|
||||||
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
||||||
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
|
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
|
@ -22,6 +21,17 @@ func (s *StepAllocateIp) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
config := state.Get("config").(*Config)
|
config := state.Get("config").(*Config)
|
||||||
server := state.Get("server").(*servers.Server)
|
server := state.Get("server").(*servers.Server)
|
||||||
|
|
||||||
|
var instanceIP floatingips.FloatingIP
|
||||||
|
|
||||||
|
// This is here in case we error out before putting instanceIp into the
|
||||||
|
// statebag below, because it is requested by Cleanup()
|
||||||
|
state.Put("access_ip", &instanceIP)
|
||||||
|
|
||||||
|
if s.FloatingIP == "" && !s.ReuseIPs && s.FloatingIPNetwork == "" {
|
||||||
|
ui.Message("Floating IP not required")
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
// We need the v2 compute client
|
// We need the v2 compute client
|
||||||
computeClient, err := config.computeV2Client()
|
computeClient, err := config.computeV2Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -30,23 +40,14 @@ func (s *StepAllocateIp) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
// We might need the v2 network client
|
// We need the v2 network client
|
||||||
var networkClient *gophercloud.ServiceClient
|
networkClient, err := config.networkV2Client()
|
||||||
if s.FloatingIP != "" || s.ReuseIPs || s.FloatingIPNetwork != "" {
|
if err != nil {
|
||||||
networkClient, err = config.networkV2Client()
|
err = fmt.Errorf("Error initializing network client: %s", err)
|
||||||
if err != nil {
|
state.Put("error", err)
|
||||||
err = fmt.Errorf("Error initializing network client: %s", err)
|
return multistep.ActionHalt
|
||||||
state.Put("error", err)
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var instanceIP floatingips.FloatingIP
|
|
||||||
|
|
||||||
// This is here in case we error out before putting instanceIp into the
|
|
||||||
// statebag below, because it is requested by Cleanup()
|
|
||||||
state.Put("access_ip", &instanceIP)
|
|
||||||
|
|
||||||
// Try to Use the OpenStack floating IP by checking provided parameters in
|
// Try to Use the OpenStack floating IP by checking provided parameters in
|
||||||
// the following order:
|
// the following order:
|
||||||
// - try to use "FloatingIP" ID directly if it's provided
|
// - try to use "FloatingIP" ID directly if it's provided
|
||||||
|
@ -146,20 +147,25 @@ func (s *StepAllocateIp) Cleanup(state multistep.StateBag) {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
instanceIP := state.Get("access_ip").(*floatingips.FloatingIP)
|
instanceIP := state.Get("access_ip").(*floatingips.FloatingIP)
|
||||||
|
|
||||||
|
// Don't clean up if unless required
|
||||||
|
if instanceIP.ID == "" && instanceIP.FloatingIP == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Don't delete pool addresses we didn't allocate
|
// Don't delete pool addresses we didn't allocate
|
||||||
if state.Get("floatingip_istemp") == false {
|
if state.Get("floatingip_istemp") == false {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if instanceIP.FloatingIP != "" && instanceIP.ID != "" {
|
// We need the v2 network client
|
||||||
// We need the v2 network client
|
client, err := config.networkV2Client()
|
||||||
client, err := config.networkV2Client()
|
if err != nil {
|
||||||
if err != nil {
|
ui.Error(fmt.Sprintf(
|
||||||
ui.Error(fmt.Sprintf(
|
"Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
||||||
"Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if instanceIP.ID != "" {
|
||||||
if err := floatingips.Delete(client, instanceIP.ID).ExtractErr(); err != nil {
|
if err := floatingips.Delete(client, instanceIP.ID).ExtractErr(); err != nil {
|
||||||
ui.Error(fmt.Sprintf(
|
ui.Error(fmt.Sprintf(
|
||||||
"Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
"Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
|
||||||
|
|
Loading…
Reference in New Issue