Add properties for id attributes
The user, tenant and similar values are ambiguous. Most contexts that subclass oslo.context already provide some version of this and use the _id attributes anyway. Ideally we would deprecate these values in future but for now just provide the alias. Change-Id: Ia59bf69eaa83be1904abe835bfee68b785c92c54
This commit is contained in:
parent
2faf1f7d68
commit
78644789ce
@ -32,6 +32,23 @@ def generate_request_id():
|
||||
return b'req-' + str(uuid.uuid4()).encode('ascii')
|
||||
|
||||
|
||||
class _new_prop(object):
|
||||
"""Create a new property that refers to an old one.
|
||||
|
||||
For backwards compatibility reasons we need to maintain some attributes of
|
||||
context that should be deprecated. Create a property with a name that
|
||||
points to an otherwise public attribute.
|
||||
"""
|
||||
def __init__(self, old_name):
|
||||
self.old_name = old_name
|
||||
|
||||
def __get__(self, obj, objtype):
|
||||
return getattr(obj, self.old_name)
|
||||
|
||||
def __set__(self, obj, val):
|
||||
return setattr(obj, self.old_name, val)
|
||||
|
||||
|
||||
class RequestContext(object):
|
||||
|
||||
"""Helper class to represent useful information about a request context.
|
||||
@ -67,6 +84,16 @@ class RequestContext(object):
|
||||
if overwrite or not get_current():
|
||||
self.update_store()
|
||||
|
||||
# NOTE(jamielennox): for now we store under the old name and reference via
|
||||
# the new name. This is currently easier than changing the __init__
|
||||
# arguments. In future this should be swapped and the non-_id suffixed
|
||||
# attributes deprecated.
|
||||
user_id = _new_prop('user')
|
||||
project_id = _new_prop('tenant')
|
||||
domain_id = _new_prop('domain')
|
||||
user_domain_id = _new_prop('user_domain')
|
||||
project_domain_id = _new_prop('project_domain')
|
||||
|
||||
def update_store(self):
|
||||
_request_store.context = self
|
||||
|
||||
|
@ -13,6 +13,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import uuid
|
||||
|
||||
from oslotest import base as test_base
|
||||
|
||||
from oslo_context import context
|
||||
@ -92,3 +94,30 @@ class ContextTest(test_base.BaseTestCase):
|
||||
self.assertFalse(context.is_user_context(ctx))
|
||||
ctx = context.RequestContext(is_admin=False)
|
||||
self.assertTrue(context.is_user_context(ctx))
|
||||
|
||||
def test_aliased_props(self):
|
||||
user = uuid.uuid4().hex
|
||||
tenant = uuid.uuid4().hex
|
||||
domain = uuid.uuid4().hex
|
||||
user_domain = uuid.uuid4().hex
|
||||
project_domain = uuid.uuid4().hex
|
||||
|
||||
ctx = context.RequestContext(user=user,
|
||||
tenant=tenant,
|
||||
domain=domain,
|
||||
user_domain=user_domain,
|
||||
project_domain=project_domain)
|
||||
|
||||
# original attributes
|
||||
self.assertEqual(user, ctx.user)
|
||||
self.assertEqual(tenant, ctx.tenant)
|
||||
self.assertEqual(domain, ctx.domain)
|
||||
self.assertEqual(user_domain, ctx.user_domain)
|
||||
self.assertEqual(project_domain, ctx.project_domain)
|
||||
|
||||
# aliased properties
|
||||
self.assertEqual(user, ctx.user_id)
|
||||
self.assertEqual(tenant, ctx.project_id)
|
||||
self.assertEqual(domain, ctx.domain_id)
|
||||
self.assertEqual(user_domain, ctx.user_domain_id)
|
||||
self.assertEqual(project_domain, ctx.project_domain_id)
|
||||
|
Loading…
Reference in New Issue
Block a user