Skip to content

Commit

Permalink
Attempt to relogin forever regardless of the error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Smith committed Jan 17, 2018
1 parent d9ba746 commit 6171e4b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions acitoolkit/acisession.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ def run(self):
self._apic.resubscribe()
if resp.ok:
self._check_callbacks()
else:
logging.error('Could not relogin to APIC.')
self._login_timeout = 30
except ConnectionError:
logging.error('Could not relogin to APIC due to ConnectionError')
self._apic.login_error = True
Expand Down Expand Up @@ -441,7 +444,8 @@ class Session(object):
This class is responsible for all communication with the APIC.
"""
def __init__(self, url, uid, pwd=None, cert_name=None, key=None, verify_ssl=False,
appcenter_user=False, subscription_enabled=True, proxies=None):
appcenter_user=False, subscription_enabled=True, proxies=None,
relogin_forever=False):
"""
:param url: String containing the APIC URL such as ``https://1.2.3.4``
:param uid: String containing the username that will be used as\
Expand All @@ -459,7 +463,8 @@ def __init__(self, url, uid, pwd=None, cert_name=None, key=None, verify_ssl=Fals
the context of an APIC appcenter app
:param proxies: Optional dictionary containing the proxies passed\
directly to the Requests library
:param relogin_forever: Boolean that when set to True will attempt to re-login
forever regardless of the error returned from APIC.
"""
if not isinstance(url, str):
url = str(url)
Expand All @@ -484,6 +489,8 @@ def __init__(self, url, uid, pwd=None, cert_name=None, key=None, verify_ssl=Fals
raise CredentialsError("The key path must be a string")
if (cert_name and not key) or (not cert_name and key):
raise CredentialsError("Both a certificate name and private key must be provided")
if not isinstance(relogin_forever, bool):
raise CredentialsError("relogin_forever must be a boolean")

if 'https://' in url:
self.ipaddr = url[len('https://'):]
Expand Down Expand Up @@ -530,6 +537,7 @@ def __init__(self, url, uid, pwd=None, cert_name=None, key=None, verify_ssl=Fals
self._relogin_callbacks = []
self.login_error = False
self._logged_in = False
self.relogin_forever = relogin_forever
self._subscription_enabled = subscription_enabled
self._proxies = proxies
if subscription_enabled:
Expand Down Expand Up @@ -629,6 +637,9 @@ def _send_login(self, timeout=None):
'pwd': self.pwd}}}
ret = self.push_to_apic(login_url, data=data, timeout=timeout)
if not ret.ok:
if self.relogin_forever:
logging.error('Could not relogin to APIC. Relogin forever enabled...')
return ret
logging.error('Could not relogin to APIC. Aborting login thread.')
self.login_thread.exit()
self.subscription_thread.exit()
Expand Down Expand Up @@ -681,8 +692,9 @@ def refresh_login(self, timeout=None):
"""
refresh_url = '/api/aaaRefresh.json'
resp = self.get(refresh_url, timeout=timeout)
ret_data = json.loads(resp.text)['imdata'][0]
self.token = str(ret_data['aaaLogin']['attributes']['token'])
if resp.ok:
ret_data = json.loads(resp.text)['imdata'][0]
self.token = str(ret_data['aaaLogin']['attributes']['token'])
return resp

def close(self):
Expand Down
2 changes: 1 addition & 1 deletion applications/multisite/intersite.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def login(self):
url = 'https://' + url
else:
url = 'http://' + url
self.session = Session(url, self.credentials.user_name, self.credentials.password)
self.session = Session(url, self.credentials.user_name, self.credentials.password, relogin_forever=True)
resp = self.session.login()
return resp

Expand Down

0 comments on commit 6171e4b

Please sign in to comment.