Coverage for watcher/common/context.py: 98%
39 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
13from oslo_context import context
14from oslo_db.sqlalchemy import enginefacade
15from oslo_log import log
16from oslo_utils import timeutils
19LOG = log.getLogger(__name__)
22@enginefacade.transaction_context_provider
23class RequestContext(context.RequestContext):
24 """Extends security contexts from the OpenStack common library."""
26 def __init__(self, user_id=None, project_id=None, is_admin=None,
27 roles=None, timestamp=None, request_id=None, auth_token=None,
28 overwrite=True, user_name=None, project_name=None,
29 domain_name=None, domain_id=None, auth_token_info=None,
30 **kwargs):
31 """Stores several additional request parameters:
33 :param domain_id: The ID of the domain.
34 :param domain_name: The name of the domain.
35 :param is_public_api: Specifies whether the request should be processed
36 without authentication.
38 """
39 user = kwargs.pop('user', None)
40 tenant = kwargs.pop('tenant', None)
41 super(RequestContext, self).__init__(
42 auth_token=auth_token,
43 user_id=user_id or user,
44 project_id=project_id or tenant,
45 domain_id=kwargs.pop('domain', None) or domain_name or domain_id,
46 user_domain_id=kwargs.pop('user_domain', None),
47 project_domain_id=kwargs.pop('project_domain', None),
48 is_admin=is_admin,
49 read_only=kwargs.pop('read_only', False),
50 show_deleted=kwargs.pop('show_deleted', False),
51 request_id=request_id,
52 resource_uuid=kwargs.pop('resource_uuid', None),
53 is_admin_project=kwargs.pop('is_admin_project', True),
54 overwrite=overwrite,
55 roles=roles,
56 global_request_id=kwargs.pop('global_request_id', None),
57 system_scope=kwargs.pop('system_scope', None))
59 self.remote_address = kwargs.pop('remote_address', None)
60 self.read_deleted = kwargs.pop('read_deleted', None)
61 self.service_catalog = kwargs.pop('service_catalog', None)
62 self.quota_class = kwargs.pop('quota_class', None)
64 # FIXME(dims): user_id and project_id duplicate information that is
65 # already present in the oslo_context's RequestContext. We need to
66 # get rid of them.
67 self.domain_name = domain_name
68 self.domain_id = domain_id
69 self.auth_token_info = auth_token_info
70 self.user_id = user_id or user
71 self.project_id = project_id
72 if not timestamp:
73 timestamp = timeutils.utcnow()
74 if isinstance(timestamp, str):
75 timestamp = timeutils.parse_isotime(timestamp)
76 self.timestamp = timestamp
77 self.user_name = user_name
78 self.project_name = project_name
79 self.is_admin = is_admin
80 # if self.is_admin is None:
81 # self.is_admin = policy.check_is_admin(self)
83 def to_dict(self):
84 values = super(RequestContext, self).to_dict()
85 # FIXME(dims): defensive hasattr() checks need to be
86 # removed once we figure out why we are seeing stack
87 # traces
88 values.update({
89 'user_id': getattr(self, 'user_id', None),
90 'user_name': getattr(self, 'user_name', None),
91 'project_id': getattr(self, 'project_id', None),
92 'project_name': getattr(self, 'project_name', None),
93 'domain_id': getattr(self, 'domain_id', None),
94 'domain_name': getattr(self, 'domain_name', None),
95 'auth_token_info': getattr(self, 'auth_token_info', None),
96 'is_admin': getattr(self, 'is_admin', None),
97 'timestamp': self.timestamp.isoformat() if hasattr(
98 self, 'timestamp') else None,
99 'request_id': getattr(self, 'request_id', None),
100 })
101 return values
103 @classmethod
104 def from_dict(cls, values):
105 return cls(**values)
107 def __str__(self):
108 return "<Context %s>" % self.to_dict()
111def make_context(*args, **kwargs):
112 return RequestContext(*args, **kwargs)