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
This commit is contained in:
Jamie Lennox
2014-08-07 21:40:59 +10:00
parent 4b10d11c1a
commit 8f1605e30a
2 changed files with 57 additions and 30 deletions

View File

@@ -11,6 +11,7 @@
# under the License. # under the License.
import abc import abc
import os
import six import six
import stevedore import stevedore
@@ -121,3 +122,52 @@ class BaseAuthPlugin(object):
to create the plugin. to create the plugin.
""" """
return cls(**kwargs) 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)

View File

@@ -15,11 +15,6 @@ import os
from keystoneclient.auth import base 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): def register_argparse_arguments(parser, argv):
"""Register CLI options needed to create a plugin. """Register CLI options needed to create a plugin.
@@ -43,27 +38,16 @@ def register_argparse_arguments(parser, argv):
help='The auth plugin to load') help='The auth plugin to load')
options, _args = in_parser.parse_known_args(argv) options, _args = in_parser.parse_known_args(argv)
name = options.os_auth_plugin
if not options.os_auth_plugin: if not name:
return None 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) 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 return plugin
@@ -82,12 +66,5 @@ def load_from_argparse_arguments(namespace, **kwargs):
if not namespace.os_auth_plugin: if not namespace.os_auth_plugin:
return None return None
plugin_class = base.get_plugin_class(namespace.os_auth_plugin) plugin = base.get_plugin_class(namespace.os_auth_plugin)
return plugin.load_from_argparse_arguments(namespace, **kwargs)
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)