Add get_by_instance_uuids() to InstanceMappingList

This provides a bulk lookup for things that need it, such as the
server_groups api module.

Related to blueprint cells-aware-api

Change-Id: I8bd5c9611b7ba076116aac50f4c9bfea6fb41034
This commit is contained in:
Dan Smith 2017-03-08 11:02:27 -08:00
parent bff9aa2801
commit 38a0231065
3 changed files with 30 additions and 2 deletions

View File

@ -136,7 +136,8 @@ class InstanceMapping(base.NovaTimestampObject, base.NovaObject):
class InstanceMappingList(base.ObjectListBase, base.NovaObject):
# Version 1.0: Initial version
# Version 1.1: Added get_by_cell_id method.
VERSION = '1.1'
# Version 1.2: Added get_by_instance_uuids method
VERSION = '1.2'
fields = {
'objects': fields.ListOfObjectsField('InstanceMapping'),
@ -169,3 +170,17 @@ class InstanceMappingList(base.ObjectListBase, base.NovaObject):
db_mappings = cls._get_by_cell_id_from_db(context, cell_id)
return base.obj_make_list(context, cls(), objects.InstanceMapping,
db_mappings)
@staticmethod
@db_api.api_context_manager.reader
def _get_by_instance_uuids_from_db(context, uuids):
return (context.session.query(api_models.InstanceMapping)
.options(joinedload('cell_mapping'))
.filter(api_models.InstanceMapping.instance_uuid.in_(uuids))
.all())
@base.remotable_classmethod
def get_by_instance_uuids(cls, context, uuids):
db_mappings = cls._get_by_instance_uuids_from_db(context, uuids)
return base.obj_make_list(context, cls(), objects.InstanceMapping,
db_mappings)

View File

@ -18,6 +18,7 @@ from nova.objects import cell_mapping
from nova.objects import instance_mapping
from nova import test
from nova.tests import fixtures
from nova.tests import uuidsentinel
sample_mapping = {'instance_uuid': '',
@ -170,3 +171,15 @@ class InstanceMappingListTestCase(test.NoDBTestCase):
)
self.assertEqual(1, len(inst_mapping_list))
self.assertEqual(db_inst_mapping1['id'], inst_mapping_list[0].id)
def test_instance_mapping_get_by_instance_uuids(self):
db_inst_mapping1 = create_mapping()
db_inst_mapping2 = create_mapping(cell_id=None)
# Create a third that we won't include
create_mapping()
uuids = [db_inst_mapping1.instance_uuid,
db_inst_mapping2.instance_uuid]
mappings = instance_mapping.InstanceMappingList.get_by_instance_uuids(
self.context, uuids + [uuidsentinel.deleted_instance])
self.assertEqual(sorted(uuids),
sorted([m.instance_uuid for m in mappings]))

View File

@ -1111,7 +1111,7 @@ object_data = {
'InstanceInfoCache': '1.5-cd8b96fefe0fc8d4d337243ba0bf0e1e',
'InstanceList': '2.2-ff71772c7bf6d72f6ef6eee0199fb1c9',
'InstanceMapping': '1.0-65de80c491f54d19374703c0753c4d47',
'InstanceMappingList': '1.1-14d4a4296c3cbf51e660b657dc37b19e',
'InstanceMappingList': '1.2-ee638619aa3d8a82a59c0c83bfa64d78',
'InstanceNUMACell': '1.4-7c1eb9a198dee076b4de0840e45f4f55',
'InstanceNUMATopology': '1.3-ec0030cb0402a49c96da7051c037082a',
'InstancePCIRequest': '1.1-b1d75ebc716cb12906d9d513890092bf',