OpenStack builder: do not always use floating IPs

Only associate floating IPs if user provided "floating_ip_network" or
"floating_ip".
Remove FindExternalNetwork helper method because it won't be used.
This commit is contained in:
Andrei Ozerov 2018-08-17 00:49:06 +03:00
parent 5d6ba4301d
commit 103403db48
2 changed files with 23 additions and 65 deletions

View File

@ -12,38 +12,6 @@ import (
"github.com/gophercloud/gophercloud/pagination"
)
// ExternalNetwork is a network with external router.
type ExternalNetwork struct {
networks.Network
external.NetworkExternalExt
}
// FindExternalNetwork returns existing network with external router.
// It will return first network if there are many.
func FindExternalNetwork(client *gophercloud.ServiceClient) (*ExternalNetwork, error) {
var externalNetworks []ExternalNetwork
allPages, err := networks.List(client, networks.ListOpts{
Status: "ACTIVE",
}).AllPages()
if err != nil {
return nil, err
}
// Extract external networks from found networks.
err = networks.ExtractNetworksInto(allPages, &externalNetworks)
if err != nil {
return nil, err
}
if len(externalNetworks) == 0 {
return nil, fmt.Errorf("no external networks found")
}
// Return the first external network.
return &externalNetworks[0], nil
}
// CheckFloatingIP gets a floating IP by its ID and checks if it is already
// associated with any internal interface.
// It returns floating IP if it can be used.
@ -114,18 +82,24 @@ func GetInstancePortID(client *gophercloud.ServiceClient, id string) (string, er
return interfaces[0].PortID, nil
}
// CheckExternalNetworkRef checks provided network reference and returns a valid
// CheckFloatingIPNetwork checks provided network reference and returns a valid
// Networking service ID.
func CheckExternalNetworkRef(client *gophercloud.ServiceClient, networkRef string) (string, error) {
func CheckFloatingIPNetwork(client *gophercloud.ServiceClient, networkRef string) (string, error) {
if _, err := uuid.Parse(networkRef); err != nil {
return GetExternalNetworkIDByName(client, networkRef)
return GetFloatingIPNetworkIDByName(client, networkRef)
}
return networkRef, nil
}
// GetExternalNetworkIDByName searches for the external network ID by the provided name.
func GetExternalNetworkIDByName(client *gophercloud.ServiceClient, networkName string) (string, error) {
// ExternalNetwork is a network with external router.
type ExternalNetwork struct {
networks.Network
external.NetworkExternalExt
}
// GetFloatingIPNetworkIDByName searches for the external network ID by the provided name.
func GetFloatingIPNetworkIDByName(client *gophercloud.ServiceClient, networkName string) (string, error) {
var externalNetworks []ExternalNetwork
allPages, err := networks.List(client, networks.ListOpts{

View File

@ -43,8 +43,9 @@ func (s *StepAllocateIp) Run(_ context.Context, state multistep.StateBag) multis
// statebag below, because it is requested by Cleanup()
state.Put("access_ip", &instanceIP)
// Try to use floating IP provided by the user or find a free floating IP.
// WRITE NEW COMMENT>
if s.FloatingIP != "" {
// Try to use FloatingIP if it was provided by the user.
freeFloatingIP, err := CheckFloatingIP(networkClient, s.FloatingIP)
if err != nil {
err := fmt.Errorf("Error using provided floating IP '%s': %s", s.FloatingIP, err)
@ -71,32 +72,15 @@ func (s *StepAllocateIp) Run(_ context.Context, state multistep.StateBag) multis
instanceIP = *freeFloatingIP
ui.Message(fmt.Sprintf("Selected floating IP: '%s' (%s)", instanceIP.ID, instanceIP.FloatingIP))
state.Put("floatingip_istemp", false)
}
// Create a new floating IP if it wasn't obtained in the previous step.
if instanceIP.ID == "" {
var floatingNetwork string
if s.FloatingIPNetwork != "" {
// Validate provided external network reference and get an ID.
floatingNetwork, err = CheckExternalNetworkRef(networkClient, s.FloatingIPNetwork)
if err != nil {
err := fmt.Errorf("Error using the provided floating_ip_network: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
} else {
// Search for the external network that can be used for the floating IPs.
ui.Say(fmt.Sprintf("Searching for the external network..."))
externalNetwork, err := FindExternalNetwork(networkClient)
if err != nil {
err := fmt.Errorf("Error searching the external network: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
floatingNetwork = externalNetwork.ID
} else if s.FloatingIPNetwork != "" {
// Lastly, if FloatingIPNetwork was provided by the user, we need to use it
// to allocate a new floating IP and associate it to the instance.
floatingNetwork, err := CheckFloatingIPNetwork(networkClient, s.FloatingIPNetwork)
if err != nil {
err := fmt.Errorf("Error using the provided floating_ip_network: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Creating floating IP using network %s ...", floatingNetwork))
@ -115,7 +99,7 @@ func (s *StepAllocateIp) Run(_ context.Context, state multistep.StateBag) multis
state.Put("floatingip_istemp", true)
}
// Assoctate a floating IP that was obtained in the previous steps.
// Assoctate a floating IP if it was obtained in the previous steps.
if instanceIP.ID != "" {
ui.Say(fmt.Sprintf("Associating floating IP '%s' (%s) with instance port...",
instanceIP.ID, instanceIP.FloatingIP))