Skip to content

Commit

Permalink
Merge pull request #93 from cloudblue/feature/LITE-28440-Add-filters-…
Browse files Browse the repository at this point in the history
…to-marketplaces

LITE-28440: Add filtering and ordering to marketplaces endpoints
  • Loading branch information
d3rky authored and akodelia committed Aug 30, 2023
2 parents 8c906a0 + 7f9c079 commit 97e1ad0
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 10 deletions.
30 changes: 25 additions & 5 deletions connect_ext_ppr/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from fastapi_filter import FilterDepends, with_prefix
from fastapi_filter.contrib.sqlalchemy import Filter

from connect_ext_ppr.models.deployment import Deployment, DeploymentRequest
from connect_ext_ppr.models.deployment import (
Deployment, DeploymentRequest, MarketplaceConfiguration,
)
from connect_ext_ppr.models.ppr import PPRVersion


Expand All @@ -16,17 +18,14 @@ class Constants(Filter.Constants):


class PPRVersionFilter(Filter):
id: Optional[str]
version: Optional[int]

class Constants(Filter.Constants):
model = PPRVersion


class DeploymentRequestFilter(Filter):
id: Optional[str]
deployment: Optional[DeploymentFilter] = FilterDepends(
with_prefix('deployment', DeploymentFilter),
)
status: Optional[str]
delegate_l2: Optional[bool]

Expand All @@ -36,3 +35,24 @@ class DeploymentRequestFilter(Filter):

class Constants(Filter.Constants):
model = DeploymentRequest


class MarketplaceConfigurationFilter(Filter):
marketplace: Optional[str]
ppr: Optional[PPRVersionFilter] = FilterDepends(with_prefix('ppr', PPRVersionFilter))

order_by: Optional[List[str]]

class Constants(Filter.Constants):
model = MarketplaceConfiguration


class DeploymentRequestExtendedFilter(DeploymentRequestFilter):
id: Optional[str]

deployment: Optional[DeploymentFilter] = FilterDepends(
with_prefix('deployment', DeploymentFilter),
)

