Skip to content

Commit

Permalink
LITE-28077: Getting hubs by product
Browse files Browse the repository at this point in the history
  • Loading branch information
akodelia committed Jul 25, 2023
1 parent c5d2987 commit 7fc256d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
24 changes: 24 additions & 0 deletions connect_ext_ppr/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ConfigurationSchema,
DeploymentRequestSchema,
DeploymentSchema,
HubSchema,
ProductSchema,
)
from connect_ext_ppr.utils import (
Expand Down Expand Up @@ -330,6 +331,29 @@ def list_products(
response_list.append(ProductSchema(id=product.id, name=product.name, icon=product.logo))
return response_list

@router.get(
'/products/{product_id}/hubs',
summary="List all product's hub",
response_model=List[HubSchema],
)
def list_hubs_by_product(
self,
product_id: str,
client: ConnectClient = Depends(get_installation_client),
db: VerboseBaseSession = Depends(get_db),
installation: dict = Depends(get_installation),
):
hubs_ids = [
h[0] for h in db.query(Deployment.hub_id).filter_by(
account_id=installation['owner']['id'],
product_id=product_id,
).distinct()
]
reponse_list = []
for hub in get_hubs(client, hubs_ids):
reponse_list.append(HubSchema(id=hub['id'], name=hub['name']))
return reponse_list

@classmethod
def on_startup(cls, logger, config):
# When database schema is completely defined
Expand Down
42 changes: 42 additions & 0 deletions tests/api/test_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,45 @@ def test_products(
'icon': product2.logo,
},
]


def test_hubs_by_product(
deployment_factory,
installation,
api_client,
mocker,
product_factory,
):
product1 = product_factory(id='PRD-000-001', name='Product 1')
product2 = product_factory(id='PRD-000-002', name='Product 2')

deployment_factory(account_id='PA-159-159')
deployment_factory(
account_id=installation['owner']['id'],
product_id=product1.id,
)
dep1 = deployment_factory(
account_id=installation['owner']['id'],
product_id=product2.id,
)
dep2 = deployment_factory(
account_id=installation['owner']['id'],
product_id=product2.id,
hub_id='HB-123-12',
)

hubs = [
{'id': dep1.hub_id, 'name': 'An awesome hub'},
{'id': dep2.hub_id, 'name': 'Another awesome hub'},
]
mocker.patch('connect_ext_ppr.webapp.get_hubs', side_effect=[hubs])

response = api_client.get(
f'/api/products/{product2.id}/hubs',
installation=installation,
)

assert response.status_code == 200
assert len(response.json()) == 2

assert response.json() == hubs

0 comments on commit 7fc256d

Please sign in to comment.