~yolanda.robla/ubuntu/precise/ceilometer/trunk

« back to all changes in this revision

Viewing changes to ceilometer/openstack/common/context.py

  • Committer: Ubuntu
  • Date: 2013-01-16 14:00:41 UTC
  • Revision ID: ubuntu@server-bb5d7af3-612c-4306-801d-9265ae6fcca3.canonistack-20130116140041-hklvv3jquzb8zbl9
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 OpenStack LLC.
 
4
# All Rights Reserved.
 
5
#
 
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
#    not use this file except in compliance with the License. You may obtain
 
8
#    a copy of the License at
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#    Unless required by applicable law or agreed to in writing, software
 
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
"""
 
19
Simple class that stores security context information in the web request.
 
20
 
 
21
Projects should subclass this class if they wish to enhance the request
 
22
context or provide additional information in their specific WSGI pipeline.
 
23
"""
 
24
 
 
25
import itertools
 
26
import uuid
 
27
 
 
28
 
 
29
def generate_request_id():
 
30
    return 'req-' + str(uuid.uuid4())
 
31
 
 
32
 
 
33
class RequestContext(object):
 
34
 
 
35
    """
 
36
    Stores information about the security context under which the user
 
37
    accesses the system, as well as additional request information.
 
38
    """
 
39
 
 
40
    def __init__(self, auth_tok=None, user=None, tenant=None, is_admin=False,
 
41
                 read_only=False, show_deleted=False, request_id=None):
 
42
        self.auth_tok = auth_tok
 
43
        self.user = user
 
44
        self.tenant = tenant
 
45
        self.is_admin = is_admin
 
46
        self.read_only = read_only
 
47
        self.show_deleted = show_deleted
 
48
        if not request_id:
 
49
            request_id = generate_request_id()
 
50
        self.request_id = request_id
 
51
 
 
52
    def to_dict(self):
 
53
        return {'user': self.user,
 
54
                'tenant': self.tenant,
 
55
                'is_admin': self.is_admin,
 
56
                'read_only': self.read_only,
 
57
                'show_deleted': self.show_deleted,
 
58
                'auth_token': self.auth_tok,
 
59
                'request_id': self.request_id}
 
60
 
 
61
 
 
62
def get_admin_context(show_deleted="no"):
 
63
    context = RequestContext(None,
 
64
                             tenant=None,
 
65
                             is_admin=True,
 
66
                             show_deleted=show_deleted)
 
67
    return context
 
68
 
 
69
 
 
70
def get_context_from_function_and_args(function, args, kwargs):
 
71
    """Find an arg of type RequestContext and return it.
 
72
 
 
73
       This is useful in a couple of decorators where we don't
 
74
       know much about the function we're wrapping.
 
75
    """
 
76
 
 
77
    for arg in itertools.chain(kwargs.values(), args):
 
78
        if isinstance(arg, RequestContext):
 
79
            return arg
 
80
 
 
81
    return None