openstack context

时间:2024-07-08 23:34:50
之前一直不知道context模块中存储的是什么东西,这回看一下代码;
其中最主要的类是:RequestContext;
class RequestContext(object):
"""Helper class to represent useful information about a request context.
Stores information about the security context under which the user
accesses the system, as well as additional request information.
"""
    user_idt_format = '{user} {tenant} {domain} {user_domain} {p_domain}'
    def __init__(self, auth_token=None, user=None, tenant=None, domain=None,
                       user_domain=None, project_domain=None, is_admin=False,
                       read_only=False, show_deleted=False, request_id=None,
                       instance_uuid=None):
        self.auth_token = auth_token
        self.user = user
        self.tenant = tenant
        self.domain = domain
        self.user_domain = user_domain
        self.project_domain = project_domain
        self.is_admin = is_admin
        self.read_only = read_only
        self.show_deleted = show_deleted
        self.instance_uuid = instance_uuid
        if not request_id:
            request_id = generate_request_id()
        self.request_id = request_id
        …...
"""Helper class to represent useful information about a request context.
Stores information about the security context under which the user
accesses the system, as well as additional request information.
"""
RequestContext类主要保存了一次web请求的useful context information,主要包括安全验证信息和其他一些信息;
如果用户想要对request的上下文新增一些信息,或加强功能,可以继承这个类;
其中两个函数分别获取admin上下文信息,和判断一个context是不是一个normal user:
def get_admin_context(show_deleted=False):
    context = RequestContext(None,
                    tenant=None,
                    is_admin=True,
                    show_deleted=show_deleted)
    return context
def is_user_context(context):
"""Indicates if the request context is a normal user."""
    if not context:
        return False
    if context.is_admin:
        return False
    if not context.user_id or not context.project_id:
        return False
    return True