From 5783a4e183a83bb6348290495320044c3e26284c Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 2 Aug 2014 15:42:12 -0700 Subject: [PATCH] Find IPs on OpenStack clouds without floating-ips The floating-ip extension, while pretty ubiquitous, is not a foregone conclusion. Specifically, Rackspace, while also served by the rax module, is a valid OpenStack cloud and can be interacted with directly via nova interfaces. Add support for determining public and private IPs for OpenStack clouds that don't use floating ips by reading the public and private keys from the addresses dict. --- cloud/nova_compute | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/cloud/nova_compute b/cloud/nova_compute index 905295c1d21..fb74ce15e29 100644 --- a/cloud/nova_compute +++ b/cloud/nova_compute @@ -299,6 +299,19 @@ def _add_floating_ip(module, nova, server): return server +def _get_ips(addresses, ext_tag, key_name): + + ret = [] + for (k, v) in addresses.iteritems(): + if k == key_name: + ret.extend([addrs['addr'] for addrs in v]) + else: + for interface_spec in v: + if 'OS-EXT-IPS:type' in interface_spec and interface_spec['OS-EXT-IPS:type'] == ext_tag: + ret.append(interface_spec['addr']) + return ret + + def _create_server(module, nova): # issue an error early on and not launch the instance if module.params['floating_ip'] is not None: @@ -339,8 +352,9 @@ def _create_server(module, nova): if module.params['floating_ip'] is not None: server = _add_floating_ip(module, nova, server) - private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if 'OS-EXT-IPS:type' in x and x['OS-EXT-IPS:type'] == 'fixed'] - public = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if 'OS-EXT-IPS:type' in x and x['OS-EXT-IPS:type'] == 'floating'] + private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private') + public = _get_ips(getattr(server, 'addresses'), 'floating', 'public') + # now exit with info module.exit_json(changed = True, id = server.id, private_ip=''.join(private), public_ip=''.join(public), status = server.status, info = server._info) @@ -351,8 +365,8 @@ def _create_server(module, nova): module.fail_json(msg = "Timeout waiting for the server to come up.. Please check manually") if server.status == 'ERROR': module.fail_json(msg = "Error in creating the server.. Please check manually") - private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed'] - public = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating'] + private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private') + public = _get_ips(getattr(server, 'addresses'), 'floating', 'public') module.exit_json(changed = True, id = info['id'], private_ip=''.join(private), public_ip=''.join(public), status = server.status, info = server._info) @@ -373,8 +387,8 @@ def _get_server_state(module, nova): if server and module.params['state'] == 'present': if server.status != 'ACTIVE': module.fail_json( msg="The VM is available but not Active. state:" + server.status) - private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if 'OS-EXT-IPS:type' in x and x['OS-EXT-IPS:type'] == 'fixed'] - public = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if 'OS-EXT-IPS:type' in x and x['OS-EXT-IPS:type'] == 'floating'] + private = _get_ips(getattr(server, 'addresses'), 'fixed', 'private') + public = _get_ips(getattr(server, 'addresses'), 'floating', 'public') module.exit_json(changed = False, id = server.id, public_ip = ''.join(public), private_ip = ''.join(private), info = server._info) if server and module.params['state'] == 'absent': return True