Skip to content

Commit b8429f0

Browse files
author
Marian Ganisin
committed
Refactor tenant readiness check
Move the check to ThreeScaleClient as there it is available for all cases (e.g. when client is used without dealing with tenants). Original wait_tenant_ready kept available. Collected all the used checks of availability and added short sleep as the checks are still insufficient
1 parent 0772d01 commit b8429f0

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

threescale_api/client.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
2+
import time
23
from urllib.parse import urljoin
34

5+
import backoff
46
import requests
57

68
from threescale_api import errors, resources
@@ -9,13 +11,15 @@
911

1012

1113
class ThreeScaleClient:
12-
def __init__(self, url: str, token: str, throws: bool = True, ssl_verify: bool = True):
14+
def __init__(self, url: str, token: str,
15+
throws: bool = True, ssl_verify: bool = True, wait: bool = False):
1316
"""Creates instance of the 3scale client
1417
Args:
1518
url: 3scale instance url
1619
token: Access token
1720
throws: Whether it should throw an error
1821
ssl_verify: Whether to verify ssl
22+
wait: Whether to do extra checks of 3scale availability
1923
"""
2024
self._rest = RestApiClient(url=url, token=token, throws=throws, ssl_verify=ssl_verify)
2125
self._services = resources.Services(self, instance_klass=resources.Service)
@@ -46,6 +50,32 @@ def __init__(self, url: str, token: str, throws: bool = True, ssl_verify: bool =
4650
self._fields_definitions =\
4751
resources.FieldsDefinitions(self, instance_klass=resources.FieldsDefinition)
4852

53+
if wait:
54+
self.wait_for_tenant()
55+
# TODO: all the implemented checks aren't enough yet
56+
# 3scale can still return 404/409 error, therefore slight artificial sleep
57+
# here to mitigate the problem. This requires proper fix in checks
58+
time.sleep(16)
59+
60+
@backoff.on_predicate(backoff.fibo, lambda ready: not ready, max_tries=8, jitter=None)
61+
def wait_for_tenant(self) -> bool:
62+
"""
63+
When True is returned, there is some chance the tenant is actually ready.
64+
"""
65+
# TODO: checks below were collected from various sources to craft
66+
# ultimate readiness check. There might be duplicates though, so
67+
# worth to review it one day
68+
try:
69+
return self.account_plans.exists() \
70+
and len(self.account_plans.fetch()["plans"]) >= 1 \
71+
and len(self.account_plans.list()) >= 1 \
72+
and self.accounts.exists() \
73+
and len(self.accounts.list()) >= 1 \
74+
and self.services.exists() \
75+
and len(self.services.list()) >= 1
76+
except Exception:
77+
return False
78+
4979
@property
5080
def rest(self) -> 'RestApiClient':
5181
"""Get REST api client instance

threescale_api/resources.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from threescale_api.defaults import DefaultClient, DefaultPlanClient, DefaultPlanResource, \
99
DefaultResource, DefaultStateClient, DefaultUserResource, DefaultStateResource
1010
from threescale_api import client
11-
import backoff
1211

1312
log = logging.getLogger(__name__)
1413

@@ -1114,24 +1113,19 @@ def __init__(self, entity_name='system_name', **kwargs):
11141113
def entity_id(self) -> int:
11151114
return self.entity["signup"]["account"]["id"]
11161115

1117-
@backoff.on_predicate(backoff.fibo, lambda ready: not ready, max_tries=8, jitter=None)
11181116
def wait_tenant_ready(self) -> bool:
11191117
"""
11201118
When True is returned, there is some chance the tenant is actually ready.
11211119
"""
1122-
api = self.admin_api()
1123-
return api.account_plans.exists() and len(api.account_plans.list()) >= 1 and\
1124-
api.accounts.exists() and len(api.accounts.list()) >= 1
1120+
return self.admin_api().wait_for_tenant()
11251121

1126-
def admin_api(self, wait=False) -> 'client.ThreeScaleClient':
1122+
def admin_api(self, ssl_verify=True, wait=False) -> 'client.ThreeScaleClient':
11271123
"""
11281124
Returns admin api client for tenant.
11291125
Its strongly recommended to call this with wait=True
11301126
"""
1131-
if wait:
1132-
self.wait_tenant_ready()
1133-
ssl_verify = self.threescale_client.rest._ssl_verify
1134-
return client.ThreeScaleClient(self.admin_base_url, self.admin_token, ssl_verify=ssl_verify)
1127+
return client.ThreeScaleClient(
1128+
self.admin_base_url, self.admin_token, ssl_verify=ssl_verify, wait=wait)
11351129

11361130
def trigger_billing(self, date: str):
11371131
"""Trigger billing for whole tenant

0 commit comments

Comments
 (0)