1616import logging
1717
1818from os_client_config import config
19+ from os_client_config import exceptions as occ_exceptions
1920
2021
2122LOG = logging .getLogger (__name__ )
@@ -182,6 +183,14 @@ def auth_config_hook(self, config):
182183 LOG .debug ("auth_config_hook(): %s" % config )
183184 return config
184185
186+ def load_auth_plugin (self , config ):
187+ """Get auth plugin and validate args"""
188+
189+ loader = self ._get_auth_loader (config )
190+ config = self ._validate_auth (config , loader )
191+ auth_plugin = loader .load_from_options (** config ['auth' ])
192+ return auth_plugin
193+
185194 def _validate_auth_ksc (self , config , cloud , fixed_argparse = None ):
186195 """Old compatibility hack for OSC, no longer needed/wanted"""
187196 return config
@@ -192,6 +201,8 @@ def _validate_auth(self, config, loader, fixed_argparse=None):
192201
193202 plugin_options = loader .get_options ()
194203
204+ msgs = []
205+ prompt_options = []
195206 for p_opt in plugin_options :
196207 # if it's in config, win, move it and kill it from config dict
197208 # if it's in config.auth but not in config we're good
@@ -202,6 +213,16 @@ def _validate_auth(self, config, loader, fixed_argparse=None):
202213 winning_value = self ._find_winning_auth_value (
203214 p_opt , config ['auth' ])
204215
216+ # if the plugin tells us that this value is required
217+ # then error if it's doesn't exist now
218+ if not winning_value and p_opt .required :
219+ msgs .append (
220+ 'Missing value {auth_key}'
221+ ' required for auth plugin {plugin}' .format (
222+ auth_key = p_opt .name , plugin = config .get ('auth_type' ),
223+ )
224+ )
225+
205226 # Clean up after ourselves
206227 for opt in [p_opt .name ] + [o .name for o in p_opt .deprecated ]:
207228 opt = opt .replace ('-' , '_' )
@@ -224,6 +245,13 @@ def _validate_auth(self, config, loader, fixed_argparse=None):
224245 p_opt .dest not in config ['auth' ] and
225246 self ._pw_callback is not None
226247 ):
248+ # Defer these until we know all required opts are present
249+ prompt_options .append (p_opt )
250+
251+ if msgs :
252+ raise occ_exceptions .OpenStackConfigException ('\n ' .join (msgs ))
253+ else :
254+ for p_opt in prompt_options :
227255 config ['auth' ][p_opt .dest ] = self ._pw_callback (p_opt .prompt )
228256
229257 return config
0 commit comments