From aab68ced6a84c9fa39adb2cb9bb19165e699ff3a Mon Sep 17 00:00:00 2001 From: David J Peacock Date: Fri, 13 Sep 2019 10:08:02 -0400 Subject: [PATCH] Update get_hosts to use available API os-hosts was deprecated in nova quite some time ago. This patch updates get_hosts to use the available os-hypervisors functions. Change-Id: I36421e9859a266f0278c1b5f2acb4ebbacbfca82 Story: 2006584 Task: 36708 bz: 1740567 --- heat/engine/clients/os/nova.py | 19 +++++++--------- heat/tests/clients/test_nova_client.py | 31 +++++++++++--------------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/heat/engine/clients/os/nova.py b/heat/engine/clients/os/nova.py index 311eb713f1..c634388ad9 100644 --- a/heat/engine/clients/os/nova.py +++ b/heat/engine/clients/os/nova.py @@ -281,20 +281,15 @@ class NovaClientPlugin(microversion_mixin.MicroversionMixin, return flavor - def get_host(self, host_name): - """Get the host id specified by name. + def get_host(self, hypervisor_hostname): + """Gets list of matching hypervisors by specified name. - :param host_name: the name of host to find - :returns: the list of match hosts - :raises exception.EntityNotFound: + :param hypervisor_hostname: the name of host to find + :returns: list of matching hypervisor hosts + :raises nova client exceptions.NotFound: """ - host_list = self.client().hosts.list() - for host in host_list: - if host.host_name == host_name and host.service == self.COMPUTE: - return host - - raise exception.EntityNotFound(entity='Host', name=host_name) + return self.client().hypervisors.search(hypervisor_hostname) def get_keypair(self, key_name): """Get the public key specified by :key_name: @@ -872,4 +867,6 @@ class FlavorConstraint(NovaBaseConstraint): class HostConstraint(NovaBaseConstraint): + expected_exceptions = (exceptions.NotFound,) + resource_getter_name = 'get_host' diff --git a/heat/tests/clients/test_nova_client.py b/heat/tests/clients/test_nova_client.py index 35e37cc61d..e78f82667a 100644 --- a/heat/tests/clients/test_nova_client.py +++ b/heat/tests/clients/test_nova_client.py @@ -118,28 +118,21 @@ class NovaClientPluginTest(NovaClientPluginTestCase): def test_get_host(self): """Tests the get_host function.""" - my_host_name = 'myhost' + my_hypervisor_hostname = 'myhost' my_host = mock.MagicMock() - my_host.host_name = my_host_name - my_host.service = 'compute' + my_host.hypervisor_hostname = my_hypervisor_hostname - wrong_host = mock.MagicMock() - wrong_host.host_name = 'wrong_host' - wrong_host.service = 'compute' - self.nova_client.hosts.list.side_effect = [ - [my_host], - [wrong_host], - exception.EntityNotFound(entity='Host', name='nohost') + self.nova_client.hypervisors.search.side_effect = [ + my_host, nova_exceptions.NotFound(404) ] - self.assertEqual(my_host, self.nova_plugin.get_host(my_host_name)) - self.assertRaises(exception.EntityNotFound, - self.nova_plugin.get_host, my_host_name) - self.assertRaises(exception.EntityNotFound, + self.assertEqual(my_host, + self.nova_plugin.get_host(my_hypervisor_hostname)) + self.assertRaises(nova_exceptions.NotFound, self.nova_plugin.get_host, 'nohost') - self.assertEqual(3, self.nova_client.hosts.list.call_count) - calls = [mock.call(), mock.call(), mock.call()] + self.assertEqual(2, self.nova_client.hypervisors.search.call_count) + calls = [mock.call('myhost'), mock.call('nohost')] self.assertEqual(calls, - self.nova_client.hosts.list.call_args_list) + self.nova_client.hypervisors.search.call_args_list) def test_get_keypair(self): """Tests the get_keypair function.""" @@ -558,7 +551,9 @@ class HostConstraintTest(common.HeatTestCase): def test_validation_error(self): self.mock_get_host.side_effect = exception.EntityNotFound( entity='Host', name='bar') - self.assertFalse(self.constraint.validate("bar", self.ctx)) + self.assertRaises( + exception.EntityNotFound, + self.constraint.validate, "bar", self.ctx) class KeypairConstraintTest(common.HeatTestCase):