From fbfc7ac3c4de82e8555699fd045e4253972d14de Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Mon, 1 May 2017 12:36:02 -0400 Subject: [PATCH] objects: add ComputeNode.get_by_uuid method The HostAPI is going to use this to uniquely identify a host across cells. Depends-On: I940979d85fc105c9733ae19c406e85debe8fdffb Part of blueprint service-hyper-uuid-in-api Change-Id: I21f4b18e4554e9e912496e623461129196c72852 --- nova/objects/compute_node.py | 11 +++++++++- nova/tests/unit/objects/test_compute_node.py | 21 ++++++++++++++++++++ nova/tests/unit/objects/test_objects.py | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/nova/objects/compute_node.py b/nova/objects/compute_node.py index 25336323c9cd..339b331c85a9 100644 --- a/nova/objects/compute_node.py +++ b/nova/objects/compute_node.py @@ -49,7 +49,8 @@ class ComputeNode(base.NovaPersistentObject, base.NovaObject): # Version 1.15: Added uuid # Version 1.16: Added disk_allocation_ratio # Version 1.17: Added mapped - VERSION = '1.17' + # Version 1.18: Added get_by_uuid(). + VERSION = '1.18' fields = { 'id': fields.IntegerField(read_only=True), @@ -239,6 +240,14 @@ class ComputeNode(base.NovaPersistentObject, base.NovaObject): db_compute = db.compute_node_get(context, compute_id) return cls._from_db_object(context, cls(), db_compute) + @base.remotable_classmethod + def get_by_uuid(cls, context, compute_uuid): + nodes = ComputeNodeList.get_all_by_uuids(context, [compute_uuid]) + # We have a unique index on the uuid column so we can get back 0 or 1. + if not nodes: + raise exception.ComputeHostNotFound(host=compute_uuid) + return nodes[0] + # NOTE(hanlind): This is deprecated and should be removed on the next # major version bump @base.remotable_classmethod diff --git a/nova/tests/unit/objects/test_compute_node.py b/nova/tests/unit/objects/test_compute_node.py index 71589587f86d..6d7c4d920227 100644 --- a/nova/tests/unit/objects/test_compute_node.py +++ b/nova/tests/unit/objects/test_compute_node.py @@ -164,6 +164,27 @@ class _TestComputeNodeObject(object): self.assertNotIn('uuid', compute.obj_what_changed()) get_mock.assert_called_once_with(self.context, 123) + @mock.patch.object(compute_node.ComputeNodeList, 'get_all_by_uuids') + def test_get_by_uuid(self, get_all_by_uuids): + fake_node = copy.copy(fake_compute_node) + fake_node['stats'] = None + get_all_by_uuids.return_value = objects.ComputeNodeList( + objects=[objects.ComputeNode(**fake_node)]) + compute = compute_node.ComputeNode.get_by_uuid( + self.context, uuidsentinel.fake_compute_node) + self.assertEqual(uuidsentinel.fake_compute_node, compute.uuid) + get_all_by_uuids.assert_called_once_with( + self.context, [uuidsentinel.fake_compute_node]) + + @mock.patch.object(compute_node.ComputeNodeList, 'get_all_by_uuids') + def test_get_by_uuid_not_found(self, get_all_by_uuids): + get_all_by_uuids.return_value = objects.ComputeNodeList() + self.assertRaises(exception.ComputeHostNotFound, + compute_node.ComputeNode.get_by_uuid, + self.context, uuidsentinel.fake_compute_node) + get_all_by_uuids.assert_called_once_with( + self.context, [uuidsentinel.fake_compute_node]) + @mock.patch.object(db, 'compute_node_get') def test_get_without_mapped(self, get_mock): fake_node = copy.copy(fake_compute_node) diff --git a/nova/tests/unit/objects/test_objects.py b/nova/tests/unit/objects/test_objects.py index 45e9e121ec37..30cefabd2066 100644 --- a/nova/tests/unit/objects/test_objects.py +++ b/nova/tests/unit/objects/test_objects.py @@ -1072,7 +1072,7 @@ object_data = { 'BuildRequestList': '1.0-cd95608eccb89fbc702c8b52f38ec738', 'CellMapping': '1.0-7f1a7e85a22bbb7559fc730ab658b9bd', 'CellMappingList': '1.0-4ee0d9efdfd681fed822da88376e04d2', - 'ComputeNode': '1.17-abc222ef5a707dcd3e3d32b48197c9e7', + 'ComputeNode': '1.18-431fafd8ac4a5f3559bd9b1f1332cc22', 'ComputeNodeList': '1.17-52f3b0962b1c86b98590144463ebb192', 'DNSDomain': '1.0-7b0b2dab778454b6a7b6c66afe163a1a', 'DNSDomainList': '1.0-4ee0d9efdfd681fed822da88376e04d2',