Add retries to barbicanv1 client
Allow client to retry in retriable responce codes.
Change-Id: I8454f2f87213f6fe82aa8ad77dc616e2a0a717c1
Related-Prod: PROD-29798
diff --git a/_modules/barbicanv1/common.py b/_modules/barbicanv1/common.py
index df18df9..80d927f 100644
--- a/_modules/barbicanv1/common.py
+++ b/_modules/barbicanv1/common.py
@@ -1,4 +1,6 @@
+import functools
import logging
+import time
from uuid import UUID
@@ -82,8 +84,11 @@
def send(method):
def wrap(func):
+ @functools.wraps(func)
def wrapped_f(*args, **kwargs):
cloud_name = kwargs.pop('cloud_name')
+ connect_retries = 30
+ connect_retry_delay = 1
if not cloud_name:
e = NoCredentials()
log.error('%s' % e)
@@ -95,12 +100,27 @@
if k.startswith('__'):
kwargs.pop(k)
url, request_kwargs = func(*args, **kwargs)
- response = getattr(adapter, method)(url, **request_kwargs)
- if not response.content:
+ response = None
+ for i in range(connect_retries):
+ try:
+ response = getattr(adapter, method)(
+ url, connect_retries=connect_retries,
+ **request_kwargs)
+ except Exception as e:
+ if not hasattr(e, 'http_status') or (e.http_status >= 500
+ or e.http_status == 0):
+ msg = ("Got retriable exception when contacting "
+ "Barbican API. Sleeping for %ss. Attepmpts "
+ "%s of %s")
+ log.error(msg % (connect_retry_delay, i, connect_retries))
+ time.sleep(connect_retry_delay)
+ continue
+ break
+ if not response or not response.content:
return {}
try:
resp = response.json()
- except:
+ except ValueError:
resp = response.content
return resp
return wrapped_f
@@ -131,9 +151,15 @@
if _check_uuid(ref):
uuid = ref
else:
+ retries = 30
+ while retries:
# Then we have name not uuid
- resp = resource_list(
- name=ref, cloud_name=cloud_name)[resp_key]
+ resp = resource_list(
+ name=ref, cloud_name=cloud_name).get(resp_key)
+ if resp is not None:
+ break
+ retries -= 1
+ time.sleep(1)
if len(resp) == 0:
raise ResourceNotFound(resp_key, ref)
elif len(resp) > 1: