[Refactor code] Update client v1
- Re-order argument alphabetically and add docstring. - Move osprofiler_profilter to zunclient.client.Client. Change-Id: Ie9be389495e2f13454f1f8d1c1d66b22d813a9ec
This commit is contained in:
parent
6c49f94b2f
commit
b2bda489a5
@ -20,6 +20,8 @@ from zunclient import api_versions
|
|||||||
from zunclient import exceptions
|
from zunclient import exceptions
|
||||||
from zunclient.i18n import _
|
from zunclient.i18n import _
|
||||||
|
|
||||||
|
osprofiler_profiler = importutils.try_import("osprofiler.profiler")
|
||||||
|
|
||||||
|
|
||||||
def _get_client_class_and_version(version):
|
def _get_client_class_and_version(version):
|
||||||
if not isinstance(version, api_versions.APIVersion):
|
if not isinstance(version, api_versions.APIVersion):
|
||||||
@ -81,6 +83,16 @@ def Client(version='1', username=None, auth_url=None, **kwargs):
|
|||||||
right_name='project_name')
|
right_name='project_name')
|
||||||
_check_arguments(kwargs, 'Queens', 'tenant_id', right_name='project_id')
|
_check_arguments(kwargs, 'Queens', 'tenant_id', right_name='project_id')
|
||||||
|
|
||||||
|
profile = kwargs.pop('profile', None)
|
||||||
|
if osprofiler_profiler and profile:
|
||||||
|
# Initialize the root of the future trace: the created trace ID
|
||||||
|
# will be used as the very first parent to which all related
|
||||||
|
# traces will be bound to. The given HMAC key must correspond to
|
||||||
|
# the one set in zun-api zun.conf, otherwise the latter
|
||||||
|
# will fail to check the request signature and will skip
|
||||||
|
# initialization of osprofiler on the server side.
|
||||||
|
osprofiler_profiler.init(profile)
|
||||||
|
|
||||||
api_version, client_class = _get_client_class_and_version(version)
|
api_version, client_class = _get_client_class_and_version(version)
|
||||||
return client_class(api_version=api_version,
|
return client_class(api_version=api_version,
|
||||||
auth_url=auth_url,
|
auth_url=auth_url,
|
||||||
|
@ -43,7 +43,8 @@ class ClientTest(testtools.TestCase):
|
|||||||
mock_token.return_value = mock_auth_plugin
|
mock_token.return_value = mock_auth_plugin
|
||||||
session = mock.Mock()
|
session = mock.Mock()
|
||||||
mock_session.return_value = session
|
mock_session.return_value = session
|
||||||
client.Client(input_auth_token='mytoken', zun_url='http://myurl/')
|
client.Client(auth_token='mytoken',
|
||||||
|
endpoint_override='http://myurl/')
|
||||||
mock_session.assert_called_once_with(
|
mock_session.assert_called_once_with(
|
||||||
auth=mock_auth_plugin, verify=True)
|
auth=mock_auth_plugin, verify=True)
|
||||||
http_client.assert_called_once_with(
|
http_client.assert_called_once_with(
|
||||||
@ -62,7 +63,7 @@ class ClientTest(testtools.TestCase):
|
|||||||
self, mock_session, mock_loader, http_client):
|
self, mock_session, mock_loader, http_client):
|
||||||
mock_plugin = mock.Mock()
|
mock_plugin = mock.Mock()
|
||||||
mock_loader.return_value = mock_plugin
|
mock_loader.return_value = mock_plugin
|
||||||
client.Client(input_auth_token='mytoken', auth_url='authurl')
|
client.Client(auth_token='mytoken', auth_url='authurl')
|
||||||
mock_loader.assert_called_once_with('token')
|
mock_loader.assert_called_once_with('token')
|
||||||
mock_plugin.load_from_options.assert_called_once_with(
|
mock_plugin.load_from_options.assert_called_once_with(
|
||||||
auth_url='authurl',
|
auth_url='authurl',
|
||||||
@ -155,7 +156,7 @@ class ClientTest(testtools.TestCase):
|
|||||||
http_client):
|
http_client):
|
||||||
session = mock.Mock()
|
session = mock.Mock()
|
||||||
client.Client(session=session, zun_url='zunurl',
|
client.Client(session=session, zun_url='zunurl',
|
||||||
endpoint_override='zunurl_override')
|
endpoint_override='zunurl')
|
||||||
mock_session.assert_not_called()
|
mock_session.assert_not_called()
|
||||||
http_client.assert_called_once_with(
|
http_client.assert_called_once_with(
|
||||||
interface='public',
|
interface='public',
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
|
|
||||||
from keystoneauth1 import loading
|
from keystoneauth1 import loading
|
||||||
from keystoneauth1 import session as ksa_session
|
from keystoneauth1 import session as ksa_session
|
||||||
from oslo_utils import importutils
|
|
||||||
|
|
||||||
from zunclient.common import httpclient
|
from zunclient.common import httpclient
|
||||||
from zunclient.v1 import containers
|
from zunclient.v1 import containers
|
||||||
@ -23,99 +22,108 @@ from zunclient.v1 import hosts
|
|||||||
from zunclient.v1 import images
|
from zunclient.v1 import images
|
||||||
from zunclient.v1 import services
|
from zunclient.v1 import services
|
||||||
|
|
||||||
profiler = importutils.try_import("osprofiler.profiler")
|
|
||||||
|
|
||||||
|
|
||||||
class Client(object):
|
class Client(object):
|
||||||
def __init__(self, username=None, api_key=None, project_id=None,
|
"""Top-level object to access the OpenStack Container API."""
|
||||||
project_name=None, auth_url=None, zun_url=None,
|
|
||||||
endpoint_type=None, endpoint_override=None,
|
def __init__(self, api_version=None, auth_token=None,
|
||||||
service_type='container',
|
auth_type='password', auth_url=None, endpoint_override=None,
|
||||||
region_name=None, input_auth_token=None,
|
interface='public', insecure=False, password=None,
|
||||||
session=None, password=None, auth_type='password',
|
|
||||||
interface='public', service_name=None, insecure=False,
|
|
||||||
user_domain_id=None, user_domain_name=None,
|
|
||||||
project_domain_id=None, project_domain_name=None,
|
project_domain_id=None, project_domain_name=None,
|
||||||
api_version=None, **kwargs):
|
project_id=None, project_name=None, region_name=None,
|
||||||
|
service_name=None, service_type='container', session=None,
|
||||||
|
user_domain_id=None, user_domain_name=None,
|
||||||
|
username=None, **kwargs):
|
||||||
|
"""Initialization of Client object.
|
||||||
|
|
||||||
# fix (yolanda): os-cloud-config is using endpoint_override
|
:param api_version: Container API version
|
||||||
# instead of zun_url
|
:type api_version: zunclient.api_version.APIVersion
|
||||||
if endpoint_override and not zun_url:
|
:param str auth_token: Auth token
|
||||||
zun_url = endpoint_override
|
:param str auth_url: Auth URL
|
||||||
|
:param str auth_type: Auth Type
|
||||||
if zun_url and input_auth_token:
|
:param str endpoint_override: Bypass URL
|
||||||
|
:param str interface: Interface
|
||||||
|
:param str insecure: Allow insecure
|
||||||
|
:param str password: User password
|
||||||
|
:param str project_domain_id: ID of project domain
|
||||||
|
:param str project_domain_name: Nam of project domain
|
||||||
|
:param str project_id: Project/Tenant ID
|
||||||
|
:param str project_name: Project/Tenant Name
|
||||||
|
:param str region_name: Region Name
|
||||||
|
:param str service_name: Service Name
|
||||||
|
:param str service_type: Service Type
|
||||||
|
:param str session: Session
|
||||||
|
:param str user_domain_id: ID of user domain
|
||||||
|
:param str user_id: User ID
|
||||||
|
:param str username: Username
|
||||||
|
"""
|
||||||
|
if endpoint_override and auth_token:
|
||||||
auth_type = 'admin_token'
|
auth_type = 'admin_token'
|
||||||
session = None
|
session = None
|
||||||
loader_kwargs = dict(
|
loader_kwargs = {
|
||||||
token=input_auth_token,
|
'token': auth_token,
|
||||||
endpoint=zun_url)
|
'endpoint': endpoint_override
|
||||||
elif input_auth_token and not session:
|
}
|
||||||
|
elif auth_token and not session:
|
||||||
auth_type = 'token'
|
auth_type = 'token'
|
||||||
loader_kwargs = dict(
|
loader_kwargs = {
|
||||||
token=input_auth_token,
|
'token': auth_token,
|
||||||
auth_url=auth_url,
|
'auth_url': auth_url,
|
||||||
project_id=project_id,
|
'project_domain_id': project_domain_id,
|
||||||
project_name=project_name,
|
'project_domain_name': project_domain_name,
|
||||||
user_domain_id=user_domain_id,
|
'project_id': project_id,
|
||||||
user_domain_name=user_domain_name,
|
'project_name': project_name,
|
||||||
project_domain_id=project_domain_id,
|
'user_domain_id': user_domain_id,
|
||||||
project_domain_name=project_domain_name)
|
'user_domain_name': user_domain_name
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
loader_kwargs = dict(
|
loader_kwargs = {
|
||||||
username=username,
|
'auth_url': auth_url,
|
||||||
password=password,
|
'password': password,
|
||||||
auth_url=auth_url,
|
'project_domain_id': project_domain_id,
|
||||||
project_id=project_id,
|
'project_domain_name': project_domain_name,
|
||||||
project_name=project_name,
|
'project_id': project_id,
|
||||||
user_domain_id=user_domain_id,
|
'project_name': project_name,
|
||||||
user_domain_name=user_domain_name,
|
'user_domain_id': user_domain_id,
|
||||||
project_domain_id=project_domain_id,
|
'user_domain_name': user_domain_name,
|
||||||
project_domain_name=project_domain_name)
|
'username': username,
|
||||||
|
}
|
||||||
|
|
||||||
# Backwards compatibility for people not passing in Session
|
# Backwards compatibility for people not passing in Session
|
||||||
if session is None:
|
if session is None:
|
||||||
loader = loading.get_plugin_loader(auth_type)
|
loader = loading.get_plugin_loader(auth_type)
|
||||||
|
|
||||||
# This should be able to handle v2 and v3 Keystone Auth
|
# This should be able to handle v2 and v3 Keystone Auth
|
||||||
auth_plugin = loader.load_from_options(**loader_kwargs)
|
auth_plugin = loader.load_from_options(**loader_kwargs)
|
||||||
session = ksa_session.Session(
|
session = ksa_session.Session(auth=auth_plugin,
|
||||||
auth=auth_plugin, verify=(not insecure))
|
verify=(not insecure))
|
||||||
|
|
||||||
client_kwargs = {}
|
client_kwargs = {}
|
||||||
if zun_url:
|
if not endpoint_override:
|
||||||
client_kwargs['endpoint_override'] = zun_url
|
|
||||||
|
|
||||||
if not zun_url:
|
|
||||||
try:
|
try:
|
||||||
# Trigger an auth error so that we can throw the exception
|
# Trigger an auth error so that we can throw the exception
|
||||||
# we always have
|
# we always have
|
||||||
session.get_endpoint(
|
session.get_endpoint(
|
||||||
service_type=service_type,
|
|
||||||
service_name=service_name,
|
service_name=service_name,
|
||||||
|
service_type=service_type,
|
||||||
interface=interface,
|
interface=interface,
|
||||||
region_name=region_name)
|
region_name=region_name
|
||||||
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise RuntimeError("Not Authorized")
|
raise RuntimeError('Not authorized')
|
||||||
|
else:
|
||||||
|
client_kwargs = {'endpoint_override': endpoint_override}
|
||||||
|
|
||||||
self.http_client = httpclient.SessionClient(
|
self.http_client = httpclient.SessionClient(service_type=service_type,
|
||||||
service_type=service_type,
|
service_name=service_name,
|
||||||
service_name=service_name,
|
interface=interface,
|
||||||
interface=interface,
|
region_name=region_name,
|
||||||
region_name=region_name,
|
session=session,
|
||||||
session=session,
|
api_version=api_version,
|
||||||
api_version=api_version,
|
**client_kwargs)
|
||||||
**client_kwargs)
|
|
||||||
self.containers = containers.ContainerManager(self.http_client)
|
self.containers = containers.ContainerManager(self.http_client)
|
||||||
self.images = images.ImageManager(self.http_client)
|
self.images = images.ImageManager(self.http_client)
|
||||||
self.services = services.ServiceManager(self.http_client)
|
self.services = services.ServiceManager(self.http_client)
|
||||||
self.hosts = hosts.HostManager(self.http_client)
|
self.hosts = hosts.HostManager(self.http_client)
|
||||||
|
|
||||||
profile = kwargs.pop("profile", None)
|
@property
|
||||||
if profiler and profile:
|
def api_version(self):
|
||||||
# Initialize the root of the future trace: the created trace ID
|
return self.http_client.api_version
|
||||||
# will be used as the very first parent to which all related
|
|
||||||
# traces will be bound to. The given HMAC key must correspond to
|
|
||||||
# the one set in zun-api zun.conf, otherwise the latter
|
|
||||||
# will fail to check the request signature and will skip
|
|
||||||
# initialization of osprofiler on the server side.
|
|
||||||
profiler.init(profile)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user