diff --git a/manila/context.py b/manila/context.py index e1711c0133..9fec8da85f 100644 --- a/manila/context.py +++ b/manila/context.py @@ -108,13 +108,14 @@ class RequestContext(context.RequestContext): def to_dict(self): values = super(RequestContext, self).to_dict() values.update({ - 'user_id': self.user_id, - 'project_id': self.project_id, - 'read_deleted': self.read_deleted, - 'remote_address': self.remote_address, - 'timestamp': self.timestamp.isoformat(), - 'quota_class': self.quota_class, - 'service_catalog': self.service_catalog}) + 'user_id': getattr(self, 'user_id', None), + 'project_id': getattr(self, 'project_id', None), + 'read_deleted': getattr(self, 'read_deleted', None), + 'remote_address': getattr(self, 'remote_address', None), + 'timestamp': self.timestamp.isoformat() if hasattr( + self, 'timestamp') else None, + 'quota_class': getattr(self, 'quota_class', None), + 'service_catalog': getattr(self, 'service_catalog', None)}) return values @classmethod diff --git a/manila/tests/test_context.py b/manila/tests/test_context.py index 87262c95bb..105c6eb725 100644 --- a/manila/tests/test_context.py +++ b/manila/tests/test_context.py @@ -84,3 +84,22 @@ class ContextTestCase(test.TestCase): # user and tenant kwargs get popped off before we log anything self.assertNotIn("'user': 'user'", info['log_msg']) self.assertNotIn("'tenant': 'project'", info['log_msg']) + + def test_to_dict_works_w_missing_manila_context_attributes(self): + manila_context_attributes = ['user_id', 'project_id', 'read_deleted', + 'remote_address', 'timestamp', + 'quota_class', 'service_catalog'] + ctxt = context.RequestContext('111', '222', roles=['admin', 'weasel']) + + # Early in context initialization to_dict() can be triggered + # before all manila specific context attributes have been set. + # Set up this situation here. + for attr in manila_context_attributes: + delattr(ctxt, attr) + + # We should be able to run to_dict() without getting an + # AttributeError exception + res = ctxt.to_dict() + + for attr in manila_context_attributes: + self.assertIsNone(res[attr])