Skip to content

Commit b86915a

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Refactor check_valid_auth_options function"
2 parents 6c79919 + 78ae571 commit b86915a

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

openstackclient/api/auth.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,29 +147,28 @@ def check_valid_authorization_options(options, auth_plugin_name):
147147
def check_valid_authentication_options(options, auth_plugin_name):
148148
"""Validate authentication options, and provide helpful error messages."""
149149

150+
# Get all the options defined within the plugin.
151+
plugin_opts = base.get_plugin_options(auth_plugin_name)
152+
plugin_opts = {opt.dest: opt for opt in plugin_opts}
153+
154+
# NOTE(aloga): this is an horrible hack. We need a way to specify the
155+
# required options in the plugins. Using the "required" argument for
156+
# the oslo_config.cfg.Opt does not work, as it is not possible to load the
157+
# plugin if the option is not defined, so the error will simply be:
158+
# "NoMatchingPlugin: The plugin foobar could not be found"
150159
msgs = []
151-
if auth_plugin_name.endswith('password'):
152-
if not options.auth.get('username'):
153-
msgs.append(_('Set a username with --os-username, OS_USERNAME,'
154-
' or auth.username'))
155-
if not options.auth.get('auth_url'):
156-
msgs.append(_('Set an authentication URL, with --os-auth-url,'
157-
' OS_AUTH_URL or auth.auth_url'))
158-
elif auth_plugin_name.endswith('token'):
159-
if not options.auth.get('token'):
160-
msgs.append(_('Set a token with --os-token, OS_TOKEN or '
161-
'auth.token'))
162-
if not options.auth.get('auth_url'):
163-
msgs.append(_('Set a service AUTH_URL, with --os-auth-url, '
164-
'OS_AUTH_URL or auth.auth_url'))
165-
elif auth_plugin_name == 'token_endpoint':
166-
if not options.auth.get('token'):
167-
msgs.append(_('Set a token with --os-token, OS_TOKEN or '
168-
'auth.token'))
169-
if not options.auth.get('url'):
170-
msgs.append(_('Set a service URL, with --os-url, OS_URL or '
171-
'auth.url'))
172-
160+
if 'password' in plugin_opts and not options.auth.get('username'):
161+
msgs.append(_('Set a username with --os-username, OS_USERNAME,'
162+
' or auth.username'))
163+
if 'auth_url' in plugin_opts and not options.auth.get('auth_url'):
164+
msgs.append(_('Set a service AUTH_URL, with --os-auth-url, '
165+
'OS_AUTH_URL or auth.auth_url'))
166+
if 'url' in plugin_opts and not options.auth.get('url'):
167+
msgs.append(_('Set a service URL, with --os-url, '
168+
'OS_URL or auth.url'))
169+
if 'token' in plugin_opts and not options.auth.get('token'):
170+
msgs.append(_('Set a token with --os-token, '
171+
'OS_TOKEN or auth.token'))
173172
if msgs:
174173
raise exc.CommandError(
175174
_('Missing parameter(s): \n%s') % '\n'.join(msgs))

0 commit comments

Comments
 (0)