Merge "Allow providing a default value to CLI loading"
This commit is contained in:
@@ -14,9 +14,11 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from keystoneclient.auth import base
|
from keystoneclient.auth import base
|
||||||
|
from keystoneclient import utils
|
||||||
|
|
||||||
|
|
||||||
def register_argparse_arguments(parser, argv):
|
@utils.positional()
|
||||||
|
def register_argparse_arguments(parser, argv, default=None):
|
||||||
"""Register CLI options needed to create a plugin.
|
"""Register CLI options needed to create a plugin.
|
||||||
|
|
||||||
The function inspects the provided arguments so that it can also register
|
The function inspects the provided arguments so that it can also register
|
||||||
@@ -24,13 +26,15 @@ def register_argparse_arguments(parser, argv):
|
|||||||
|
|
||||||
:param argparse.ArgumentParser: the parser to attach argparse options to.
|
:param argparse.ArgumentParser: the parser to attach argparse options to.
|
||||||
:param list argv: the arguments provided to the appliation.
|
:param list argv: the arguments provided to the appliation.
|
||||||
|
:param str/class default: a default plugin name or a plugin object to use
|
||||||
|
if one isn't specified by the CLI. default: None.
|
||||||
|
|
||||||
:returns: The plugin class that will be loaded or None if not provided.
|
:returns: The plugin class that will be loaded or None if not provided.
|
||||||
|
|
||||||
:raises exceptions.NoMatchingPlugin: if a plugin cannot be created.
|
:raises exceptions.NoMatchingPlugin: if a plugin cannot be created.
|
||||||
"""
|
"""
|
||||||
in_parser = argparse.ArgumentParser(add_help=False)
|
in_parser = argparse.ArgumentParser(add_help=False)
|
||||||
env_plugin = os.environ.get('OS_AUTH_PLUGIN')
|
env_plugin = os.environ.get('OS_AUTH_PLUGIN', default)
|
||||||
for p in (in_parser, parser):
|
for p in (in_parser, parser):
|
||||||
p.add_argument('--os-auth-plugin',
|
p.add_argument('--os-auth-plugin',
|
||||||
metavar='<name>',
|
metavar='<name>',
|
||||||
@@ -38,15 +42,18 @@ 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 name:
|
if not options.os_auth_plugin:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
msg = 'Options specific to the %s plugin.' % name
|
if isinstance(options.os_auth_plugin, type):
|
||||||
group = parser.add_argument_group('Authentication Options', msg)
|
msg = 'Default Authentication options'
|
||||||
|
plugin = options.os_auth_plugin
|
||||||
|
else:
|
||||||
|
msg = 'Options specific to the %s plugin.' % options.os_auth_plugin
|
||||||
|
plugin = base.get_plugin_class(options.os_auth_plugin)
|
||||||
|
|
||||||
plugin = base.get_plugin_class(name)
|
group = parser.add_argument_group('Authentication Options', msg)
|
||||||
plugin.register_argparse_arguments(group)
|
plugin.register_argparse_arguments(group)
|
||||||
return plugin
|
return plugin
|
||||||
|
|
||||||
@@ -66,5 +73,9 @@ def load_from_argparse_arguments(namespace, **kwargs):
|
|||||||
if not namespace.os_auth_plugin:
|
if not namespace.os_auth_plugin:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if isinstance(namespace.os_auth_plugin, type):
|
||||||
|
plugin = namespace.os_auth_plugin
|
||||||
|
else:
|
||||||
plugin = 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)
|
return plugin.load_from_argparse_arguments(namespace, **kwargs)
|
||||||
|
@@ -104,6 +104,42 @@ class CliTests(utils.TestCase):
|
|||||||
self.assertEqual(self.a_float, a['a_float'])
|
self.assertEqual(self.a_float, a['a_float'])
|
||||||
self.assertEqual(3, a['a_int'])
|
self.assertEqual(3, a['a_int'])
|
||||||
|
|
||||||
|
@utils.mock_plugin
|
||||||
|
def test_with_default_string_value(self, m):
|
||||||
|
name = uuid.uuid4().hex
|
||||||
|
klass = cli.register_argparse_arguments(self.p, [], default=name)
|
||||||
|
self.assertIs(utils.MockPlugin, klass)
|
||||||
|
m.assert_called_once_with(name)
|
||||||
|
|
||||||
|
@utils.mock_plugin
|
||||||
|
def test_overrides_default_string_value(self, m):
|
||||||
|
name = uuid.uuid4().hex
|
||||||
|
default = uuid.uuid4().hex
|
||||||
|
argv = ['--os-auth-plugin', name]
|
||||||
|
klass = cli.register_argparse_arguments(self.p, argv, default=default)
|
||||||
|
self.assertIs(utils.MockPlugin, klass)
|
||||||
|
m.assert_called_once_with(name)
|
||||||
|
|
||||||
|
@utils.mock_plugin
|
||||||
|
def test_with_default_type_value(self, m):
|
||||||
|
klass = cli.register_argparse_arguments(self.p, [],
|
||||||
|
default=utils.MockPlugin)
|
||||||
|
self.assertIs(utils.MockPlugin, klass)
|
||||||
|
self.assertEqual(0, m.call_count)
|
||||||
|
|
||||||
|
@utils.mock_plugin
|
||||||
|
def test_overrides_default_type_value(self, m):
|
||||||
|
# using this test plugin would fail if called because there
|
||||||
|
# is no get_options() function
|
||||||
|
class TestPlugin(object):
|
||||||
|
pass
|
||||||
|
name = uuid.uuid4().hex
|
||||||
|
argv = ['--os-auth-plugin', name]
|
||||||
|
klass = cli.register_argparse_arguments(self.p, argv,
|
||||||
|
default=TestPlugin)
|
||||||
|
self.assertIs(utils.MockPlugin, klass)
|
||||||
|
m.assert_called_once_with(name)
|
||||||
|
|
||||||
def test_deprecated_cli_options(self):
|
def test_deprecated_cli_options(self):
|
||||||
TesterPlugin.register_argparse_arguments(self.p)
|
TesterPlugin.register_argparse_arguments(self.p)
|
||||||
val = uuid.uuid4().hex
|
val = uuid.uuid4().hex
|
||||||
|
Reference in New Issue
Block a user