Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LITE-28177: Listing products that an account have at least one deployment #44

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions connect_ext_ppr/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
from connect_ext_ppr.models.deployment import Deployment, DeploymentRequest
from connect_ext_ppr.models.enums import ConfigurationStateChoices, DeploymentStatusChoices
from connect_ext_ppr.models.file import File
from connect_ext_ppr.models.replicas import Product
from connect_ext_ppr.service import add_deployments
from connect_ext_ppr.schemas import (
ConfigurationCreateSchema,
ConfigurationSchema,
DeploymentRequestSchema,
DeploymentSchema,
ProductSchema,
)
from connect_ext_ppr.utils import (
_get_extension_client,
Expand Down Expand Up @@ -309,6 +311,25 @@ def remove_configuration(

return Response(status_code=204)

@router.get(
'/products',
summary='List all products availables for account',
response_model=List[ProductSchema],
)
def list_products(
self,
db: VerboseBaseSession = Depends(get_db),
installation: dict = Depends(get_installation),
):
products_ids = db.query(Deployment.product_id).filter_by(
account_id=installation['owner']['id'],
).distinct()

response_list = []
for product in db.query(Product).filter(Product.id.in_(products_ids)):
response_list.append(ProductSchema(id=product.id, name=product.name, icon=product.logo))
return response_list

@classmethod
def on_startup(cls, logger, config):
# When database schema is completely defined
Expand Down
44 changes: 44 additions & 0 deletions tests/api/test_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
def test_products(
deployment_factory,
installation,
api_client,
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,
)
deployment_factory(
account_id=installation['owner']['id'],
product_id=product2.id,
)
deployment_factory(
account_id=installation['owner']['id'],
product_id=product2.id,
hub_id='HB-123-12',
)

response = api_client.get(
'/api/products',
installation=installation,
)

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

assert response.json() == [
{
'id': product1.id,
'name': product1.name,
'icon': product1.logo,
},
{
'id': product2.id,
'name': product2.name,
'icon': product2.logo,
},
]
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ def _build_product(
):
if not id:
id = 'PR-{0}'.format(random.randint(10000, 99999))
product = Product(id=id, name=name, logo=logo, owner_id=owner_id, version=version)
dbsession.add(product)
dbsession.commit()
product = dbsession.query(Product).filter_by(id=id).first()
if not product:
product = Product(id=id, name=name, logo=logo, owner_id=owner_id, version=version)
dbsession.add(product)
dbsession.commit()
return product
return _build_product

Expand Down Expand Up @@ -125,7 +127,7 @@ def _build_deployment(
vendor_id='VA-000-000',
hub_id='HB-0000-0000',
):
product = product_factory(product_id)
product = product_factory(id=product_id)
product_id = product.id

dep = Deployment(
Expand Down
8 changes: 0 additions & 8 deletions tests/test_webapp.py

This file was deleted.