From 2f32a84057a7562524cda9f224b13ba0a394d7ce Mon Sep 17 00:00:00 2001 From: spisarski Date: Thu, 14 Dec 2017 08:25:17 -0700 Subject: Added utility function to retrieve the names of all hosts with a hypervisor. Change-Id: I25b91c6eee78ebf35785c4884dded7c9ab2cc412 Signed-off-by: spisarski --- snaps/openstack/utils/nova_utils.py | 15 +++++++++++++++ snaps/openstack/utils/tests/nova_utils_tests.py | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py index 2d569e6..8297db9 100644 --- a/snaps/openstack/utils/nova_utils.py +++ b/snaps/openstack/utils/nova_utils.py @@ -475,6 +475,21 @@ def get_availability_zone_hosts(nova, zone_name='nova'): return out +def get_hypervisor_hosts(nova): + """ + Returns the host names of all nova nodes with active hypervisors + :param nova: the Nova client + :return: a list of hypervisor host names + """ + out = list() + hypervisors = nova.hypervisors.list() + for hypervisor in hypervisors: + if hypervisor.state == "up": + out.append(hypervisor.hypervisor_hostname) + + return out + + def delete_vm_instance(nova, vm_inst): """ Deletes a VM instance diff --git a/snaps/openstack/utils/tests/nova_utils_tests.py b/snaps/openstack/utils/tests/nova_utils_tests.py index c4bc9cb..c7be5ef 100644 --- a/snaps/openstack/utils/tests/nova_utils_tests.py +++ b/snaps/openstack/utils/tests/nova_utils_tests.py @@ -53,6 +53,16 @@ class NovaSmokeTests(OSComponentTestCase): # This should not throw an exception nova.flavors.list() + def test_nova_get_hypervisor_hosts(self): + """ + Tests to ensure that get_hypervisors() function works. + """ + nova = nova_utils.nova_client(self.os_creds) + + hosts = nova_utils.get_hypervisor_hosts(nova) + # This should not throw an exception + self.assertGreaterEqual(len(hosts), 1) + def test_nova_connect_fail(self): """ Tests to ensure that the improper credentials cannot connect. -- cgit 1.2.3-korg