Tweak the way plugin attributes are loaded

By using the setdefault method to add plugin parameters to the kwargs
passed to load_from_options() we are unnecessarily calling the getter
function when the value may not be saved.

This has generally not been a problem however if we start doing things
like prompting users for a password in the getter this is an expensive
and disruptive operation for a parameter that may not be stored.

Change-Id: Ic8dc3a425662c4a89372f178b175b8caab44e024
This commit is contained in:
Jamie Lennox
2015-11-24 10:12:46 +11:00
parent 912dad2e80
commit 8c49f441b2
2 changed files with 25 additions and 2 deletions
+2 -2
View File
@@ -139,10 +139,10 @@ class BaseLoader(object):
:returns: An authentication Plugin.
:rtype: :py:class:`keystoneauth1.plugin.BaseAuthPlugin`
"""
for opt in self.get_options():
for opt in (o for o in self.get_options() if o.dest not in kwargs):
val = getter(opt)
if val is not None:
val = opt.type(val)
kwargs.setdefault(opt.dest, val)
kwargs[opt.dest] = val
return self.load_from_options(**kwargs)
@@ -77,3 +77,26 @@ class LoadingTests(utils.TestCase):
# check that additional kwargs get passed through
self.assertEqual(val, p['other'])
def test_loading_getter_with_kwargs(self):
called_opts = set()
vals = {'a-bool': False,
'a-float': 99.99}
def _getter(opt):
called_opts.add(opt.name)
# return str because oslo.config should convert them back
return str(vals[opt.name])
p = utils.MockLoader().load_from_options_getter(_getter,
a_int=66,
a_str='another')
# only the options not passed by kwargs should get passed to getter
self.assertEqual(set(('a-bool', 'a-float')), called_opts)
self.assertEqual(False, p['a_bool'])
self.assertEqual(99.99, p['a_float'])
self.assertEqual('another', p['a_str'])
self.assertEqual(66, p['a_int'])