Add load_from_options_getter to plugin loading

Loading from getter function was introduced into keystoneclient as a way
to enable OCC and auth_token middleware to define more unusual loading
mechanisms.

It was skipped in the move to keystoneauth. This adds it back.

Closes-Bug: #1514339
Change-Id: I1d464917e0ddec8e13f9395ad75db6ccc53cb813
This commit is contained in:
Jamie Lennox
2015-11-23 13:17:57 +11:00
committed by Jamie Lennox
parent 1154120cbb
commit 6f9cb04625
4 changed files with 56 additions and 14 deletions
+23
View File
@@ -123,3 +123,26 @@ class BaseLoader(object):
raise exceptions.MissingRequiredOptions(missing_required)
return self.plugin_class(**kwargs)
def load_from_options_getter(self, getter, **kwargs):
"""Load a plugin from a getter function that returns appropriate values
To handle cases other than the provided CONF and CLI loading you can
specify a custom loader function that will be queried for the option
value.
The getter is a function that takes a
:py:class:`keystoneauth1.loading.Opt` and returns a value to load with.
:param getter: A function that returns a value for the given opt.
:type getter: callable
:returns: An authentication Plugin.
:rtype: :py:class:`keystoneauth1.plugin.BaseAuthPlugin`
"""
for opt in self.get_options():
val = getter(opt)
if val is not None:
val = opt.type(val)
kwargs.setdefault(opt.dest, val)
return self.load_from_options(**kwargs)
+3 -8
View File
@@ -97,12 +97,7 @@ def load_from_argparse_arguments(namespace, **kwargs):
else:
plugin = base.get_plugin_loader(namespace.os_auth_type)
plugin_opts = plugin.get_options()
def _getter(opt):
return getattr(namespace, 'os_%s' % opt.dest)
for opt in plugin_opts:
val = getattr(namespace, 'os_%s' % opt.dest)
if val is not None:
val = opt.type(val)
kwargs.setdefault(opt.dest, val)
return plugin.load_from_options(**kwargs)
return plugin.load_from_options_getter(_getter)
+3 -6
View File
@@ -129,10 +129,7 @@ def load_from_conf_options(conf, group, **kwargs):
conf.register_opts(oslo_opts, group=group)
for opt in plugin_opts:
val = conf[group][opt.dest]
if val is not None:
val = opt.type(val)
kwargs.setdefault(opt.dest, val)
def _getter(opt):
return conf[group][opt.dest]
return plugin.load_from_options(**kwargs)
return plugin.load_from_options_getter(_getter, **kwargs)
@@ -50,3 +50,30 @@ class LoadingTests(utils.TestCase):
for l in loaders.values():
self.assertIsInstance(l, loading.BaseLoader)
def test_loading_getter(self):
called_opts = []
vals = {'a-int': 44,
'a-bool': False,
'a-float': 99.99,
'a-str': 'value'}
val = uuid.uuid4().hex
def _getter(opt):
called_opts.append(opt.name)
# return str because oslo.config should convert them back
return str(vals[opt.name])
p = utils.MockLoader().load_from_options_getter(_getter, other=val)
self.assertEqual(set(vals), set(called_opts))
for k, v in vals.items():
# replace - to _ because it's the dest used to create kwargs
self.assertEqual(v, p[k.replace('-', '_')])
# check that additional kwargs get passed through
self.assertEqual(val, p['other'])