Merge "Inherit Context from oslo"
This commit is contained in:
commit
4b97008b0e
@ -24,6 +24,7 @@ from sahara import exceptions as ex
|
|||||||
from sahara.i18n import _
|
from sahara.i18n import _
|
||||||
from sahara.i18n import _LE
|
from sahara.i18n import _LE
|
||||||
from sahara.i18n import _LW
|
from sahara.i18n import _LW
|
||||||
|
from sahara.openstack.common import context
|
||||||
from sahara.openstack.common import log as logging
|
from sahara.openstack.common import log as logging
|
||||||
|
|
||||||
|
|
||||||
@ -31,12 +32,11 @@ CONF = cfg.CONF
|
|||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# TODO(slukjanov): it'll be better to use common_context.RequestContext as base
|
class Context(context.RequestContext):
|
||||||
class Context(object):
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
user_id=None,
|
user_id=None,
|
||||||
tenant_id=None,
|
tenant_id=None,
|
||||||
token=None,
|
auth_token=None,
|
||||||
service_catalog=None,
|
service_catalog=None,
|
||||||
username=None,
|
username=None,
|
||||||
tenant_name=None,
|
tenant_name=None,
|
||||||
@ -49,13 +49,13 @@ class Context(object):
|
|||||||
LOG.warn(_LW('Arguments dropped when creating context: %s'),
|
LOG.warn(_LW('Arguments dropped when creating context: %s'),
|
||||||
kwargs)
|
kwargs)
|
||||||
|
|
||||||
self.user_id = user_id
|
super(Context, self).__init__(auth_token=auth_token,
|
||||||
self.tenant_id = tenant_id
|
user=user_id,
|
||||||
self.token = token
|
tenant=tenant_id,
|
||||||
|
is_admin=is_admin)
|
||||||
self.service_catalog = service_catalog
|
self.service_catalog = service_catalog
|
||||||
self.username = username
|
self.username = username
|
||||||
self.tenant_name = tenant_name
|
self.tenant_name = tenant_name
|
||||||
self.is_admin = is_admin
|
|
||||||
self.remote_semaphore = remote_semaphore or semaphore.Semaphore(
|
self.remote_semaphore = remote_semaphore or semaphore.Semaphore(
|
||||||
CONF.cluster_remote_threshold)
|
CONF.cluster_remote_threshold)
|
||||||
self.roles = roles
|
self.roles = roles
|
||||||
@ -68,7 +68,7 @@ class Context(object):
|
|||||||
return Context(
|
return Context(
|
||||||
self.user_id,
|
self.user_id,
|
||||||
self.tenant_id,
|
self.tenant_id,
|
||||||
self.token,
|
self.auth_token,
|
||||||
self.service_catalog,
|
self.service_catalog,
|
||||||
self.username,
|
self.username,
|
||||||
self.tenant_name,
|
self.tenant_name,
|
||||||
@ -81,7 +81,7 @@ class Context(object):
|
|||||||
return {
|
return {
|
||||||
'user_id': self.user_id,
|
'user_id': self.user_id,
|
||||||
'tenant_id': self.tenant_id,
|
'tenant_id': self.tenant_id,
|
||||||
'token': self.token,
|
'auth_token': self.auth_token,
|
||||||
'service_catalog': self.service_catalog,
|
'service_catalog': self.service_catalog,
|
||||||
'username': self.username,
|
'username': self.username,
|
||||||
'tenant_name': self.tenant_name,
|
'tenant_name': self.tenant_name,
|
||||||
@ -91,9 +91,28 @@ class Context(object):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def is_auth_capable(self):
|
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)
|
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():
|
def get_admin_context():
|
||||||
return Context(is_admin=True)
|
return Context(is_admin=True)
|
||||||
|
@ -138,6 +138,6 @@ def use_os_admin_auth_token(cluster):
|
|||||||
ctx.username = CONF.keystone_authtoken.admin_user
|
ctx.username = CONF.keystone_authtoken.admin_user
|
||||||
ctx.tenant_id = cluster.tenant_id
|
ctx.tenant_id = cluster.tenant_id
|
||||||
client = keystone.client_for_admin_from_trust(cluster.trust_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(
|
ctx.service_catalog = json.dumps(
|
||||||
client.service_catalog.catalog['catalog'])
|
client.service_catalog.catalog['catalog'])
|
||||||
|
@ -29,14 +29,14 @@ class SaharaTestCase(base.BaseTestCase):
|
|||||||
self.setup_context()
|
self.setup_context()
|
||||||
|
|
||||||
def setup_context(self, username="test_user", tenant_id="tenant_1",
|
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):
|
service_catalog=None, **kwargs):
|
||||||
self.addCleanup(context.set_ctx,
|
self.addCleanup(context.set_ctx,
|
||||||
context.ctx() if context.has_ctx() else None)
|
context.ctx() if context.has_ctx() else None)
|
||||||
|
|
||||||
context.set_ctx(context.Context(
|
context.set_ctx(context.Context(
|
||||||
username=username, tenant_id=tenant_id,
|
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))
|
tenant_name=tenant_name, **kwargs))
|
||||||
|
|
||||||
def override_config(self, name, override, group=None):
|
def override_config(self, name, override, group=None):
|
||||||
|
@ -55,14 +55,14 @@ def client():
|
|||||||
ctx = context.current()
|
ctx = context.current()
|
||||||
if CONF.cinder_api_version == 1:
|
if CONF.cinder_api_version == 1:
|
||||||
volume_url = base.url_for(ctx.service_catalog, 'volume')
|
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)
|
ctx.tenant_id, volume_url)
|
||||||
else:
|
else:
|
||||||
volume_url = base.url_for(ctx.service_catalog, 'volumev2')
|
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)
|
ctx.tenant_id, volume_url)
|
||||||
|
|
||||||
cinder.client.auth_token = ctx.token
|
cinder.client.auth_token = ctx.auth_token
|
||||||
cinder.client.management_url = volume_url
|
cinder.client.management_url = volume_url
|
||||||
|
|
||||||
return cinder
|
return cinder
|
||||||
|
@ -37,7 +37,7 @@ SSH_PORT = 22
|
|||||||
def client():
|
def client():
|
||||||
ctx = context.current()
|
ctx = context.current()
|
||||||
heat_url = base.url_for(ctx.service_catalog, 'orchestration')
|
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):
|
def get_stack(stack_name):
|
||||||
|
@ -38,7 +38,7 @@ def client():
|
|||||||
'''Return the current context client.'''
|
'''Return the current context client.'''
|
||||||
ctx = context.current()
|
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)
|
tenant_id=ctx.tenant_id)
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ def client():
|
|||||||
'username': ctx.username,
|
'username': ctx.username,
|
||||||
'tenant_name': ctx.tenant_name,
|
'tenant_name': ctx.tenant_name,
|
||||||
'tenant_id': ctx.tenant_id,
|
'tenant_id': ctx.tenant_id,
|
||||||
'token': ctx.token,
|
'token': ctx.auth_token,
|
||||||
'endpoint_url': base.url_for(ctx.service_catalog, 'network')
|
'endpoint_url': base.url_for(ctx.service_catalog, 'network')
|
||||||
}
|
}
|
||||||
return neutron_cli.Client('2.0', **args)
|
return neutron_cli.Client('2.0', **args)
|
||||||
|
@ -31,7 +31,7 @@ def client():
|
|||||||
project_id=ctx.tenant_id,
|
project_id=ctx.tenant_id,
|
||||||
auth_url=auth_url)
|
auth_url=auth_url)
|
||||||
|
|
||||||
nova.client.auth_token = ctx.token
|
nova.client.auth_token = ctx.auth_token
|
||||||
nova.client.management_url = compute_url
|
nova.client.management_url = compute_url
|
||||||
nova.images = images.SaharaImageManager(nova)
|
nova.images = images.SaharaImageManager(nova)
|
||||||
return nova
|
return nova
|
||||||
|
@ -438,7 +438,7 @@ class InstanceInteropHelper(remote.Remote):
|
|||||||
self.instance.node_group.cluster.neutron_management_network)
|
self.instance.node_group.cluster.neutron_management_network)
|
||||||
ctx = context.current()
|
ctx = context.current()
|
||||||
neutron_info['uri'] = base.url_for(ctx.service_catalog, 'network')
|
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['tenant'] = ctx.tenant_name
|
||||||
neutron_info['host'] = self.instance.management_ip
|
neutron_info['host'] = self.instance.management_ip
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user