summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@redhat.com>2014-08-13 11:26:56 +1000
committerJamie Lennox <jamielennox@redhat.com>2014-08-21 09:26:37 +1000
commit8028d758ee4897862402e9b1a202006e13d3bad8 (patch)
treeddb4e7c53f8460e071c80e4ae5d7a78ce147e928
parent9cc2f7c73e92ae6c6b4fbf19272bb2f9209f45e7 (diff)
downloadpython-keystoneclient-8028d758ee4897862402e9b1a202006e13d3bad8.tar.gz
Allow providing a default value to CLI loading
Allow users to specify a default value to loading auth plugins from the CLI so that you can fallback to some default behaviour if the user doesn't specify a plugin. Change-Id: I44eb838f7ccc3b377dd1ba53dbb941e973e4a22e
-rw-r--r--keystoneclient/auth/cli.py27
-rw-r--r--keystoneclient/tests/auth/test_cli.py36
2 files changed, 55 insertions, 8 deletions
diff --git a/keystoneclient/auth/cli.py b/keystoneclient/auth/cli.py
index 8bbed2a..f1f2de3 100644
--- a/keystoneclient/auth/cli.py
+++ b/keystoneclient/auth/cli.py
@@ -14,9 +14,11 @@ import argparse
import os
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.
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 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.
:raises exceptions.NoMatchingPlugin: if a plugin cannot be created.
"""
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):
p.add_argument('--os-auth-plugin',
metavar='<name>',
@@ -38,15 +42,18 @@ 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 name:
+ if not options.os_auth_plugin:
return None
- msg = 'Options specific to the %s plugin.' % name
- group = parser.add_argument_group('Authentication Options', msg)
+ if isinstance(options.os_auth_plugin, type):
+ 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)
return plugin
@@ -66,5 +73,9 @@ def load_from_argparse_arguments(namespace, **kwargs):
if not namespace.os_auth_plugin:
return None
- plugin = base.get_plugin_class(namespace.os_auth_plugin)
+ if isinstance(namespace.os_auth_plugin, type):
+ plugin = namespace.os_auth_plugin
+ else:
+ plugin = base.get_plugin_class(namespace.os_auth_plugin)
+
return plugin.load_from_argparse_arguments(namespace, **kwargs)
diff --git a/keystoneclient/tests/auth/test_cli.py b/keystoneclient/tests/auth/test_cli.py
index 4d3289e..fc09195 100644
--- a/keystoneclient/tests/auth/test_cli.py
+++ b/keystoneclient/tests/auth/test_cli.py
@@ -104,6 +104,42 @@ class CliTests(utils.TestCase):
self.assertEqual(self.a_float, a['a_float'])
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):
TesterPlugin.register_argparse_arguments(self.p)
val = uuid.uuid4().hex