Merge "Individual plugin CLI registering"

This commit is contained in:
Jenkins
2014-08-14 08:08:43 +00:00
committed by Gerrit Code Review
2 changed files with 57 additions and 30 deletions

View File

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

View File

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