From e32d0d247df304a1bb675b2132e9aef7da3eed32 Mon Sep 17 00:00:00 2001 From: Hiroaki Kobayashi Date: Fri, 3 Feb 2017 16:39:00 +0900 Subject: [PATCH] Use oslo.context This patch replaces old oslo-incubator code with the oslo.context module. Change-Id: I4409ffdff5f93d4171f516b39bbeeb518c037b94 Closes-Bug: #1661520 --- climate/openstack/common/context.py | 111 ---------------------- climate/tests/db/sqlalchemy/test_utils.py | 3 +- test-requirements.txt | 1 + 3 files changed, 3 insertions(+), 112 deletions(-) delete mode 100644 climate/openstack/common/context.py diff --git a/climate/openstack/common/context.py b/climate/openstack/common/context.py deleted file mode 100644 index 09019ee3..00000000 --- a/climate/openstack/common/context.py +++ /dev/null @@ -1,111 +0,0 @@ -# Copyright 2011 OpenStack Foundation. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -""" -Simple class that stores security context information in the web request. - -Projects should subclass this class if they wish to enhance the request -context or provide additional information in their specific WSGI pipeline. -""" - -import itertools -import uuid - - -def generate_request_id(): - return 'req-%s' % str(uuid.uuid4()) - - -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 - - def to_dict(self): - user_idt = ( - self.user_idt_format.format(user=self.user or '-', - tenant=self.tenant or '-', - domain=self.domain or '-', - user_domain=self.user_domain or '-', - p_domain=self.project_domain or '-')) - - return {'user': self.user, - 'tenant': self.tenant, - 'domain': self.domain, - 'user_domain': self.user_domain, - 'project_domain': self.project_domain, - 'is_admin': self.is_admin, - 'read_only': self.read_only, - 'show_deleted': self.show_deleted, - 'auth_token': self.auth_token, - 'request_id': self.request_id, - 'instance_uuid': self.instance_uuid, - 'user_identity': user_idt} - - -def get_admin_context(show_deleted=False): - context = RequestContext(None, - tenant=None, - is_admin=True, - show_deleted=show_deleted) - return context - - -def get_context_from_function_and_args(function, args, kwargs): - """Find an arg of type RequestContext and return it. - - This is useful in a couple of decorators where we don't - know much about the function we're wrapping. - """ - - for arg in itertools.chain(kwargs.values(), args): - if isinstance(arg, RequestContext): - return arg - - return None - - -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 diff --git a/climate/tests/db/sqlalchemy/test_utils.py b/climate/tests/db/sqlalchemy/test_utils.py index 412205e4..716a6b67 100644 --- a/climate/tests/db/sqlalchemy/test_utils.py +++ b/climate/tests/db/sqlalchemy/test_utils.py @@ -17,9 +17,10 @@ import datetime import uuid +from oslo_context import context + from climate.db.sqlalchemy import api as db_api from climate.db.sqlalchemy import utils as db_utils -from climate.openstack.common import context from climate import tests diff --git a/test-requirements.txt b/test-requirements.txt index da127cf8..68ca5d96 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -18,6 +18,7 @@ coverage>=4.0 # Apache-2.0 pylint==1.4.5 # GPLv2 sphinxcontrib-pecanwsme>=0.8 # Apache-2.0 oslotest>=1.10.0 # Apache-2.0 +oslo.context>=2.12.0 # Apache-2.0 # Documentation oslosphinx>=4.7.0 # Apache-2.0