Skip to content

Commit

Permalink
small refactor in client
Browse files Browse the repository at this point in the history
  • Loading branch information
mkudlej committed Oct 3, 2023
1 parent 690d5aa commit d350ecb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ see https://github.com/3scale/3scale-operator/blob/master/doc/backend-reference.
Command to run integration unit tests: `pipenv run pytest --log-cli-level=10 -vvvv -s ./tests/integration/ |& tee x`

### TODO
- do not simulate app plan id
- create unit integration tests for:
- ProductDeploymentSpec
- AuthenticationSpec
Expand Down
21 changes: 11 additions & 10 deletions tests/integration/test_integration_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_application_list_global(api, application):
applications = api.applications.list()
assert len(applications) >= 1


def test_application_list(account, application):
applications = account.applications.list()
assert len(applications) >= 1
Expand All @@ -33,26 +34,26 @@ def test_application_can_be_read_by_name(api, application_params, application):


@pytest.fixture(scope='module')
def update_application_params(application_plan2):
suffix = secrets.token_urlsafe(8)
name = f"updated-{suffix}"
return dict(name=name, description=name, plan_id=application_plan2['id'])


@pytest.fixture(scope='module')
def application_plan_params2(service):
def application_plan_params2():
suffix = secrets.token_urlsafe(8)
return dict(name=f"test-{suffix}", setup_fee="1.00", state_event='publish', cost_per_month="3.00")


@pytest.fixture(scope='module')
def application_plan2(api, service, application_plan_params2):
def application_plan2(service, application_plan_params2):
resource = service.app_plans.create(params=application_plan_params2)
yield resource
resource.delete()


def test_application_update(application, update_application_params):
@pytest.fixture(scope='module')
def update_application_params(application_plan2):
suffix = secrets.token_urlsafe(8)
name = f"updated-{suffix}"
return dict(name=name, description=name, plan_id=application_plan2['id'])


