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
This commit is contained in:
Matt Riedemann 2017-05-01 12:36:02 -04:00
parent 6e73eb4d0d
commit fbfc7ac3c4
3 changed files with 32 additions and 2 deletions

View File

@ -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

View File

@ -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)

View File

@ -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',