Use context.from_dict to determine available arguments

RequestContext.__init__ takes and ignores arbitrary keyword arguments.

The ignored arguments are generally made up of things that
oslo_context added to the to_dict that karbor doesn't handle.
Instead of accepting and ignoring those arguments make from_dict
smart enough to construct the correct arguments to the class.

Related-Bug: #1602081
Change-Id: I6782e63e6a530f53e7bf430d28cc3309903e7fb4
This commit is contained in:
chenying 2017-05-03 23:29:53 +08:00
parent 3c006f887a
commit c2dd9e0f65
2 changed files with 23 additions and 7 deletions

View File

@ -38,7 +38,7 @@ class RequestContext(context.RequestContext):
timestamp=None, request_id=None, auth_token=None,
overwrite=True, quota_class=None, service_catalog=None,
domain=None, user_domain=None, project_domain=None,
**kwargs):
auth_token_info=None):
"""Initialize RequestContext.
:param read_deleted: 'no' indicates deleted records are hidden, 'yes'
@ -47,9 +47,6 @@ class RequestContext(context.RequestContext):
:param overwrite: Set to False to ensure that the greenthread local
copy of the index is not overwritten.
:param kwargs: Extra arguments that might be present, but we ignore
because they possibly came in from older rpc messages.
"""
super(RequestContext, self).__init__(auth_token=auth_token,
@ -70,7 +67,7 @@ class RequestContext(context.RequestContext):
timestamp = timeutils.parse_isotime(timestamp)
self.timestamp = timestamp
self.quota_class = quota_class
self._auth_token_info = kwargs.get('auth_token_info')
self._auth_token_info = auth_token_info
if service_catalog:
# Only include required parts of service_catalog
@ -125,7 +122,26 @@ class RequestContext(context.RequestContext):
@classmethod
def from_dict(cls, values):
return cls(**values)
allowed_keys = [
'user_id',
'project_id',
'project_name',
'domain',
'read_deleted',
'remote_address',
'timestamp',
'quota_class',
'service_catalog',
'request_id',
'is_admin',
'roles',
'auth_token',
'user_domain',
'project_domain',
'auth_token_info'
]
kwargs = {k: values[k] for k in values if k in allowed_keys}
return cls(**kwargs)
def elevated(self, read_deleted=None, overwrite=False):
"""Return a version of this context with admin flag set."""

View File

@ -53,7 +53,7 @@ class ContextTestCase(base.TestCase):
def test_request_context_elevated(self):
user_context = context.RequestContext(
'fake_user', 'fake_project', admin=False)
'fake_user', 'fake_project', is_admin=False)
self.assertFalse(user_context.is_admin)
admin_context = user_context.elevated()
self.assertFalse(user_context.is_admin)