From a9115b4cd8dd18f74286e7047d1d4196d17ce1b7 Mon Sep 17 00:00:00 2001 From: Itisha Dewan Date: Tue, 14 Jun 2016 06:57:02 +0400 Subject: [PATCH] switch from keystoneclient to keystoneauth move glanceclient to keystoneauth as keystoneclient's auth session, plugins and adapter code has been deprecated. refer to [1] for more information. 1: https://github.com/openstack/python-keystoneclient/commit/1a84e24fa4ce6d3169b59e385f35b2a63f2257f0 implements bp: use-keystoneauth Co-Authored-By: Itisha Change-Id: I88fb327628e1bec48dc391f50d66b3deab4a8ab9 --- glanceclient/client.py | 4 +- glanceclient/common/http.py | 8 +-- glanceclient/shell.py | 71 ++++++++++--------- glanceclient/tests/unit/test_http.py | 4 +- glanceclient/tests/unit/test_shell.py | 10 +-- .../bp-use-keystoneauth-e12f300e58577b13.yaml | 11 +++ requirements.txt | 2 +- 7 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 releasenotes/notes/bp-use-keystoneauth-e12f300e58577b13.yaml diff --git a/glanceclient/client.py b/glanceclient/client.py index db2a4f79..714c96ab 100644 --- a/glanceclient/client.py +++ b/glanceclient/client.py @@ -25,8 +25,8 @@ def Client(version=None, endpoint=None, session=None, *args, **kwargs): for specific details. :param string version: The version of API to use. - :param session: A keystoneclient session that should be used for transport. - :type session: keystoneclient.session.Session + :param session: A keystoneauth1 session that should be used for transport. + :type session: keystoneauth1.session.Session """ # FIXME(jamielennox): Add a deprecation warning if no session is passed. # Leaving it as an option until we can ensure nothing break when we switch. diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index 352ba10c..dbca1428 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -17,8 +17,8 @@ import copy import logging import socket -from keystoneclient import adapter -from keystoneclient import exceptions as ksc_exc +from keystoneauth1 import adapter +from keystoneauth1 import exceptions as ksa_exc from oslo_utils import importutils from oslo_utils import netutils import requests @@ -329,13 +329,13 @@ class SessionClient(adapter.Adapter, _BaseHTTPClient): headers=headers, data=data, **kwargs) - except ksc_exc.RequestTimeout as e: + except ksa_exc.ConnectTimeout as e: conn_url = self.get_endpoint(auth=kwargs.get('auth')) conn_url = "%s/%s" % (conn_url.rstrip('/'), url.lstrip('/')) message = ("Error communicating with %(url)s %(e)s" % dict(url=conn_url, e=e)) raise exc.InvalidEndpoint(message=message) - except ksc_exc.ConnectionRefused as e: + except ksa_exc.ConnectFailure as e: conn_url = self.get_endpoint(auth=kwargs.get('auth')) conn_url = "%s/%s" % (conn_url.rstrip('/'), url.lstrip('/')) message = ("Error finding address for %(url)s: %(e)s" % diff --git a/glanceclient/shell.py b/glanceclient/shell.py index 298d8ea0..51e02a67 100644 --- a/glanceclient/shell.py +++ b/glanceclient/shell.py @@ -38,11 +38,11 @@ from glanceclient._i18n import _ from glanceclient.common import utils from glanceclient import exc -from keystoneclient.auth.identity import v2 as v2_auth -from keystoneclient.auth.identity import v3 as v3_auth -from keystoneclient import discover -from keystoneclient import exceptions as ks_exc -from keystoneclient import session +from keystoneauth1 import discover +from keystoneauth1 import exceptions as ks_exc +from keystoneauth1.identity import v2 as v2_auth +from keystoneauth1.identity import v3 as v3_auth +from keystoneauth1 import loading osprofiler_profiler = importutils.try_import("osprofiler.profiler") @@ -51,10 +51,14 @@ SUPPORTED_VERSIONS = [1, 2] class OpenStackImagesShell(object): - def _append_global_identity_args(self, parser): + def _append_global_identity_args(self, parser, argv): # register common identity args - session.Session.register_cli_options(parser) - v3_auth.Password.register_argparse_arguments(parser) + parser.set_defaults(os_auth_url=utils.env('OS_AUTH_URL')) + + parser.set_defaults(os_project_name=utils.env( + 'OS_PROJECT_NAME', 'OS_TENANT_NAME')) + parser.set_defaults(os_project_id=utils.env( + 'OS_PROJECT_ID', 'OS_TENANT_ID')) parser.add_argument('--key-file', dest='os_key', @@ -68,17 +72,9 @@ class OpenStackImagesShell(object): dest='os_cert', help='DEPRECATED! Use --os-cert.') - parser.add_argument('--os-tenant-id', - default=utils.env('OS_TENANT_ID'), - help='Defaults to env[OS_TENANT_ID].') - parser.add_argument('--os_tenant_id', help=argparse.SUPPRESS) - parser.add_argument('--os-tenant-name', - default=utils.env('OS_TENANT_NAME'), - help='Defaults to env[OS_TENANT_NAME].') - parser.add_argument('--os_tenant_name', help=argparse.SUPPRESS) @@ -110,7 +106,19 @@ class OpenStackImagesShell(object): parser.add_argument('--os_endpoint_type', help=argparse.SUPPRESS) - def get_base_parser(self): + loading.register_session_argparse_arguments(parser) + # Peek into argv to see if os-auth-token (or the deprecated + # os_auth_token) or the new os-token or the environment variable + # OS_AUTH_TOKEN were given. In which case, the token auth plugin is + # what the user wants. Else, we'll default to password. + default_auth_plugin = 'password' + token_opts = ['os-token', 'os-auth-token', 'os_auth-token'] + if argv and any(i in token_opts for i in argv): + default_auth_plugin = 'token' + loading.register_auth_argparse_arguments( + parser, argv, default=default_auth_plugin) + + def get_base_parser(self, argv): parser = argparse.ArgumentParser( prog='glance', description=__doc__.strip(), @@ -194,12 +202,12 @@ class OpenStackImagesShell(object): 'the profiling will not be triggered even ' 'if osprofiler is enabled on server side.') - self._append_global_identity_args(parser) + self._append_global_identity_args(parser, argv) return parser - def get_subcommand_parser(self, version): - parser = self.get_base_parser() + def get_subcommand_parser(self, version, argv=None): + parser = self.get_base_parser(argv) self.subcommands = {} subparsers = parser.add_subparsers(metavar='') @@ -261,7 +269,7 @@ class OpenStackImagesShell(object): v2_auth_url = None v3_auth_url = None try: - ks_discover = discover.Discover(session=session, auth_url=auth_url) + ks_discover = discover.Discover(session=session, url=auth_url) v2_auth_url = ks_discover.url_for('2.0') v3_auth_url = ks_discover.url_for('3.0') except ks_exc.ClientException as e: @@ -372,16 +380,11 @@ class OpenStackImagesShell(object): "or prompted response")) # Validate password flow auth - project_info = ( - args.os_tenant_name or args.os_tenant_id or ( - args.os_project_name and ( - args.os_project_domain_name or - args.os_project_domain_id - ) - ) or args.os_project_id - ) - - if not project_info: + os_project_name = getattr( + args, 'os_project_name', getattr(args, 'os_tenant_name', None)) + os_project_id = getattr( + args, 'os_project_id', getattr(args, 'os_tenant_id', None)) + if not any([os_project_name, os_project_id]): # tenant is deprecated in Keystone v3. Use the latest # terminology instead. raise exc.CommandError( @@ -432,7 +435,7 @@ class OpenStackImagesShell(object): 'ssl_compression': args.ssl_compression } else: - ks_session = session.Session.load_from_cli_options(args) + ks_session = loading.load_session_from_argparse_arguments(args) auth_plugin_kwargs = self._get_kwargs_to_create_auth_plugin(args) ks_session.auth = self._get_keystone_auth_plugin( ks_session=ks_session, **auth_plugin_kwargs) @@ -490,7 +493,7 @@ class OpenStackImagesShell(object): def _get_subparser(api_version): try: - return self.get_subcommand_parser(api_version) + return self.get_subcommand_parser(api_version, argv) except ImportError as e: if not str(e): # Add a generic import error message if the raised @@ -504,7 +507,7 @@ class OpenStackImagesShell(object): # NOTE(flepied) Under Python3, parsed arguments are removed # from the list so make a copy for the first parsing base_argv = copy.deepcopy(argv) - parser = self.get_base_parser() + parser = self.get_base_parser(argv) (options, args) = parser.parse_known_args(base_argv) try: diff --git a/glanceclient/tests/unit/test_http.py b/glanceclient/tests/unit/test_http.py index 020e146c..8e119c33 100644 --- a/glanceclient/tests/unit/test_http.py +++ b/glanceclient/tests/unit/test_http.py @@ -15,8 +15,8 @@ import functools import json -from keystoneclient.auth import token_endpoint -from keystoneclient import session +from keystoneauth1 import session +from keystoneauth1 import token_endpoint import mock import requests from requests_mock.contrib import fixture diff --git a/glanceclient/tests/unit/test_shell.py b/glanceclient/tests/unit/test_shell.py index 05af9240..d175852b 100644 --- a/glanceclient/tests/unit/test_shell.py +++ b/glanceclient/tests/unit/test_shell.py @@ -27,8 +27,8 @@ import traceback import uuid import fixtures -from keystoneclient import exceptions as ks_exc -from keystoneclient import fixture as ks_fixture +from keystoneauth1 import exceptions as ks_exc +from keystoneauth1 import fixture as ks_fixture import mock from requests_mock.contrib import fixture as rm_fixture import six @@ -250,7 +250,9 @@ class ShellTest(testutils.TestCase): def test_get_base_parser(self): test_shell = openstack_shell.OpenStackImagesShell() - actual_parser = test_shell.get_base_parser() + # NOTE(stevemar): Use the current sys.argv for base_parser since it + # doesn't matter for this test, it just needs to initialize the CLI + actual_parser = test_shell.get_base_parser(sys.argv) description = 'Command-line interface to the OpenStack Images API.' expected = argparse.ArgumentParser( prog='glance', usage=None, @@ -637,7 +639,7 @@ class ShellTestWithKeystoneV3Auth(ShellTest): glance_shell.main(args.split()) self.assertEqual(0, self.v3_auth.call_count) - @mock.patch('keystoneclient.discover.Discover', + @mock.patch('keystoneauth1.discover.Discover', side_effect=ks_exc.ClientException()) def test_api_discovery_failed_with_unversioned_auth_url(self, discover): diff --git a/releasenotes/notes/bp-use-keystoneauth-e12f300e58577b13.yaml b/releasenotes/notes/bp-use-keystoneauth-e12f300e58577b13.yaml new file mode 100644 index 00000000..04eb2b96 --- /dev/null +++ b/releasenotes/notes/bp-use-keystoneauth-e12f300e58577b13.yaml @@ -0,0 +1,11 @@ +--- +prelude: > + Switch to using keystoneauth for session and auth plugins. +other: + - > + [`bp use-keystoneauth `_] + As of keystoneclient 2.2.0, the session and auth plugins code has + been deprecated. These modules have been moved to the keystoneauth + library. Consumers of the session and plugin modules are encouraged + to move to keystoneauth. Note that there should be no change to + end users of glanceclient. diff --git a/requirements.txt b/requirements.txt index 39807ac0..0bcfd41c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ pbr>=1.6 # Apache-2.0 Babel>=2.3.4 # BSD PrettyTable<0.8,>=0.7 # BSD -python-keystoneclient!=2.1.0,>=2.0.0 # Apache-2.0 +keystoneauth1>=2.10.0 # Apache-2.0 requests>=2.10.0 # Apache-2.0 warlock!=1.3.0,<2,>=1.0.1 # Apache-2.0 six>=1.9.0 # MIT