From 61468af1241a838bfa4b3436c6cbf59411a598a6 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Tue, 30 Oct 2018 15:33:05 +1100 Subject: [PATCH] Make delete_unattached_floating_ips return a count This modifies delete_unattached_floating_ips to return either a count of how many floating IPs were cleaned, or False if none were cleaned. Although this is an API change, from searching both codesearch and google it seems very likely that nodepool only user of this. However, since a positive integer value will evaluate True anyway extant code should continue to work. The neturon test-case is updated with an extra floating-ip to make sure the count works. The nova test-case is updated to ensure it returns False to indicate nothing was done. Sort-of-Needed-By: https://review.openstack.org/614074 Change-Id: I7bd709bd83f352c58203c767779acbe66ecfc10e --- openstack/cloud/openstackcloud.py | 4 ++-- .../unit/cloud/test_floating_ip_neutron.py | 24 +++++++++++++++++-- .../tests/unit/cloud/test_floating_ip_nova.py | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/openstack/cloud/openstackcloud.py b/openstack/cloud/openstackcloud.py index f267b99ab..d912ce8a0 100755 --- a/openstack/cloud/openstackcloud.py +++ b/openstack/cloud/openstackcloud.py @@ -6163,7 +6163,7 @@ class _OpenStackCloudMixin(_normalize.Normalizer): A value of 0 will also cause no checking of results to occur. - :returns: True if Floating IPs have been deleted, False if not + :returns: Number of Floating IPs deleted, False if none :raises: ``OpenStackCloudException``, on operation error. """ @@ -6173,7 +6173,7 @@ class _OpenStackCloudMixin(_normalize.Normalizer): if not ip['attached']: processed.append(self.delete_floating_ip( floating_ip_id=ip['id'], retry=retry)) - return all(processed) if processed else False + return len(processed) if all(processed) else False def _attach_ip_to_server( self, server, floating_ip, diff --git a/openstack/tests/unit/cloud/test_floating_ip_neutron.py b/openstack/tests/unit/cloud/test_floating_ip_neutron.py index bf4871f2c..94c06bdc0 100644 --- a/openstack/tests/unit/cloud/test_floating_ip_neutron.py +++ b/openstack/tests/unit/cloud/test_floating_ip_neutron.py @@ -928,6 +928,14 @@ class TestFloatingIP(base.TestCase): "network": "this-is-a-net-or-pool-id", "port_id": None, "status": "ACTIVE" + }, { + "id": "this-is-a-second-floating-ip-id", + "fixed_ip_address": None, + "internal_network": None, + "floating_ip_address": "203.0.113.30", + "network": "this-is-a-net-or-pool-id", + "port_id": None, + "status": "ACTIVE" }, { "id": "this-is-an-attached-floating-ip-id", "fixed_ip_address": None, @@ -949,12 +957,24 @@ class TestFloatingIP(base.TestCase): append=['v2.0', 'floatingips/{0}.json'.format( floating_ips[0]['id'])]), json={}), + # First IP has been deleted now, return just the second dict(method='GET', uri=self.get_mock_url( 'network', 'public', append=['v2.0', 'floatingips.json']), - json={'floatingips': [floating_ips[1]]}), + json={'floatingips': floating_ips[1:]}), + dict(method='DELETE', + uri=self.get_mock_url( + 'network', 'public', + append=['v2.0', 'floatingips/{0}.json'.format( + floating_ips[1]['id'])]), + json={}), + dict(method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'floatingips.json']), + json={'floatingips': [floating_ips[2]]}), ]) - self.cloud.delete_unattached_floating_ips() + cleaned_up = self.cloud.delete_unattached_floating_ips() + self.assertEqual(cleaned_up, 2) self.assert_calls() def test_create_floating_ip_no_port(self): diff --git a/openstack/tests/unit/cloud/test_floating_ip_nova.py b/openstack/tests/unit/cloud/test_floating_ip_nova.py index 1ca9a68a3..7d49e19d8 100644 --- a/openstack/tests/unit/cloud/test_floating_ip_nova.py +++ b/openstack/tests/unit/cloud/test_floating_ip_nova.py @@ -318,4 +318,4 @@ class TestFloatingIP(base.TestCase): def test_cleanup_floating_ips(self): # This should not call anything because it's unsafe on nova. - self.cloud.delete_unattached_floating_ips() + self.assertFalse(self.cloud.delete_unattached_floating_ips())