Add functions to list and find floating IPs

Change-Id: I4615ab43c1a8e9b347f4f9c066eb9f0d63a4989a
This commit is contained in:
Federico Ressi 2021-11-16 11:19:49 +01:00
parent 0036ed08cd
commit 2544c180c8
3 changed files with 53 additions and 0 deletions

View File

@ -50,9 +50,11 @@ ServiceUnavailable = _client.ServiceUnavailable
NeutronClientException = _client.NeutronClientException
neutron_client = _client.neutron_client
get_neutron_client = _client.get_neutron_client
find_floating_ip = _client.find_floating_ip
find_subnet = _client.find_subnet
find_port = _client.find_port
list_ports = _client.list_ports
list_floating_ips = _client.list_floating_ips
get_port_extra_dhcp_opts = _client.get_port_extra_dhcp_opts
create_port = _client.create_port
update_port = _client.update_port

View File

@ -86,6 +86,19 @@ def get_neutron_client(session=None, shared=True, init_client=None,
_RAISE_ERROR = object()
def find_floating_ip(client=None, unique=False, default=_RAISE_ERROR,
**attributes):
"""Look for a port matching some property values"""
floating_ips = list_floating_ips(client=client, **attributes)
if default is _RAISE_ERROR or floating_ips:
if unique:
return floating_ips.unique
else:
return floating_ips.first
else:
return default
def find_port(client=None, unique=False, default=_RAISE_ERROR, **attributes):
"""Look for a port matching some property values"""
ports = list_ports(client=client, **attributes)
@ -115,6 +128,12 @@ def list_ports(client=None, **params):
return tobiko.select(ports)
def list_floating_ips(client=None, retrieve_all=True, **params):
floating_ips = neutron_client(client).list_floatingips(
retrieve_all=retrieve_all, **params)['floatingips']
return tobiko.select(floating_ips)
def get_port_extra_dhcp_opts(port_id, client=None, **params):
port = neutron_client(client).show_port(port_id, **params)['port']
return port['extra_dhcp_opts']

View File

@ -177,6 +177,38 @@ class PortTest(testtools.TestCase):
self.assertIn(port_address, cidr)
@keystone.skip_unless_has_keystone_credentials()
class FloatingIpTest(testtools.TestCase):
server = tobiko.required_fixture(stacks.CirrosServerStackFixture)
def test_list_floating_ip(self):
port_id = self.server.port_id
floating_ips = neutron.list_floating_ips()
floating_ip = floating_ips.with_items(port_id=port_id).unique
self.assertEqual(floating_ip['floating_ip_address'],
self.server.floating_ip_address)
def test_list_floating_ip_with_port_id(self):
port_id = self.server.port_id
floating_ip = neutron.list_floating_ips(port_id=port_id).unique
self.assertEqual(floating_ip['floating_ip_address'],
self.server.floating_ip_address)
def test_list_floating_ip_with_floating_ip_address(self):
floating_ip_address = self.server.floating_ip_address
floating_ip = neutron.list_floating_ips(
floating_ip_address=floating_ip_address).unique
self.assertEqual(floating_ip['port_id'],
self.server.port_id)
def test_find_floating_ip_with_port_id(self):
port_id = self.server.port_id
floating_ip = neutron.find_floating_ip(port_id=port_id, unique=True)
self.assertEqual(floating_ip['floating_ip_address'],
self.server.floating_ip_address)
@keystone.skip_unless_has_keystone_credentials()
class AgentTest(testtools.TestCase):