From 983cc997ffc0a4cfaab65fc3dc0e6c1f9b26a321 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 9 Jul 2019 09:19:02 -0400 Subject: [PATCH] Return empty lists for resources if neutron doesn't exist It's the general design of the cloud layer to return empty resources rather than throw exceptions when a user tries to fetch a non-existent resource. Change-Id: I1c6ce28af767f466f349b51a47abe27c0dcdd3fb --- openstack/cloud/_network.py | 12 ++++++++++ openstack/tests/unit/cloud/test_shade.py | 24 ++++--------------- ...resources-empty-list-6aa760c01e7d97d7.yaml | 5 ++++ 3 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 releasenotes/notes/list-network-resources-empty-list-6aa760c01e7d97d7.yaml diff --git a/openstack/cloud/_network.py b/openstack/cloud/_network.py index 3fd0e407e..9a58f34ad 100644 --- a/openstack/cloud/_network.py +++ b/openstack/cloud/_network.py @@ -123,6 +123,9 @@ class NetworkCloudMixin(_normalize.Normalizer): :returns: A list of ``munch.Munch`` containing network info. """ + # If the cloud is running nova-network, just return an empty list. + if not self.has_service('network'): + return [] # Translate None from search interface to empty {} for kwargs below if not filters: filters = {} @@ -136,6 +139,9 @@ class NetworkCloudMixin(_normalize.Normalizer): :returns: A list of router ``munch.Munch``. """ + # If the cloud is running nova-network, just return an empty list. + if not self.has_service('network'): + return [] # Translate None from search interface to empty {} for kwargs below if not filters: filters = {} @@ -152,6 +158,9 @@ class NetworkCloudMixin(_normalize.Normalizer): :returns: A list of subnet ``munch.Munch``. """ + # If the cloud is running nova-network, just return an empty list. + if not self.has_service('network'): + return [] # Translate None from search interface to empty {} for kwargs below if not filters: filters = {} @@ -192,6 +201,9 @@ class NetworkCloudMixin(_normalize.Normalizer): return _utils._filter_list(self._ports, None, filters or {}) def _list_ports(self, filters): + # If the cloud is running nova-network, just return an empty list. + if not self.has_service('network'): + return [] resp = self.network.get("/ports.json", params=filters) data = proxy._json_response( resp, diff --git a/openstack/tests/unit/cloud/test_shade.py b/openstack/tests/unit/cloud/test_shade.py index 3be490abd..d96b612b1 100644 --- a/openstack/tests/unit/cloud/test_shade.py +++ b/openstack/tests/unit/cloud/test_shade.py @@ -122,26 +122,10 @@ class TestShade(base.TestCase): self.assert_calls() - def test__neutron_exceptions_resource_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks.json']), - status_code=404) - ]) - self.assertRaises(exc.OpenStackCloudResourceNotFound, - self.cloud.list_networks) - self.assert_calls() - - def test__neutron_exceptions_url_not_found(self): - self.register_uris([ - dict(method='GET', - uri=self.get_mock_url( - 'network', 'public', append=['v2.0', 'networks.json']), - status_code=404) - ]) - self.assertRaises(exc.OpenStackCloudURINotFound, - self.cloud.list_networks) + def test_neutron_not_found(self): + self.use_nothing() + self.cloud.has_service = mock.Mock(return_value=False) + self.assertEqual([], self.cloud.list_networks()) self.assert_calls() def test_list_servers(self): diff --git a/releasenotes/notes/list-network-resources-empty-list-6aa760c01e7d97d7.yaml b/releasenotes/notes/list-network-resources-empty-list-6aa760c01e7d97d7.yaml new file mode 100644 index 000000000..2c7be2c72 --- /dev/null +++ b/releasenotes/notes/list-network-resources-empty-list-6aa760c01e7d97d7.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Basic networking list calls in the cloud layer been fixed to return + an empty list if neutron is not running.