From 34144101323132a3552474f4a9ae5163f2d5be76 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Wed, 6 Sep 2017 08:59:13 -0700 Subject: [PATCH] Add ComputeNodeList.get_by_hypervisor_type() This lets us pull ComputeNode objects from the DB by hypervisor type. Note that this is not remotable, so no version bump needed until/unless we make it so. Change-Id: I36f7b3386510a64df77b61409f3b4e6530ed3f90 --- nova/objects/compute_node.py | 13 +++++++++++++ nova/tests/functional/db/test_compute_node.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/nova/objects/compute_node.py b/nova/objects/compute_node.py index 339b331c85a9..742c7bdb3cf4 100644 --- a/nova/objects/compute_node.py +++ b/nova/objects/compute_node.py @@ -451,3 +451,16 @@ class ComputeNodeList(base.ObjectListBase, base.NovaObject): compute_uuids) return base.obj_make_list(context, cls(context), objects.ComputeNode, db_computes) + + @staticmethod + @db.select_db_reader_mode + def _db_compute_node_get_by_hv_type(context, hv_type): + db_computes = context.session.query(models.ComputeNode).filter( + models.ComputeNode.hypervisor_type == hv_type).all() + return db_computes + + @classmethod + def get_by_hypervisor_type(cls, context, hv_type): + db_computes = cls._db_compute_node_get_by_hv_type(context, hv_type) + return base.obj_make_list(context, cls(context), objects.ComputeNode, + db_computes) diff --git a/nova/tests/functional/db/test_compute_node.py b/nova/tests/functional/db/test_compute_node.py index b1b07353e2e8..dfa88ab64373 100644 --- a/nova/tests/functional/db/test_compute_node.py +++ b/nova/tests/functional/db/test_compute_node.py @@ -106,3 +106,20 @@ class ComputeNodeTestCase(test.TestCase): [cn1.uuid, cn2.uuid, uuidsentinel.noexists]) self.assertEqual(2, len(cns)) + + def test_get_by_hypervisor_type(self): + cn1 = fake_compute_obj.obj_clone() + cn1._context = self.context + cn1.hypervisor_type = 'ironic' + cn1.create() + + cn2 = fake_compute_obj.obj_clone() + cn2._context = self.context + cn2.hypervisor_type = 'libvirt' + cn2.host += '-alt' + cn2.create() + + cns = objects.ComputeNodeList.get_by_hypervisor_type(self.context, + 'ironic') + self.assertEqual(1, len(cns)) + self.assertEqual(cn1.uuid, cns[0].uuid)