From 5cf0c9b4eb354a9a4e0d115510946c732f4006fa Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Wed, 1 Aug 2012 00:32:30 -0400 Subject: [PATCH] Store context in local thread store for logging. This also fixes the logging of request ids for every request. Fixes bug 1031596 Change-Id: Ifd0217c99402316214efaf1fe8533c60c2277257 --- glance/context.py | 25 +++++++++++++++++++++++++ glance/tests/unit/test_context.py | 8 ++++++++ 2 files changed, 33 insertions(+) diff --git a/glance/context.py b/glance/context.py index f195d1fb..eb6e4b92 100644 --- a/glance/context.py +++ b/glance/context.py @@ -16,6 +16,7 @@ # under the License. import glance.common.utils +from glance.openstack.common import local class RequestContext(object): @@ -38,6 +39,30 @@ class RequestContext(object): self.request_id = glance.common.utils.generate_uuid() self.service_catalog = service_catalog + if not hasattr(local.store, 'context'): + self.update_store() + + def to_dict(self): + # NOTE(ameade): These keys are named to correspond with the default + # format string for logging the context in openstack common + return {'request_id': self.request_id, + 'user_id': self.user, + 'tenant_id': self.tenant, + 'is_admin': self.is_admin, + 'project_id': self.tenant, + 'read_deleted': self.show_deleted, + 'roles': self.roles, + 'auth_token': self.auth_tok, + 'service_catalog': self.service_catalog, + } + + @classmethod + def from_dict(cls, values): + return cls(**values) + + def update_store(self): + local.store.context = self + @property def owner(self): """Return the owner to correlate with an image.""" diff --git a/glance/tests/unit/test_context.py b/glance/tests/unit/test_context.py index 4036e209..598a47ee 100644 --- a/glance/tests/unit/test_context.py +++ b/glance/tests/unit/test_context.py @@ -16,6 +16,7 @@ # under the License. from glance import context +from glance.openstack.common import local from glance.tests.unit import utils as unit_utils from glance.tests import utils @@ -242,3 +243,10 @@ class TestContext(utils.BaseTestCase): def test_service_catalog(self): ctx = context.RequestContext(service_catalog=['foo']) self.assertEqual(['foo'], ctx.service_catalog) + + def test_context_local_store(self): + if hasattr(local.store, 'context'): + del local.store.context + ctx = context.RequestContext() + self.assertTrue(hasattr(local.store, 'context')) + self.assertEqual(ctx, local.store.context)