diff options
| author | Jamie Lennox <jamielennox@redhat.com> | 2015-03-09 15:53:18 +1100 |
|---|---|---|
| committer | Jamie Lennox <jamielennox@redhat.com> | 2015-03-18 10:25:55 +1100 |
| commit | e39eec0ff84185f476a1c4cd3014decd149ddf58 (patch) | |
| tree | 833285918b87fa1e8eb0a17aa3d9f484761bb9c2 /keystoneclient/auth | |
| parent | fc1f5a7963adb3c39f48131af5117bfafa3b07e7 (diff) | |
| download | python-keystoneclient-e39eec0ff84185f476a1c4cd3014decd149ddf58.tar.gz | |
Provide a generic auth plugin loader
For keystonemiddleware, shade and other projects that do more
complicated option loading than simply CLI or CONF file provide a means
to load an auth plugin where options are discovered by a provided
function.
This plugin is designed to work with the options as provided by
get_options rather than either the argparse or CONF registration
functions.
Use these as the default loading mechanism for the existing argparse and
CONF functions as it standardizes the mechanism between the two sources.
Change-Id: I15634ac30581c7aea14e709f12fb202570190f46
Closes-Bug: #1428900
Diffstat (limited to 'keystoneclient/auth')
| -rw-r--r-- | keystoneclient/auth/base.py | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/keystoneclient/auth/base.py b/keystoneclient/auth/base.py index a6c54f1..91cf86a 100644 --- a/keystoneclient/auth/base.py +++ b/keystoneclient/auth/base.py @@ -255,13 +255,11 @@ class BaseAuthPlugin(object): :returns: An auth plugin, or None if a name is not provided. :rtype: :py:class:`keystoneclient.auth.BaseAuthPlugin` """ - for opt in cls.get_options(): - val = getattr(namespace, 'os_%s' % opt.dest) - if val is not None: - val = opt.type(val) - kwargs.setdefault(opt.dest, val) - return cls.load_from_options(**kwargs) + def _getter(opt): + return getattr(namespace, 'os_%s' % opt.dest) + + return cls.load_from_options_getter(_getter, **kwargs) @classmethod def register_conf_options(cls, conf, group): @@ -287,10 +285,34 @@ class BaseAuthPlugin(object): :returns: An authentication Plugin. :rtype: :py:class:`keystoneclient.auth.BaseAuthPlugin` """ + + def _getter(opt): + return conf[group][opt.dest] + + return cls.load_from_options_getter(_getter, **kwargs) + + @classmethod + def load_from_options_getter(cls, 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 one value, an + :py:class:`oslo_config.cfg.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:`keystoneclient.auth.BaseAuthPlugin` + """ + plugin_opts = cls.get_options() for opt in plugin_opts: - val = conf[group][opt.dest] + val = getter(opt) if val is not None: val = opt.type(val) kwargs.setdefault(opt.dest, val) |
