Inherit Context from oslo

In the long-term, OpenStack projects should use oslo.context as the base
class for their custom contexts. This enables the use of
application-agnostic tools, e.g. global logging configurations.

This patch uses the Context class from oslo-incubator (which will be
soon replaced by oslo.context) as the base class for Sahara Context.
The goal is to be ready for the first oslo.context release.

Change-Id: If7abc705b431aa97cd9d1c59f863d15e6a4fbdef
Partially implements: blueprint app-agnostic-logging-parameters
This commit is contained in:
Adrien Vergé 2014-11-26 20:53:09 +01:00
parent 62b146dc4c
commit dc9cf00e95
9 changed files with 40 additions and 21 deletions

View File

@ -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)

View File

@ -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'])

View File

@ -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):

View File

@ -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

View File

@ -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):

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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