66 :license: MIT, see LICENSE for more details.
77"""
88# pylint: disable=invalid-name
9+ import time
10+ import warnings
11+
912import json
1013import logging
1114import requests
12- import warnings
13- import time
15+
1416
1517from SoftLayer import auth as slauth
1618from SoftLayer import config
1719from SoftLayer import consts
1820from SoftLayer import exceptions
1921from SoftLayer import transports
2022
21- from pprint import pprint as pp
2223LOGGER = logging .getLogger (__name__ )
2324API_PUBLIC_ENDPOINT = consts .API_PUBLIC_ENDPOINT
2425API_PRIVATE_ENDPOINT = consts .API_PRIVATE_ENDPOINT
@@ -188,7 +189,7 @@ def __init__(self, auth=None, transport=None, config_file=None):
188189 verify = self .settings ['softlayer' ].getboolean ('verify' ),
189190 )
190191
191- self .transport = transport
192+ self .transport = transport
192193
193194 def authenticate_with_password (self , username , password ,
194195 security_question_id = None ,
@@ -357,15 +358,15 @@ def __repr__(self):
357358 def __len__ (self ):
358359 return 0
359360
361+
360362class IAMClient (BaseClient ):
361363 """IBM ID Client for using IAM authentication
362364
363365 :param auth: auth driver that looks like SoftLayer.auth.AuthenticationBase
364366 :param transport: An object that's callable with this signature: transport(SoftLayer.transports.Request)
365367 """
366368
367-
368- def authenticate_with_password (self , username , password ):
369+ def authenticate_with_password (self , username , password , security_question_id = None , security_question_answer = None ):
369370 """Performs IBM IAM Username/Password Authentication
370371
371372 :param string username: your IBMid username
@@ -394,14 +395,14 @@ def authenticate_with_password(self, username, password):
394395 auth = requests .auth .HTTPBasicAuth ('bx' , 'bx' )
395396 )
396397 if response .status_code != 200 :
397- LOGGER .error ("Unable to login: {}" . format ( response .text ) )
398+ LOGGER .error ("Unable to login: %s" , response .text )
398399
399400 response .raise_for_status ()
400401
401402 tokens = json .loads (response .text )
402403 self .settings ['softlayer' ]['access_token' ] = tokens ['access_token' ]
403404 self .settings ['softlayer' ]['refresh_token' ] = tokens ['refresh_token' ]
404-
405+
405406 config .write_config (self .settings , self .config_file )
406407 self .auth = slauth .BearerAuthentication ('' , tokens ['access_token' ], tokens ['refresh_token' ])
407408
@@ -434,7 +435,7 @@ def authenticate_with_passcode(self, passcode):
434435 auth = requests .auth .HTTPBasicAuth ('bx' , 'bx' )
435436 )
436437 if response .status_code != 200 :
437- LOGGER .error ("Unable to login: {}" . format ( response .text ) )
438+ LOGGER .error ("Unable to login: %s" , response .text )
438439
439440 response .raise_for_status ()
440441
@@ -443,7 +444,7 @@ def authenticate_with_passcode(self, passcode):
443444 self .settings ['softlayer' ]['refresh_token' ] = tokens ['refresh_token' ]
444445 a_expire = time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (tokens ['expiration' ]))
445446 r_expire = time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (tokens ['refresh_token_expiration' ]))
446- LOGGER .warning ("Tokens retrieved, expires at {} , Refresh expires at {}" . format ( a_expire , r_expire ) )
447+ LOGGER .warning ("Tokens retrieved, expires at %s , Refresh expires at %s" , a_expire , r_expire )
447448 config .write_config (self .settings , self .config_file )
448449 self .auth = slauth .BearerAuthentication ('' , tokens ['access_token' ], tokens ['refresh_token' ])
449450
@@ -489,16 +490,16 @@ def refresh_iam_token(self, r_token, account_id=None, ims_account=None):
489490 headers = headers ,
490491 auth = requests .auth .HTTPBasicAuth ('bx' , 'bx' )
491492 )
492-
493+
493494 if response .status_code != 200 :
494- LOGGER .warning ("Unable to refresh IAM Token. {}" . format ( response .text ) )
495-
495+ LOGGER .warning ("Unable to refresh IAM Token. %s" , response .text )
496+
496497 response .raise_for_status ()
497-
498+
498499 tokens = json .loads (response .text )
499500 a_expire = time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (tokens ['expiration' ]))
500501 r_expire = time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (tokens ['refresh_token_expiration' ]))
501- LOGGER .warning ("Successfully refreshed Tokens. Expires at {} , Refresh expires at {}" . format ( a_expire , r_expire ) )
502+ LOGGER .warning ("Tokens retrieved, expires at %s , Refresh expires at %s" , a_expire , r_expire )
502503
503504 self .settings ['softlayer' ]['access_token' ] = tokens ['access_token' ]
504505 self .settings ['softlayer' ]['refresh_token' ] = tokens ['refresh_token' ]
@@ -513,9 +514,7 @@ def call(self, service, method, *args, **kwargs):
513514 except exceptions .SoftLayerAPIError as ex :
514515
515516 if ex .faultCode == 401 :
516- LOGGER .warning ("Token has expired, trying to refresh. {}" .format (ex .faultString ))
517- # self.refresh_iam_token(r_token)
518- # return super().call(service, method, *args, **kwargs)
517+ LOGGER .warning ("Token has expired, trying to refresh. %s" , ex .faultString )
519518 return ex
520519 else :
521520 raise ex
0 commit comments