class Constants(Filter.Constants):
model = DeploymentRequest
14 changes: 12 additions & 2 deletions connect_ext_ppr/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
VerboseBaseSession,
)
from connect_ext_ppr.errors import ExtensionHttpError, ExtensionValidationError
from connect_ext_ppr.filters import DeploymentFilter, DeploymentRequestFilter
from connect_ext_ppr.filters import (
DeploymentFilter, DeploymentRequestExtendedFilter, DeploymentRequestFilter,
MarketplaceConfigurationFilter,
)
from connect_ext_ppr.models.configuration import Configuration
from connect_ext_ppr.models.deployment import (
Deployment,
Expand Down Expand Up @@ -183,7 +186,7 @@ def add_dep_request(
)
def list_deployment_requests(
self,
dr_filter: DeploymentRequestFilter = FilterDepends(DeploymentRequestFilter),
dr_filter: DeploymentRequestExtendedFilter = FilterDepends(DeploymentRequestExtendedFilter),
pagination_params: PaginationParams = Depends(),
response: Response = None,
client: ConnectClient = Depends(get_installation_client),
Expand Down Expand Up @@ -278,6 +281,7 @@ def list_deployment_request_tasks(
def list_deployment_request_marketplaces(
self,
depl_req_id: str,
m_filter: MarketplaceConfigurationFilter = FilterDepends(MarketplaceConfigurationFilter),
pagination_params: PaginationParams = Depends(),
response: Response = None,
db: VerboseBaseSession = Depends(get_db),
Expand All @@ -290,6 +294,8 @@ def list_deployment_request_marketplaces(
marketplaces = db.query(MarketplaceConfiguration).options(
selectinload(MarketplaceConfiguration.ppr),
).filter_by(deployment_request=dr.id)
marketplaces = m_filter.filter(marketplaces)
marketplaces = m_filter.sort(marketplaces)
marketplaces = apply_pagination(marketplaces, db, pagination_params, response)

marketplaces_pprs = {m.marketplace: m.ppr for m in marketplaces}
Expand Down Expand Up @@ -378,6 +384,7 @@ def get_deployment(
def list_requests_for_deployment(
self,
deployment_id: str,
dr_filter: DeploymentRequestFilter = FilterDepends(DeploymentRequestFilter),
pagination_params: PaginationParams = Depends(),
response: Response = None,
client: ConnectClient = Depends(get_installation_client),
Expand Down Expand Up @@ -660,6 +667,7 @@ def add_ppr(
def get_marketplaces_by_deployment(
self,
deployment_id: str,
m_filter: MarketplaceConfigurationFilter = FilterDepends(MarketplaceConfigurationFilter),
pagination_params: PaginationParams = Depends(),
response: Response = None,
client: ConnectClient = Depends(get_installation_client),
Expand All @@ -671,6 +679,8 @@ def get_marketplaces_by_deployment(
mkplc_configs = db.query(MarketplaceConfiguration).options(
selectinload(MarketplaceConfiguration.ppr),
).filter_by(deployment_id=deployment_id, active=True)
mkplc_configs = m_filter.filter(mkplc_configs)
mkplc_configs = m_filter.sort(mkplc_configs)
mkplc_configs = apply_pagination(mkplc_configs, db, pagination_params, response)

mkplc_ids = [m.marketplace for m in mkplc_configs]
Expand Down
46 changes: 43 additions & 3 deletions tests/api/test_deployment_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ def test_get_deployment_request_not_found(
(('pending', None), ('error', 'An Error message!')),
)
def test_list_deployment_request_tasks(
dbsession,
mocker,
deployment_factory,
deployment_request_factory,
installation,
Expand Down Expand Up @@ -366,7 +364,6 @@ def test_list_deployment_request_tasks(


def test_list_deployment_request_tasks_not_found(
dbsession,
deployment_factory,
deployment_request_factory,
installation,
Expand Down Expand Up @@ -966,6 +963,49 @@ def test_list_deployment_request_marketplaces(
assert response.json() == expected_response


def test_list_deployment_request_marketplaces_filters(
mocker,
deployment_factory,
deployment_request_factory,
installation,
api_client,
marketplace,
marketplace_config_factory,
ppr_version_factory,
):
m1 = marketplace
marketplaces = [m1]

mocker.patch(
'connect_ext_ppr.webapp.get_marketplaces',
return_value=marketplaces,
)
dep1 = deployment_factory(account_id=installation['owner']['id'])
ppr = ppr_version_factory(deployment=dep1)

dr1 = deployment_request_factory(deployment=dep1)

marketplace_config_factory(deployment_request=dr1, marketplace_id=m1['id'])
marketplace_config_factory(deployment_request=dr1, marketplace_id='MP-12344', ppr_id=ppr.id)
marketplace_config_factory(deployment_request=dr1, marketplace_id='MP-12345')

marketplace_config_factory(deployment=dep1, marketplace_id=m1['id'])
marketplace_config_factory(deployment=dep1, marketplace_id='MP-12344')
marketplace_config_factory(deployment=dep1, marketplace_id='MP-124-114')

response = api_client.get(
f"/api/deployments/requests/{dr1.id}/marketplaces?marketplace={m1['id']}",
installation=installation,
)

assert response.status_code == 200
assert response.json() == [{
'id': m1['id'],
'name': m1['name'],
'icon': m1['icon'],
}], response.json()


def test_list_deployment_request_marketplaces_not_found(
deployment_factory,
deployment_request_factory,
Expand Down
61 changes: 61 additions & 0 deletions tests/api/test_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,67 @@ def test_list_deployments_requests_for_deployment_with_pagination(
assert response.headers['Content-Range'] == expected_header


@pytest.mark.parametrize(
('filters', 'expected_amount', 'expected_header'),
(
('', 3, 'items 0-2/3'),
('status=pending', 1, 'items 0-0/1'),
('delegate_l2=false', 2, 'items 0-1/2'),
('delegate_l2=true', 1, 'items 0-0/1'),
('ppr__version=1', 1, 'items 0-0/1'),
),
)
def test_list_deployment_requests_filters(
filters,
expected_amount,
expected_header,
mocker,
deployment_factory,
deployment_request_factory,
ppr_version_factory,
installation,
api_client,
):
hubs_data = [{
'id': 'HB-0000-000',
'name': 'Another Hub for the best',
}]

mocker.patch(
'connect_ext_ppr.webapp.get_hubs',
side_effect=[hubs_data],
)
dep1 = deployment_factory(
product_id='PRD-000-001',
account_id=installation['owner']['id'],
hub_id=hubs_data[0]['id'],
)

deployment_request_factory(
deployment=dep1,
status='error',
ppr=ppr_version_factory(deployment=dep1, version=1))

deployment_request_factory(
deployment=dep1,
status='done',
delegate_l2=True,
ppr=ppr_version_factory(deployment=dep1, version=2))

deployment_request_factory(
deployment=dep1,
status='pending',
ppr=ppr_version_factory(deployment=dep1, version=3))

response = api_client.get(
f'/api/deployments/requests?{filters}',
installation=installation,
)
assert response.status_code == 200
assert len(response.json()) == expected_amount
assert response.headers['Content-Range'] == expected_header


def test_list_deployment_request_deployment_not_found(
deployment_factory,
deployment_request_factory,
Expand Down
43 changes: 43 additions & 0 deletions tests/models/test_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,46 @@ def test_generate_all_next_verbose_id(
dbsession.refresh(task)
assert f'TSK-{suffix}-00{index}' == task.id
index += 1


def test_list_deployment_request_marketplaces_filters(
mocker,
deployment_factory,
deployment_request_factory,
installation,
api_client,
marketplace,
marketplace_config_factory,
ppr_version_factory,
):
m1 = marketplace
marketplaces = [m1]

mocker.patch(
'connect_ext_ppr.webapp.get_marketplaces',
return_value=marketplaces,
)
dep1 = deployment_factory(account_id=installation['owner']['id'])
ppr = ppr_version_factory(deployment=dep1)

dr1 = deployment_request_factory(deployment=dep1)

marketplace_config_factory(deployment_request=dr1, marketplace_id=m1['id'])
marketplace_config_factory(deployment_request=dr1, marketplace_id='MP-12344', ppr_id=ppr.id)
marketplace_config_factory(deployment_request=dr1, marketplace_id='MP-12345')

marketplace_config_factory(deployment=dep1, marketplace_id=m1['id'])
marketplace_config_factory(deployment=dep1, marketplace_id='MP-12344')
marketplace_config_factory(deployment=dep1, marketplace_id='MP-124-114')

response = api_client.get(
f"/api/deployments/requests/{dr1.id}/marketplaces?marketplace={m1['id']}",
installation=installation,
)

assert response.status_code == 200
assert response.json() == [{
'id': m1['id'],
'name': m1['name'],
'icon': m1['icon'],
}], response.json()

0 comments on commit 97e1ad0

Please sign in to comment.