From c797a1d651f844bea4276cbef9ac67f85c87d99c Mon Sep 17 00:00:00 2001 From: Andrew Bogott Date: Fri, 15 Jun 2012 02:13:13 -0500 Subject: 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 --- openstack/common/context.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'openstack/common') 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 -- cgit