Skip to content

Commit

Permalink
Add CellMappingList.get_by_disabled() query method
Browse files Browse the repository at this point in the history
This query methods helps us to query for either the enabled cells if
False is passed as the value to the disabled argument or the disabled
cells if True is passed as a value to the disabled argument in the call.

Related to blueprint cell-disable

Change-Id: I3e0e1da186e4b3531b42e699ebd466280702d588
  • Loading branch information
tssurya committed Mar 21, 2018
1 parent 9377127 commit a26ad9f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
21 changes: 20 additions & 1 deletion nova/objects/cell_mapping.py
Expand Up @@ -12,6 +12,8 @@

from oslo_utils import versionutils
from sqlalchemy.sql.expression import asc
from sqlalchemy.sql import false
from sqlalchemy.sql import true

from nova.db.sqlalchemy import api as db_api
from nova.db.sqlalchemy import api_models
Expand Down Expand Up @@ -130,7 +132,8 @@ def is_cell0(self):
@base.NovaObjectRegistry.register
class CellMappingList(base.ObjectListBase, base.NovaObject):
# Version 1.0: Initial version
VERSION = '1.0'
# Version 1.1: Add get_by_disabled()
VERSION = '1.1'

fields = {
'objects': fields.ListOfObjectsField('CellMapping'),
Expand All @@ -146,3 +149,19 @@ def _get_all_from_db(context):
def get_all(cls, context):
db_mappings = cls._get_all_from_db(context)
return base.obj_make_list(context, cls(), CellMapping, db_mappings)

@staticmethod
@db_api.api_context_manager.reader
def _get_by_disabled_from_db(context, disabled):
if disabled:
return context.session.query(api_models.CellMapping).filter_by(
disabled=true()).order_by(asc(api_models.CellMapping.id)).all()
else:
return context.session.query(api_models.CellMapping).filter_by(
disabled=false()).order_by(asc(
api_models.CellMapping.id)).all()

@base.remotable_classmethod
def get_by_disabled(cls, context, disabled):
db_mappings = cls._get_by_disabled_from_db(context, disabled)
return base.obj_make_list(context, cls(), CellMapping, db_mappings)
18 changes: 18 additions & 0 deletions nova/tests/functional/db/test_cell_mapping.py
Expand Up @@ -100,3 +100,21 @@ def test_get_all(self):
mapping = mappings[db_mapping.uuid]
for key in cell_mapping.CellMapping.fields.keys():
self.assertEqual(db_mapping[key], mapping[key])

def test_get_by_disabled(self):
enabled_mapping = create_mapping(disabled=False)
disabled_mapping = create_mapping(disabled=True)

ctxt = context.RequestContext()
mappings = cell_mapping.CellMappingList.get_all(ctxt)
self.assertEqual(2, len(mappings))
self.assertEqual(enabled_mapping['uuid'], mappings[0].uuid)
self.assertEqual(disabled_mapping['uuid'], mappings[1].uuid)
mappings = cell_mapping.CellMappingList.get_by_disabled(ctxt,
disabled=False)
self.assertEqual(1, len(mappings))
self.assertEqual(enabled_mapping['uuid'], mappings[0].uuid)
mappings = cell_mapping.CellMappingList.get_by_disabled(ctxt,
disabled=True)
self.assertEqual(1, len(mappings))
self.assertEqual(disabled_mapping['uuid'], mappings[0].uuid)
29 changes: 29 additions & 0 deletions nova/tests/unit/objects/test_cell_mapping.py
Expand Up @@ -161,6 +161,35 @@ def test_get_all_sorted(self):
# order
self.assertEqual([1, 2, 3, 10], ids)

def test_get_by_disabled(self):
for ident in (4, 3):
# We start with id's 4 and 3 because we already have 2 enabled cell
# mappings in the base test case setup. 4 is before 3 to simply
# verify the sorting mechanism.
cm = objects.CellMapping(context=self.context,
id=ident,
uuid=getattr(uuids, 'cell%i' % ident),
transport_url='fake://%i' % ident,
database_connection='fake://%i' % ident,
disabled=True)
cm.create()
obj = objects.CellMappingList.get_all(self.context)
ids = [c.id for c in obj]
# Returns all the cells
self.assertEqual([1, 2, 3, 4], ids)

obj = objects.CellMappingList.get_by_disabled(self.context,
disabled=False)
ids = [c.id for c in obj]
# Returns only the enabled ones
self.assertEqual([1, 2], ids)

obj = objects.CellMappingList.get_by_disabled(self.context,
disabled=True)
ids = [c.id for c in obj]
# Returns only the disabled ones
self.assertEqual([3, 4], ids)


class TestCellMappingListObject(test_objects._LocalTest,
_TestCellMappingListObject):
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/unit/objects/test_objects.py
Expand Up @@ -1065,7 +1065,7 @@ def obj_name(cls):
'BuildRequest': '1.3-077dee42bed93f8a5b62be77657b7152',
'BuildRequestList': '1.0-cd95608eccb89fbc702c8b52f38ec738',
'CellMapping': '1.1-5d652928000a5bc369d79d5bde7e497d',
'CellMappingList': '1.0-4ee0d9efdfd681fed822da88376e04d2',
'CellMappingList': '1.1-496ef79bb2ab41041fff8bcb57996352',
'ComputeNode': '1.18-431fafd8ac4a5f3559bd9b1f1332cc22',
'ComputeNodeList': '1.17-52f3b0962b1c86b98590144463ebb192',
'CpuDiagnostics': '1.0-d256f2e442d1b837735fd17dfe8e3d47',
Expand Down

0 comments on commit a26ad9f

Please sign in to comment.