Merge "Add get_by_cell_and_project() method to InstanceMappingList"
This commit is contained in:
commit
9e767106a0
@ -249,3 +249,19 @@ class InstanceMappingList(base.ObjectListBase, base.NovaObject):
|
||||
@classmethod
|
||||
def destroy_bulk(cls, context, instance_uuids):
|
||||
return cls._destroy_bulk_in_db(context, instance_uuids)
|
||||
|
||||
@staticmethod
|
||||
@db_api.api_context_manager.reader
|
||||
def _get_by_cell_and_project_from_db(context, cell_id, project_id):
|
||||
return (context.session.query(api_models.InstanceMapping)
|
||||
.options(joinedload('cell_mapping'))
|
||||
.filter_by(cell_id=cell_id)
|
||||
.filter_by(project_id=project_id)
|
||||
.all())
|
||||
|
||||
@classmethod
|
||||
def get_by_cell_and_project(cls, context, cell_id, project_id):
|
||||
db_mappings = cls._get_by_cell_and_project_from_db(context, cell_id,
|
||||
project_id)
|
||||
return base.obj_make_list(context, cls(), objects.InstanceMapping,
|
||||
db_mappings)
|
||||
|
@ -255,3 +255,40 @@ class InstanceMappingListTestCase(test.NoDBTestCase):
|
||||
self.context, uuids + [uuidsentinel.deleted_instance])
|
||||
self.assertEqual(sorted(uuids),
|
||||
sorted([m.instance_uuid for m in mappings]))
|
||||
|
||||
def test_get_by_cell_and_project(self):
|
||||
cells = []
|
||||
# Create two cells
|
||||
for uuid in (uuidsentinel.cell1, uuidsentinel.cell2):
|
||||
cm = cell_mapping.CellMapping(context=self.context, uuid=uuid,
|
||||
database_connection="fake:///",
|
||||
transport_url='fake://')
|
||||
cm.create()
|
||||
cells.append(cm)
|
||||
# With each cell having two instance_mappings of two project_ids.
|
||||
uuids = {cells[0].id: [uuidsentinel.c1i1, uuidsentinel.c1i2],
|
||||
cells[1].id: [uuidsentinel.c2i1, uuidsentinel.c2i2]}
|
||||
project_ids = ['fake-project-1', 'fake-project-2']
|
||||
for cell_id, uuid in uuids.items():
|
||||
instance_mapping.InstanceMapping._create_in_db(
|
||||
self.context,
|
||||
{'project_id': project_ids[0],
|
||||
'cell_id': cell_id,
|
||||
'instance_uuid': uuid[0]})
|
||||
instance_mapping.InstanceMapping._create_in_db(
|
||||
self.context,
|
||||
{'project_id': project_ids[1],
|
||||
'cell_id': cell_id,
|
||||
'instance_uuid': uuid[1]})
|
||||
|
||||
ims = instance_mapping.InstanceMappingList.get_by_cell_and_project(
|
||||
self.context, cells[0].id, 'fake-project-2')
|
||||
self.assertEqual([uuidsentinel.c1i2],
|
||||
sorted([m.instance_uuid for m in ims]))
|
||||
ims = instance_mapping.InstanceMappingList.get_by_cell_and_project(
|
||||
self.context, cells[1].id, 'fake-project-1')
|
||||
self.assertEqual([uuidsentinel.c2i1],
|
||||
sorted([m.instance_uuid for m in ims]))
|
||||
ims = instance_mapping.InstanceMappingList.get_by_cell_and_project(
|
||||
self.context, cells[0].id, 'fake-project-3')
|
||||
self.assertEqual([], sorted([m.instance_uuid for m in ims]))
|
||||
|
Loading…
Reference in New Issue
Block a user