Skip to content

Instantly share code, notes, and snippets.

@shin-
Created September 20, 2018 23:27
Show Gist options
  • Save shin-/84599a9ad3dbc0f0b7872ad259c60c42 to your computer and use it in GitHub Desktop.
Save shin-/84599a9ad3dbc0f0b7872ad259c60c42 to your computer and use it in GitHub Desktop.
diff --git a/drivers/openstack/client.go b/drivers/openstack/client.go
index 7710c2ff..dc08d29c 100644
--- a/drivers/openstack/client.go
+++ b/drivers/openstack/client.go
@@ -49,6 +49,7 @@ type Client interface {
AssignFloatingIP(d *Driver, floatingIP *FloatingIP) error
DeleteFloatingIP(d *Driver, floatingIP *FloatingIP) error
GetFloatingIPs(d *Driver) ([]FloatingIP, error)
+ GetFloatingIP(d *Driver, ip string) (*FloatingIP, error)
GetFloatingIPPoolID(d *Driver) (string, error)
GetInstancePortID(d *Driver) (string, error)
GetTenantID(d *Driver) (string, error)
@@ -409,7 +410,25 @@ func (c *GenericClient) GetFloatingIPs(d *Driver) ([]FloatingIP, error) {
if d.ComputeNetwork {
return c.getNovaNetworkFloatingIPs(d)
}
- return c.getNeutronNetworkFloatingIPs(d)
+ return c.getNeutronNetworkFloatingIPs(d, nil)
+}
+
+func (c *GenericClient) GetFloatingIP(d *Driver, ip string) (*FloatingIP, error) {
+ if d.ComputeNetwork {
+ return nil, fmt.Errorf("operation not supported for nova networks")
+ }
+ opts := &floatingips.ListOpts{
+ FloatingIP: ip,
+ }
+
+ ips, err := c.getNeutronNetworkFloatingIPs(d, opts)
+ if err != nil {
+ return nil, err
+ }
+ if len(ips) == 0 {
+ return nil, fmt.Errorf("no floating IP associated with address %q", ip)
+ }
+ return &ips[0], nil
}
func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIP, error) {
@@ -434,15 +453,23 @@ func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIP, erro
return ips, err
}
-func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIP, error) {
+func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver, opts *floatingips.ListOpts) ([]FloatingIP, error) {
log.Debug("Listing floating IPs", map[string]string{
"FloatingNetworkId": d.FloatingIpPoolId,
"TenantID": d.TenantId,
})
- pager := floatingips.List(c.Network, floatingips.ListOpts{
- FloatingNetworkID: d.FloatingIpPoolId,
- TenantID: d.TenantId,
- })
+
+ if opts != nil {
+ opts.FloatingNetworkID = d.FloatingIpPoolId
+ opts.TenantID = d.TenantId
+ } else {
+ opts = &floatingips.ListOpts{
+ FloatingNetworkID: d.FloatingIpPoolId,
+ TenantID: d.TenantId,
+ }
+ }
+
+ pager := floatingips.List(c.Network, *opts)
ips := []FloatingIP{}
err := pager.EachPage(func(page pagination.Page) (bool, error) {
diff --git a/drivers/openstack/openstack.go b/drivers/openstack/openstack.go
index 41e825ae..4377c394 100644
--- a/drivers/openstack/openstack.go
+++ b/drivers/openstack/openstack.go
@@ -454,31 +454,24 @@ func (d *Driver) Kill() error {
}
func (d *Driver) Remove() error {
-
- var floatingIP *FloatingIP
-
log.Debug("deleting instance...", map[string]string{"MachineId": d.MachineId})
log.Info("Deleting OpenStack instance...")
if err := d.resolveIds(); err != nil {
return err
}
- if ips, err := d.client.GetFloatingIPs(d); err != nil {
- return err
- } else {
- for _, xip := range ips {
- if xip.Ip == d.IPAddress {
- floatingIP = &xip
- _ = floatingIP
- break
- }
- }
- }
- log.Debug("Deleting Floating IP: ", map[string]string{"floatingIP": floatingIP.Ip})
- if floatingIP != nil {
- if err := d.client.DeleteFloatingIP(d, floatingIP); err != nil {
+ if !d.ComputeNetwork {
+ floatingIP, err := d.client.GetFloatingIP(d, d.IPAddress)
+ if err != nil {
return err
}
+
+ if floatingIP != nil {
+ log.Debug("Deleting Floating IP: ", map[string]string{"floatingIP": floatingIP.Ip})
+ if err := d.client.DeleteFloatingIP(d, floatingIP); err != nil {
+ return err
+ }
+ }
}
if err := d.initCompute(); err != nil {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment