Skip to content

Commit

Permalink
Merge pull request #114 from mganisin/tenant-readiness
Browse files Browse the repository at this point in the history
Refactor tenant readiness check
  • Loading branch information
mganisin authored Dec 2, 2021
2 parents 0772d01 + b8429f0 commit fd9af67
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
32 changes: 31 additions & 1 deletion threescale_api/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import time
from urllib.parse import urljoin

import backoff
import requests

from threescale_api import errors, resources
Expand All @@ -9,13 +11,15 @@


class ThreeScaleClient:
def __init__(self, url: str, token: str, throws: bool = True, ssl_verify: bool = True):
def __init__(self, url: str, token: str,
throws: bool = True, ssl_verify: bool = True, wait: bool = False):
"""Creates instance of the 3scale client
Args:
url: 3scale instance url
token: Access token
throws: Whether it should throw an error
ssl_verify: Whether to verify ssl
wait: Whether to do extra checks of 3scale availability
"""
self._rest = RestApiClient(url=url, token=token, throws=throws, ssl_verify=ssl_verify)
self._services = resources.Services(self, instance_klass=resources.Service)
Expand Down Expand Up @@ -46,6 +50,32 @@ def __init__(self, url: str, token: str, throws: bool = True, ssl_verify: bool =
self._fields_definitions =\
resources.FieldsDefinitions(self, instance_klass=resources.FieldsDefinition)

if wait:
self.wait_for_tenant()
# TODO: all the implemented checks aren't enough yet
# 3scale can still return 404/409 error, therefore slight artificial sleep
# here to mitigate the problem. This requires proper fix in checks
time.sleep(16)

@backoff.on_predicate(backoff.fibo, lambda ready: not ready, max_tries=8, jitter=None)
def wait_for_tenant(self) -> bool:
"""
When True is returned, there is some chance the tenant is actually ready.
"""
# TODO: checks below were collected from various sources to craft
# ultimate readiness check. There might be duplicates though, so
# worth to review it one day
try:
return self.account_plans.exists() \
and len(self.account_plans.fetch()["plans"]) >= 1 \
and len(self.account_plans.list()) >= 1 \
and self.accounts.exists() \
and len(self.accounts.list()) >= 1 \
and self.services.exists() \
and len(self.services.list()) >= 1
except Exception:
return False

@property
def rest(self) -> 'RestApiClient':
"""Get REST api client instance
Expand Down
14 changes: 4 additions & 10 deletions threescale_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from threescale_api.defaults import DefaultClient, DefaultPlanClient, DefaultPlanResource, \
DefaultResource, DefaultStateClient, DefaultUserResource, DefaultStateResource
from threescale_api import client
import backoff

log = logging.getLogger(__name__)

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

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

def admin_api(self, wait=False) -> 'client.ThreeScaleClient':
def admin_api(self, ssl_verify=True, wait=False) -> 'client.ThreeScaleClient':
"""
Returns admin api client for tenant.
Its strongly recommended to call this with wait=True
"""
if wait:
self.wait_tenant_ready()
ssl_verify = self.threescale_client.rest._ssl_verify
return client.ThreeScaleClient(self.admin_base_url, self.admin_token, ssl_verify=ssl_verify)
return client.ThreeScaleClient(
self.admin_base_url, self.admin_token, ssl_verify=ssl_verify, wait=wait)

def trigger_billing(self, date: str):
"""Trigger billing for whole tenant
Expand Down

0 comments on commit fd9af67

Please sign in to comment.