From ea78cb62fd8d4e9e94bd8861b85c979246a50062 Mon Sep 17 00:00:00 2001 From: chbell43 Date: Fri, 26 Oct 2018 19:28:42 +0000 Subject: [PATCH] Make the config.networkV2Client conditional In cases where the OpenStack environment does not contain a v2 network, the builder will error out. We only need the networkV2Client when using floating IPs. This will allow packer to work in environments where a v2 network is not available. --- builder/openstack/step_allocate_ip.go | 32 +++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/builder/openstack/step_allocate_ip.go b/builder/openstack/step_allocate_ip.go index 74bf63b75..cd15afa35 100644 --- a/builder/openstack/step_allocate_ip.go +++ b/builder/openstack/step_allocate_ip.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" "github.com/hashicorp/packer/helper/multistep" @@ -29,12 +30,15 @@ func (s *StepAllocateIp) Run(_ context.Context, state multistep.StateBag) multis return multistep.ActionHalt } - // We need the v2 network client - networkClient, err := config.networkV2Client() - if err != nil { - err = fmt.Errorf("Error initializing network client: %s", err) - state.Put("error", err) - return multistep.ActionHalt + // We might need the v2 network client + var networkClient *gophercloud.ServiceClient + if s.FloatingIP != "" || s.ReuseIPs || s.FloatingIPNetwork != "" { + networkClient, err = config.networkV2Client() + if err != nil { + err = fmt.Errorf("Error initializing network client: %s", err) + state.Put("error", err) + return multistep.ActionHalt + } } var instanceIP floatingips.FloatingIP @@ -147,15 +151,15 @@ func (s *StepAllocateIp) Cleanup(state multistep.StateBag) { return } - // We need the v2 network client - client, err := config.networkV2Client() - if err != nil { - ui.Error(fmt.Sprintf( - "Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP)) - return - } + if instanceIP.FloatingIP != "" && instanceIP.ID != "" { + // We need the v2 network client + client, err := config.networkV2Client() + if err != nil { + ui.Error(fmt.Sprintf( + "Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP)) + return + } - if instanceIP.ID != "" { if err := floatingips.Delete(client, instanceIP.ID).ExtractErr(); err != nil { ui.Error(fmt.Sprintf( "Error deleting temporary floating IP '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))