def create_floating_ip(neutron, keystone, ext_net_name, port_id=None): """ Returns the floating IP object that was created with this call :param neutron: the Neutron client :param keystone: the Keystone client :param ext_net_name: the name of the external network on which to apply the floating IP address :param port_id: the ID of the port to which the floating IP will be associated :return: the SNAPS FloatingIp object """ logger.info('Creating floating ip to external network - ' + ext_net_name) ext_net = get_network(neutron, keystone, network_name=ext_net_name) if ext_net: body = {'floatingip': {'floating_network_id': ext_net.id}} if port_id: body['floatingip']['port_id'] = port_id fip = neutron.create_floatingip(body=body) return FloatingIp(id=fip['floatingip']['id'], ip=fip['floatingip']['floating_ip_address']) else: raise NeutronException( 'Cannot create floating IP, external network not found')
def get_floating_ips(neutron): """ Returns a list of all of the floating IPs :param neutron: the Neutron client """ out = list() fips = neutron.list_floatingips() for fip in fips['floatingips']: out.append(FloatingIp(**fip)) return out
def get_floating_ip(neutron, floating_ip): """ Returns a floating IP object that should be identical to the floating_ip parameter :param neutron: the Neutron client :param floating_ip: the SNAPS FloatingIp object :return: hopefully the same floating IP object input """ logger.debug('Attempting to retrieve existing floating ip with IP - %s', floating_ip.ip) os_fip = __get_os_floating_ip(neutron, floating_ip) if os_fip: return FloatingIp(id=os_fip['id'], ip=os_fip['floating_ip_address'])
def test_construction_named_ip_proj(self): vm_inst = FloatingIp( id='foo', description='bar', ip='192.168.122.3', fixed_ip_address='10.0.0.3', floating_network_id='id_of_net', port_id='id_of_port', router_id='id_of_router', project_id='id_of_proj') self.assertEqual('foo', vm_inst.id) self.assertEqual('bar', vm_inst.description) self.assertEqual('192.168.122.3', vm_inst.ip) self.assertEqual('10.0.0.3', vm_inst.fixed_ip_address) self.assertEqual('id_of_net', vm_inst.floating_network_id) self.assertEqual('id_of_port', vm_inst.port_id) self.assertEqual('id_of_router', vm_inst.router_id) self.assertEqual('id_of_proj', vm_inst.project_id)
def test_construction_kwargs_ip_proj(self): kwargs = {'id': 'foo', 'description': 'bar', 'ip': '192.168.122.3', 'fixed_ip_address': '10.0.0.3', 'floating_network_id': 'id_of_net', 'port_id': 'id_of_port', 'router_id': 'id_of_router', 'project_id': 'id_of_proj'} vm_inst = FloatingIp(**kwargs) self.assertEqual('foo', vm_inst.id) self.assertEqual('bar', vm_inst.description) self.assertEqual('192.168.122.3', vm_inst.ip) self.assertEqual('10.0.0.3', vm_inst.fixed_ip_address) self.assertEqual('id_of_net', vm_inst.floating_network_id) self.assertEqual('id_of_port', vm_inst.port_id) self.assertEqual('id_of_router', vm_inst.router_id) self.assertEqual('id_of_proj', vm_inst.project_id)
def get_port_floating_ips(neutron, ports): """ Returns all of the floating IPs associated with the ports returned in a list of tuples where the port object is in the first position and the floating IP object is in the second :param neutron: the Neutron client :param ports: a list of tuple 2 where index 0 is the port name and index 1 is the SNAPS-OO Port object :return: a list of tuple 2 (port_id, SNAPS FloatingIp) objects when ports is not None else a list of FloatingIp objects """ out = list() fips = neutron.list_floatingips() for fip in fips['floatingips']: for port_name, port in ports: if port and port.id == fip['port_id']: out.append((port.id, FloatingIp(**fip))) break return out