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:
chbell43 2018-10-29 18:36:08 +00:00
parent ea78cb62fd
commit 653fa2298f
1 changed files with 30 additions and 24 deletions

View File

@ -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,22 +40,13 @@ 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 != "" {
networkClient, err = config.networkV2Client()
if err != nil { if err != nil {
err = fmt.Errorf("Error initializing network client: %s", err) err = fmt.Errorf("Error initializing network client: %s", err)
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt 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:
@ -146,12 +147,16 @@ 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 {
@ -160,6 +165,7 @@ func (s *StepAllocateIp) Cleanup(state multistep.StateBag) {
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))