Add identity helper property to CellMapping

This adds CellMapping.identity, a property to help with quick logging of
a CellMapping. It's tempting to just log a a whole CellMapping object,
but that can be bad due to the credentials stored within. This adds a
convenient way to log the uuid or name-and-uuid of a CellMapping without
having to replicate the is-name-set-or-none logic everywhere.

Related to blueprint cells-aware-api
Change-Id: Ie8eaf08e9f5bda56431358c40ccf187251be4542
This commit is contained in:
Dan Smith 2017-03-09 08:14:04 -08:00
parent 9b8ab58360
commit 35f803f6f4
6 changed files with 36 additions and 12 deletions

View File

@ -203,7 +203,8 @@ def load_cells():
CELLS = objects.CellMappingList.get_all(
nova_context.get_admin_context())
LOG.debug('Found %(count)i cells: %(cells)s',
dict(count=len(CELLS), cells=CELLS))
dict(count=len(CELLS),
cells=','.join([c.identity for c in CELLS])))
if not CELLS:
LOG.error(_LE('No cells are configured, unable to continue'))
@ -2542,7 +2543,7 @@ class API(base.Base):
LOG.debug('Skipping already-collected cell0 list')
continue
LOG.debug('Listing %s instances in cell %s',
limit or 'all', cell.name or cell.uuid)
limit or 'all', cell.identity)
with nova_context.target_cell(context, cell) as ccontext:
try:
cell_insts = self._get_instances_by_filters(ccontext,

View File

@ -73,7 +73,7 @@ def targets_cell(fn):
im = None
else:
LOG.debug('Targeting cell %(cell)s for conductor method %(meth)s',
{'cell': im.cell_mapping,
{'cell': im.cell_mapping.identity,
'meth': fn.__name__})
# NOTE(danms): Target our context to the cell for the rest of
# this request, so that none of the subsequent code needs to

View File

@ -32,6 +32,13 @@ class CellMapping(base.NovaTimestampObject, base.NovaObject):
'database_connection': fields.StringField(),
}
@property
def identity(self):
if 'name' in self and self.name:
return '%s(%s)' % (self.uuid, self.name)
else:
return self.uuid
@staticmethod
def _from_db_object(context, cell_mapping, db_cell_mapping):
for key in cell_mapping.fields:

View File

@ -588,7 +588,7 @@ class HostManager(object):
services = {}
for cell in self.cells:
LOG.debug('Getting compute nodes and services for cell %(cell)s',
{'cell': cell})
{'cell': cell.identity})
with context_module.target_cell(context, cell):
if compute_uuids is None:
compute_nodes.extend(objects.ComputeNodeList.get_all(

View File

@ -4707,9 +4707,10 @@ class _ComputeAPIUnitTestMixIn(object):
mock_target.side_effect = fake_target
mock_get_cm.return_value = [
mock.MagicMock(uuid=uuids.cell1),
mock.MagicMock(uuid=objects.CellMapping.CELL0_UUID),
mock.MagicMock(uuid=uuids.cell2),
objects.CellMapping(name='foo1', uuid=uuids.cell1),
objects.CellMapping(name='foo2',
uuid=objects.CellMapping.CELL0_UUID),
objects.CellMapping(name='foo3', uuid=uuids.cell2),
]
mock_get_inst.side_effect = [
[objects.Instance(uuid=uuids.cell1inst1),
@ -4746,10 +4747,11 @@ class _ComputeAPIUnitTestMixIn(object):
mock_target.side_effect = fake_target
mock_get_cm.return_value = [
mock.MagicMock(uuid=uuids.cell1),
mock.MagicMock(uuid=objects.CellMapping.CELL0_UUID),
mock.MagicMock(uuid=uuids.cell2),
mock.MagicMock(uuid=uuids.cell3),
objects.CellMapping(name='foo1', uuid=uuids.cell1),
objects.CellMapping(name='foo2',
uuid=objects.CellMapping.CELL0_UUID),
objects.CellMapping(name='foo3', uuid=uuids.cell2),
objects.CellMapping(name='foo4', uuid=uuids.cell3),
]
mock_get_inst.side_effect = [
exception.MarkerNotFound(marker=uuids.marker),
@ -4782,7 +4784,8 @@ class _ComputeAPIUnitTestMixIn(object):
def test_get_all_instances_honors_cache(self, mock_get_inst,
mock_get_cm):
mock_get_cm.return_value = [
objects.CellMapping(uuid=objects.CellMapping.CELL0_UUID)]
objects.CellMapping(name='foo',
uuid=objects.CellMapping.CELL0_UUID)]
self.compute_api._get_instances_by_filters_all_cells(self.context, {})
self.compute_api._get_instances_by_filters_all_cells(self.context, {})

View File

@ -17,6 +17,7 @@ from nova import exception
from nova import objects
from nova.objects import cell_mapping
from nova.tests.unit.objects import test_objects
from nova.tests import uuidsentinel as uuids
def get_db_mapping(**updates):
@ -103,6 +104,18 @@ class _TestCellMappingObject(object):
self.assertTrue(objects.CellMapping(
uuid=objects.CellMapping.CELL0_UUID).is_cell0())
def test_identity_no_name_set(self):
cm = objects.CellMapping(uuid=uuids.cell1)
self.assertEqual(uuids.cell1, cm.identity)
def test_identity_name_is_none(self):
cm = objects.CellMapping(uuid=uuids.cell1)
self.assertEqual(uuids.cell1, cm.identity)
def test_identity_with_name(self):
cm = objects.CellMapping(uuid=uuids.cell1, name='foo')
self.assertEqual('%s(foo)' % uuids.cell1, cm.identity)
class TestCellMappingObject(test_objects._LocalTest,
_TestCellMappingObject):