From 8c4990f8f39b706644dd55b3c2018bf540b440d3 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Wed, 22 Mar 2017 12:08:21 +0000 Subject: [PATCH] Handle tenant_name and project_name more equally When we construct context object using Context.from_environ() tenant_name is not passed while project_name is passed. This commit makes project_name and tenant_name handled more equally. Needed-By: Ie48aa843ca8c852b1e93e760d2e3e8aaa38aed56 Change-Id: Ieec57d9ea8d95e55499a17e2c04da5e3e78a1557 --- neutron_lib/context.py | 27 ++++++++++++++------------ neutron_lib/tests/unit/test_context.py | 2 ++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/neutron_lib/context.py b/neutron_lib/context.py index f4190f5f1..eb29238f6 100644 --- a/neutron_lib/context.py +++ b/neutron_lib/context.py @@ -36,13 +36,12 @@ class ContextBase(oslo_context.RequestContext): # that pass arguments positionally. kwargs.setdefault('user', user_id) kwargs.setdefault('tenant', tenant_id) + # prefer project_name, as that's what's going to be set by + # keystone. Fall back to tenant_name if for some reason it's blank. + kwargs.setdefault('project_name', tenant_name) super(ContextBase, self).__init__(is_admin=is_admin, **kwargs) self.user_name = user_name - # NOTE(sdague): tenant* is a deprecated set of names from - # keystone, and is no longer set in modern keystone middleware - # code, as such this is almost always going to be None. - self.tenant_name = tenant_name if not timestamp: timestamp = datetime.datetime.utcnow() @@ -65,6 +64,14 @@ class ContextBase(oslo_context.RequestContext): def tenant_id(self, tenant_id): self.tenant = tenant_id + @property + def tenant_name(self): + return self.project_name + + @tenant_name.setter + def tenant_name(self, tenant_name): + self.project_name = tenant_name + @property def user_id(self): return self.user @@ -80,10 +87,8 @@ class ContextBase(oslo_context.RequestContext): 'tenant_id': self.tenant_id, 'project_id': self.project_id, 'timestamp': str(self.timestamp), - # prefer project_name, as that's what's going to be set by - # keystone. Fall back if for some reason it's blank. - 'tenant_name': self.project_name or self.tenant_name, - 'project_name': self.project_name or self.tenant_name, + 'tenant_name': self.project_name, + 'project_name': self.project_name, 'user_name': self.user_name, }) return context @@ -102,10 +107,8 @@ class ContextBase(oslo_context.RequestContext): values['domain'] = self.domain values['user_domain'] = self.user_domain values['project_domain'] = self.project_domain - # prefer project_name, as that's what's going to be set by - # keystone. Fall back if for some reason it's blank. - values['tenant_name'] = self.project_name or self.tenant_name - values['project_name'] = self.project_name or self.tenant_name + values['tenant_name'] = self.project_name + values['project_name'] = self.project_name values['user_name'] = self.user_name return values diff --git a/neutron_lib/tests/unit/test_context.py b/neutron_lib/tests/unit/test_context.py index 33a83afa0..71d170521 100644 --- a/neutron_lib/tests/unit/test_context.py +++ b/neutron_lib/tests/unit/test_context.py @@ -39,6 +39,7 @@ class TestNeutronContext(_base.BaseTestCase): self.assertEqual('tenant_id', ctx.tenant) self.assertIsNone(ctx.user_name) self.assertIsNone(ctx.tenant_name) + self.assertIsNone(ctx.project_name) self.assertIsNone(ctx.auth_token) def test_neutron_context_getter_setter(self): @@ -56,6 +57,7 @@ class TestNeutronContext(_base.BaseTestCase): # Check name is set self.assertEqual('user_name', ctx.user_name) self.assertEqual('tenant_name', ctx.tenant_name) + self.assertEqual('tenant_name', ctx.project_name) # Check user/tenant contains its ID even if user/tenant_name is passed self.assertEqual('user_id', ctx.user) self.assertEqual('tenant_id', ctx.tenant)