From 8f1605e30a0a563f5a9018b80c73aaabf6f5228d Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Thu, 7 Aug 2014 21:40:59 +1000 Subject: [PATCH] Individual plugin CLI registering Split the functions that load the auth plugins from CLI so that they can be used on a specific plugin. The intention here is to be able to turn the existing authentication options in shells into a new auth plugin and have that be loadable rather than maintain separate paths through the shells. Change-Id: I3dd5a8ed183d843246b1add3dfbf591ba4e2f94c --- keystoneclient/auth/base.py | 50 +++++++++++++++++++++++++++++++++++++ keystoneclient/auth/cli.py | 37 ++++++--------------------- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/keystoneclient/auth/base.py b/keystoneclient/auth/base.py index 4a43c4cc3..147433ea0 100644 --- a/keystoneclient/auth/base.py +++ b/keystoneclient/auth/base.py @@ -11,6 +11,7 @@ # under the License. import abc +import os import six import stevedore @@ -121,3 +122,52 @@ class BaseAuthPlugin(object): to create the plugin. """ return cls(**kwargs) + + @classmethod + def register_argparse_arguments(cls, parser): + """Register the CLI options provided by a specific plugin. + + Given a plugin class convert it's options into argparse arguments and + add them to a parser. + + :param AuthPlugin plugin: an auth plugin class. + :param argparse.ArgumentParser: the parser to attach argparse options. + """ + + # NOTE(jamielennox): ideally oslo.config would be smart enough to + # handle all the Opt manipulation that goes on in this file. However it + # is currently not. Options are handled in as similar a way as + # possible to oslo.config such that when available we should be able to + # transition. + + for opt in cls.get_options(): + if opt.default is None: + env_name = opt.name.replace('-', '_').upper() + default = os.environ.get('OS_' + env_name) + else: + default = opt.default + + parser.add_argument('--os-' + opt.name, + default=default, + metavar=opt.metavar, + help=opt.help, + dest=opt.dest) + + @classmethod + def load_from_argparse_arguments(cls, namespace, **kwargs): + """Load a specific plugin object from an argparse result. + + Convert the results of a parse into the specified plugin. + + :param AuthPlugin plugin: an auth plugin class. + :param Namespace namespace: The result from CLI parsing. + + :returns: An auth plugin, or None if a name is not provided. + """ + for opt in cls.get_options(): + val = getattr(namespace, opt.dest) + if val is not None: + val = opt.type(val) + kwargs.setdefault(opt.dest, val) + + return cls.load_from_options(**kwargs) diff --git a/keystoneclient/auth/cli.py b/keystoneclient/auth/cli.py index adce3dcab..8bbed2ae7 100644 --- a/keystoneclient/auth/cli.py +++ b/keystoneclient/auth/cli.py @@ -15,11 +15,6 @@ import os from keystoneclient.auth import base -# NOTE(jamielennox): ideally oslo.config would be smart enough to handle all -# the Opt manipulation that goes on in this file. However it is currently not. -# Options are handled in as similar a way as possible to oslo.config such that -# when available we should be able to transition. - def register_argparse_arguments(parser, argv): """Register CLI options needed to create a plugin. @@ -43,27 +38,16 @@ def register_argparse_arguments(parser, argv): help='The auth plugin to load') options, _args = in_parser.parse_known_args(argv) + name = options.os_auth_plugin - if not options.os_auth_plugin: + if not name: return None - msg = 'Options specific to the %s plugin.' % options.os_auth_plugin + msg = 'Options specific to the %s plugin.' % name group = parser.add_argument_group('Authentication Options', msg) - plugin = base.get_plugin_class(options.os_auth_plugin) - - for opt in plugin.get_options(): - if opt.default is None: - env_name = opt.name.replace('-', '_').upper() - default = os.environ.get('OS_' + env_name) - else: - default = opt.default - - group.add_argument('--os-' + opt.name, - default=default, - metavar=opt.metavar, - help=opt.help, - dest=opt.dest) + plugin = base.get_plugin_class(name) + plugin.register_argparse_arguments(group) return plugin @@ -82,12 +66,5 @@ def load_from_argparse_arguments(namespace, **kwargs): if not namespace.os_auth_plugin: return None - plugin_class = base.get_plugin_class(namespace.os_auth_plugin) - - for opt in plugin_class.get_options(): - val = getattr(namespace, opt.dest) - if val is not None: - val = opt.type(val) - kwargs.setdefault(opt.dest, val) - - return plugin_class.load_from_options(**kwargs) + plugin = base.get_plugin_class(namespace.os_auth_plugin) + return plugin.load_from_argparse_arguments(namespace, **kwargs)