summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Bogott <abogott@wikimedia.org>2012-06-15 02:13:13 -0500
committerAndrew Bogott <abogott@wikimedia.org>2012-06-20 18:59:48 -0500
commit535cf8b0080c93283405b3df378159a4e7ee1694 (patch)
tree00ee6d39c2c2cdf37066f1ccd4e4f76f8fd471ac
parentf10abb101950da519cca2d47d38692babe2c8f28 (diff)
downloadoslo-context-535cf8b0080c93283405b3df378159a4e7ee1694.tar.gz
Added dictify() and uuids to the common request context.
This makes the common context similar enough to the nova context that we can use it for annotating logs like we do in nova. Change-Id: I622c76f2e3013e4ff5e8c228d197a55918672447
-rw-r--r--openstack/common/context.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/openstack/common/context.py b/openstack/common/context.py
index a9a16f8..35724e9 100644
--- a/openstack/common/context.py
+++ b/openstack/common/context.py
@@ -22,6 +22,12 @@ Projects should subclass this class if they wish to enhance the request
context or provide additional information in their specific WSGI pipeline.
"""
+import uuid
+
+
+def generate_request_id():
+ return 'req-' + str(uuid.uuid4())
+
class RequestContext(object):
@@ -31,10 +37,30 @@ class RequestContext(object):
"""
def __init__(self, auth_tok=None, user=None, tenant=None, is_admin=False,
- read_only=False, show_deleted=False):
+ read_only=False, show_deleted=False, request_id=None):
self.auth_tok = auth_tok
self.user = user
self.tenant = tenant
self.is_admin = is_admin
self.read_only = read_only
self.show_deleted = show_deleted
+ if not request_id:
+ request_id = generate_request_id()
+ self.request_id = request_id
+
+ def to_dict(self):
+ return {'user': self.user,
+ 'tenant': self.tenant,
+ 'is_admin': self.is_admin,
+ 'read_only': self.read_only,
+ 'show_deleted': self.show_deleted,
+ 'auth_token': self.auth_tok,
+ 'request_id': self.request_id}
+
+
+def get_admin_context(show_deleted="no"):
+ context = RequestContext(None,
+ tenant=None,
+ is_admin=True,
+ show_deleted=show_deleted)
+ return context