Add InstanceMappingList.get_by_cell_id

This adds the InstanceMappingList.get_by_cell_id method which will
provide a way to list InstanceMappings by a given CellMapping.id. This
is going to be used later in a "nova-manage cell_v2 delete_cell"
command.

Change-Id: I98a22161b28f2de32760c578713be27766e6809e
Related-Bug: #1656691
This commit is contained in:
Matt Riedemann 2017-01-15 15:55:15 -05:00
parent 8677aca654
commit 90cd8acf91
3 changed files with 39 additions and 2 deletions

View File

@ -135,7 +135,8 @@ class InstanceMapping(base.NovaTimestampObject, base.NovaObject):
@base.NovaObjectRegistry.register
class InstanceMappingList(base.ObjectListBase, base.NovaObject):
# Version 1.0: Initial version
VERSION = '1.0'
# Version 1.1: Added get_by_cell_id method.
VERSION = '1.1'
fields = {
'objects': fields.ListOfObjectsField('InstanceMapping'),
@ -155,3 +156,16 @@ class InstanceMappingList(base.ObjectListBase, base.NovaObject):
return base.obj_make_list(context, cls(), objects.InstanceMapping,
db_mappings)
@staticmethod
@db_api.api_context_manager.reader
def _get_by_cell_id_from_db(context, cell_id):
return (context.session.query(api_models.InstanceMapping)
.options(joinedload('cell_mapping'))
.filter(api_models.InstanceMapping.cell_id == cell_id)).all()
@base.remotable_classmethod
def get_by_cell_id(cls, context, cell_id):
db_mappings = cls._get_by_cell_id_from_db(context, cell_id)
return base.obj_make_list(context, cls(), objects.InstanceMapping,
db_mappings)

View File

@ -147,3 +147,26 @@ class InstanceMappingListTestCase(test.NoDBTestCase):
mapping = mappings[db_mapping.instance_uuid]
for key in instance_mapping.InstanceMapping.fields.keys():
self.assertEqual(db_mapping[key], mapping[key])
def test_instance_mapping_list_get_by_cell_id(self):
"""Tests getting all of the InstanceMappings for a given CellMapping id
"""
# we shouldn't have any instance mappings yet
inst_mapping_list = (
instance_mapping.InstanceMappingList.get_by_cell_id(
self.context, sample_cell_mapping['id'])
)
self.assertEqual(0, len(inst_mapping_list))
# now create an instance mapping in a cell
db_inst_mapping1 = create_mapping()
# let's also create an instance mapping that's not in a cell to make
# sure our filtering is working
db_inst_mapping2 = create_mapping(cell_id=None)
self.assertIsNone(db_inst_mapping2['cell_id'])
# now we should list out one instance mapping for the cell
inst_mapping_list = (
instance_mapping.InstanceMappingList.get_by_cell_id(
self.context, db_inst_mapping1['cell_id'])
)
self.assertEqual(1, len(inst_mapping_list))
self.assertEqual(db_inst_mapping1['id'], inst_mapping_list[0].id)

View File

@ -1111,7 +1111,7 @@ object_data = {
'InstanceInfoCache': '1.5-cd8b96fefe0fc8d4d337243ba0bf0e1e',
'InstanceList': '2.2-ff71772c7bf6d72f6ef6eee0199fb1c9',
'InstanceMapping': '1.0-65de80c491f54d19374703c0753c4d47',
'InstanceMappingList': '1.0-9e982e3de1613b9ada85e35f69b23d47',
'InstanceMappingList': '1.1-14d4a4296c3cbf51e660b657dc37b19e',
'InstanceNUMACell': '1.3-6991a20992c5faa57fae71a45b40241b',
'InstanceNUMATopology': '1.3-ec0030cb0402a49c96da7051c037082a',
'InstancePCIRequest': '1.1-b1d75ebc716cb12906d9d513890092bf',