diff --git a/sahara/context.py b/sahara/context.py index 3f4bf3b70c..54317538cf 100644 --- a/sahara/context.py +++ b/sahara/context.py @@ -24,6 +24,7 @@ from sahara import exceptions as ex from sahara.i18n import _ from sahara.i18n import _LE from sahara.i18n import _LW +from sahara.openstack.common import context from sahara.openstack.common import log as logging @@ -31,12 +32,11 @@ CONF = cfg.CONF LOG = logging.getLogger(__name__) -# TODO(slukjanov): it'll be better to use common_context.RequestContext as base -class Context(object): +class Context(context.RequestContext): def __init__(self, user_id=None, tenant_id=None, - token=None, + auth_token=None, service_catalog=None, username=None, tenant_name=None, @@ -49,13 +49,13 @@ class Context(object): LOG.warn(_LW('Arguments dropped when creating context: %s'), kwargs) - self.user_id = user_id - self.tenant_id = tenant_id - self.token = token + super(Context, self).__init__(auth_token=auth_token, + user=user_id, + tenant=tenant_id, + is_admin=is_admin) self.service_catalog = service_catalog self.username = username self.tenant_name = tenant_name - self.is_admin = is_admin self.remote_semaphore = remote_semaphore or semaphore.Semaphore( CONF.cluster_remote_threshold) self.roles = roles @@ -68,7 +68,7 @@ class Context(object): return Context( self.user_id, self.tenant_id, - self.token, + self.auth_token, self.service_catalog, self.username, self.tenant_name, @@ -81,7 +81,7 @@ class Context(object): return { 'user_id': self.user_id, 'tenant_id': self.tenant_id, - 'token': self.token, + 'auth_token': self.auth_token, 'service_catalog': self.service_catalog, 'username': self.username, 'tenant_name': self.tenant_name, @@ -91,9 +91,28 @@ class Context(object): } def is_auth_capable(self): - return (self.service_catalog and self.token and self.tenant_id and + return (self.service_catalog and self.auth_token and self.tenant and self.user_id) + # NOTE(adrienverge): The Context class uses the 'user' and 'tenant' + # properties internally (inherited from oslo.context), but Sahara code + # often uses 'user_id' and 'tenant_id'. + @property + def user_id(self): + return self.user + + @user_id.setter + def user_id(self, value): + self.user = value + + @property + def tenant_id(self): + return self.tenant + + @tenant_id.setter + def tenant_id(self, value): + self.tenant = value + def get_admin_context(): return Context(is_admin=True) diff --git a/sahara/service/trusts.py b/sahara/service/trusts.py index 0e0706c1f9..ba317cda87 100644 --- a/sahara/service/trusts.py +++ b/sahara/service/trusts.py @@ -138,6 +138,6 @@ def use_os_admin_auth_token(cluster): ctx.username = CONF.keystone_authtoken.admin_user ctx.tenant_id = cluster.tenant_id client = keystone.client_for_admin_from_trust(cluster.trust_id) - ctx.token = client.auth_token + ctx.auth_token = client.auth_token ctx.service_catalog = json.dumps( client.service_catalog.catalog['catalog']) diff --git a/sahara/tests/unit/base.py b/sahara/tests/unit/base.py index 6a38894658..4232631119 100644 --- a/sahara/tests/unit/base.py +++ b/sahara/tests/unit/base.py @@ -29,14 +29,14 @@ class SaharaTestCase(base.BaseTestCase): self.setup_context() def setup_context(self, username="test_user", tenant_id="tenant_1", - token="test_auth_token", tenant_name='test_tenant', + auth_token="test_auth_token", tenant_name='test_tenant', service_catalog=None, **kwargs): self.addCleanup(context.set_ctx, context.ctx() if context.has_ctx() else None) context.set_ctx(context.Context( username=username, tenant_id=tenant_id, - token=token, service_catalog=service_catalog or {}, + auth_token=auth_token, service_catalog=service_catalog or {}, tenant_name=tenant_name, **kwargs)) def override_config(self, name, override, group=None): diff --git a/sahara/utils/openstack/cinder.py b/sahara/utils/openstack/cinder.py index 126f058c5b..ed4cf40e65 100644 --- a/sahara/utils/openstack/cinder.py +++ b/sahara/utils/openstack/cinder.py @@ -55,14 +55,14 @@ def client(): ctx = context.current() if CONF.cinder_api_version == 1: volume_url = base.url_for(ctx.service_catalog, 'volume') - cinder = cinder_client_v1.Client(ctx.username, ctx.token, + cinder = cinder_client_v1.Client(ctx.username, ctx.auth_token, ctx.tenant_id, volume_url) else: volume_url = base.url_for(ctx.service_catalog, 'volumev2') - cinder = cinder_client_v2.Client(ctx.username, ctx.token, + cinder = cinder_client_v2.Client(ctx.username, ctx.auth_token, ctx.tenant_id, volume_url) - cinder.client.auth_token = ctx.token + cinder.client.auth_token = ctx.auth_token cinder.client.management_url = volume_url return cinder diff --git a/sahara/utils/openstack/heat.py b/sahara/utils/openstack/heat.py index 909f31ab2b..71ac1e2129 100644 --- a/sahara/utils/openstack/heat.py +++ b/sahara/utils/openstack/heat.py @@ -37,7 +37,7 @@ SSH_PORT = 22 def client(): ctx = context.current() heat_url = base.url_for(ctx.service_catalog, 'orchestration') - return heat_client.Client('1', heat_url, token=ctx.token) + return heat_client.Client('1', heat_url, token=ctx.auth_token) def get_stack(stack_name): diff --git a/sahara/utils/openstack/keystone.py b/sahara/utils/openstack/keystone.py index 2eb933c14b..2fed88928a 100644 --- a/sahara/utils/openstack/keystone.py +++ b/sahara/utils/openstack/keystone.py @@ -38,7 +38,7 @@ def client(): '''Return the current context client.''' ctx = context.current() - return _client(username=ctx.username, token=ctx.token, + return _client(username=ctx.username, token=ctx.auth_token, tenant_id=ctx.tenant_id) diff --git a/sahara/utils/openstack/neutron.py b/sahara/utils/openstack/neutron.py index 6fa7877e37..1eff7a2fb0 100644 --- a/sahara/utils/openstack/neutron.py +++ b/sahara/utils/openstack/neutron.py @@ -32,7 +32,7 @@ def client(): 'username': ctx.username, 'tenant_name': ctx.tenant_name, 'tenant_id': ctx.tenant_id, - 'token': ctx.token, + 'token': ctx.auth_token, 'endpoint_url': base.url_for(ctx.service_catalog, 'network') } return neutron_cli.Client('2.0', **args) diff --git a/sahara/utils/openstack/nova.py b/sahara/utils/openstack/nova.py index 5060ee9634..fcca0b9fc4 100644 --- a/sahara/utils/openstack/nova.py +++ b/sahara/utils/openstack/nova.py @@ -31,7 +31,7 @@ def client(): project_id=ctx.tenant_id, auth_url=auth_url) - nova.client.auth_token = ctx.token + nova.client.auth_token = ctx.auth_token nova.client.management_url = compute_url nova.images = images.SaharaImageManager(nova) return nova diff --git a/sahara/utils/ssh_remote.py b/sahara/utils/ssh_remote.py index 2c54714bfc..a659fb826a 100644 --- a/sahara/utils/ssh_remote.py +++ b/sahara/utils/ssh_remote.py @@ -438,7 +438,7 @@ class InstanceInteropHelper(remote.Remote): self.instance.node_group.cluster.neutron_management_network) ctx = context.current() neutron_info['uri'] = base.url_for(ctx.service_catalog, 'network') - neutron_info['token'] = ctx.token + neutron_info['token'] = ctx.auth_token neutron_info['tenant'] = ctx.tenant_name neutron_info['host'] = self.instance.management_ip