def test_application_update(update_application_params, application):
updated_application = application.update(params=update_application_params)
asserts.assert_resource(updated_application)
asserts.assert_resource_params(updated_application, update_application_params)
Expand Down
11 changes: 6 additions & 5 deletions threescale_api_crd/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def fetch(self, entity_id: int = None, **kwargs):
Returns(dict): Resource dict from the 3scale
"""
LOG.info(self._log_message("[FETCH] CRD Fetch ", entity_id=entity_id, args=kwargs))

if self.is_crd_implemented():
list_crds = self.read_crd(self._entity_collection)
instance_list = self._create_instance(response=list_crds)
Expand Down Expand Up @@ -203,16 +202,19 @@ def _extract_resource_crd(self, response, collection, klass):
extract_params['collection'] = self._entity_collection
extracted = None
if isinstance(response, list):
if self.is_crd_implemented():
if response:
return [{'spec': obj.as_dict()['spec'], 'crd': obj} for obj in response]
return None

return extracted

def _instantiate_crd(self, extracted, klass):
if isinstance(extracted, list):
instance = [self.__make_instance_crd(item, klass) for item in extracted]
return self._create_instance_trans(instance)
return self.__make_instance_crd(extracted, klass)
if extracted:
return self.__make_instance_crd(extracted, klass)
return None

def _create_instance_trans(self, instance):
return instance
Expand Down Expand Up @@ -387,14 +389,13 @@ def update(self, entity_id=None, params: dict = None,
# Policies
# PricingRules
# Proxies

spec = self.before_update(new_params, resource)

maps = self.remove_from_list(self.get_list(), spec)

par = self.parent

self.before_update_list(maps, new_params, spec, resource)
maps = self.before_update_list(maps, new_params, spec, resource)

par = self.update_list(maps)
maps = self.get_list()
Expand Down
55 changes: 32 additions & 23 deletions threescale_api_crd/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ def before_create(self, params, spec):

def before_update(self, new_params, resource):
"""Called before update."""
new_params['id'] = resource.parent.entity_id
if resource and 'id' in new_params:
resource.entity_id = new_params['id']
if not resource:
resource = self.list()
#new_params['id'] = self.parent['id']
#if resource and 'id' in new_params:
# resource.entity_id = new_params['id']
return self.translate_to_crd(new_params)

def get_list(self, typ=None):
Expand All @@ -88,6 +90,8 @@ def before_update_list(self, maps, new_params, spec, resource):
obj = {}
iter_obj = obj
# service.proxy.oidc.update(params={"oidc_configuration": DEFAULT_FLOWS})
if not resource:
resource = self.list()

for path in resource.spec_path:
if path not in iter_obj:
Expand All @@ -111,7 +115,7 @@ def before_update_list(self, maps, new_params, spec, resource):
iter_obj[path] = {}
iter_obj = iter_obj[path]

maps = obj
return obj

def update_list(self, maps):
""" Returns updated list. """
Expand All @@ -131,6 +135,10 @@ def topmost_parent(self):
def list(self, **kwargs):
return DefaultClientCRD.list(self, **kwargs)

def update(self, *args, **kwargs):
kwargs['resource'] = self.list()
return DefaultClientNestedCRD.update(self, *args, **kwargs)

def delete(self):
"""This functions is not implemented for Proxies."""
raise threescale_api.ThreeScaleApiError("Delete not implemented for Proxies")
Expand Down Expand Up @@ -237,7 +245,7 @@ def get_list(self, typ=None):

def get_list_from_spec(self):
""" Returns list from spec """
return self.parent.crd.as_dict()['spec'].get('mapping_rules', [])
return self.parent.crd.as_dict()['spec'].get('mappingRules', [])

def before_update(self, new_params, resource):
"""Called before update."""
Expand All @@ -252,6 +260,7 @@ def before_update(self, new_params, resource):
def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
MappingRules.insert_into_position(maps, new_params, spec)
return maps

def update_list(self, maps):
""" Returns updated list. """
Expand Down Expand Up @@ -454,6 +463,7 @@ def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
name = new_params.get(Metrics.ID_NAME)
maps[name] = spec
return maps

def update_list(self, maps):
""" Returns updated list. """
Expand Down Expand Up @@ -539,14 +549,15 @@ def in_create(self, maps, params, spec):

def get_list_from_spec(self):
""" Returns list from spec """
return self.parent.crd.as_dict()['spec'].get('backend_usages', {})
return self.parent.crd.as_dict()['spec'].get('backendUsages', {})

def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
backend_id = spec.pop('backend_id')
spec.pop('service_id')
back = self.threescale_client.backends.read(int(backend_id))
maps[back[BackendUsages.ID_NAME]] = spec
return maps

def update_list(self, maps):
""" Returns updated list. """
Expand Down Expand Up @@ -617,7 +628,7 @@ def get_list_from_spec(self):

def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
maps = spec
return spec

def update_list(self, maps):
""" Returns updated list. """
Expand Down Expand Up @@ -700,11 +711,12 @@ def in_create(self, maps, params, spec):

def get_list_from_spec(self):
""" Returns list from spec """
return self.parent.crd.as_dict()['spec'].get('application_plans', {})
return self.parent.crd.as_dict()['spec'].get('applicationPlans', {})

def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
maps[new_params['id']] = spec
return maps

def update_list(self, maps):
""" Returns updated list. """
Expand Down Expand Up @@ -994,7 +1006,7 @@ def get_list_from_spec(self):
def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
spec = self.translate_to_crd(new_params)
maps = self.insert_to_list(maps, new_params, {'spec': spec})
return self.insert_to_list(maps, new_params, {'spec': spec})

def update_list(self, maps):
""" Returns updated list. """
Expand All @@ -1004,9 +1016,8 @@ def remove_from_list(self, mapsi, spec):
""" Returns list without item specified by 'spec'. """
maps = []
for mapi in mapsi:
map_ret = self.translate_to_crd(mapi.entity)
if map_ret != spec:
maps.append(map_ret)
if mapi['id'] != (spec['period'], spec['metricMethodRef']['systemName']):
maps.append(self.translate_to_crd(mapi.entity))
return maps

def topmost_parent(self):
Expand Down Expand Up @@ -1157,7 +1168,7 @@ def get_list_from_spec(self):
def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
spec = self.translate_to_crd(new_params)
maps = PricingRules.insert_to_list(maps, new_params, {'spec': spec})
return PricingRules.insert_to_list(maps, new_params, {'spec': spec})

def update_list(self, maps):
""" Returns updated list. """
Expand Down Expand Up @@ -1362,6 +1373,7 @@ def before_update_list(self, maps, new_params, spec, resource):
""" Modify some details in data before updating the list """
name = new_params.get(Methods.ID_NAME)
maps[name] = spec
return maps

def update_list(self, maps):
""" Returns updated list. """
Expand Down Expand Up @@ -1472,9 +1484,10 @@ def __init__(self, **kwargs):
if 'spec' in kwargs:
spec = kwargs.pop('spec')
crd = kwargs.pop('crd')
# client = kwargs.get('client')
self.spec_path = []
entity = {}
# there is no attribute which can simulate Proxy id, service id should be used
entity['id'] = crd.as_dict().get('status').get(Services.ID_NAME)
# apicastHosted or ApicastSelfManaged
if len(spec.values()):
apicast_key = list(spec.keys())[0]
Expand Down Expand Up @@ -1528,8 +1541,7 @@ def __init__(self, **kwargs):
if ('endpoint' not in entity) or ('sandbox_endpoint' not in entity):
self.client.disable_crd_implemented()
self.parent.client.disable_crd_implemented()
tmp_proxy = threescale_api.resources.Services.read(self.parent.client,
self.parent.entity_id).proxy.fetch()
tmp_proxy = self.parent.proxy.fetch()
for name in ['endpoint', 'sandbox_endpoint']:
self.entity[name] = tmp_proxy[name]
self.client.enable_crd_implemented()
Expand Down Expand Up @@ -1818,13 +1830,10 @@ def mapping_rules(self) -> 'BackendMappingRules':
def metrics(self) -> 'BackendMetrics':
return BackendMetrics(parent=self, instance_klass=BackendMetric)

@property
def usages(self) -> 'BackendUsages':
for service in self.client.threescale_client.services.list():
backend_usages = service.backend_usages.list()
if len(backend_usages) > 0 and int(backend_usages[0]['backend_id']) == int(self['id']):
return BackendUsages(parent=service, instance_klass=BackendUsage)
return None
def usages(self) -> list['BackendUsages']:
services = self.client.threescale_client.services.list() or []
return [usage for service in services
for usage in service.backend_usages.select_by(backend_id=self['id'])]


class BackendMappingRule(MappingRule):
Expand Down

0 comments on commit d350ecb

Please sign in to comment.