Skip to content

Commit

Permalink
LITE-28565 filter pricing batches by marketplace id
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzevaka committed Sep 14, 2023
1 parent 7a5b1e8 commit ac00dad
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
6 changes: 6 additions & 0 deletions connect_ext_ppr/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List, Optional


from pydantic import BaseModel
from fastapi_filter import FilterDepends, with_prefix
from fastapi_filter.contrib.sqlalchemy import Filter

Expand Down Expand Up @@ -60,3 +62,7 @@ class DeploymentRequestExtendedFilter(DeploymentRequestFilter):

class Constants(Filter.Constants):
model = DeploymentRequest


class PricingBatchFilter(BaseModel):
marketplace_id: Optional[str]
33 changes: 17 additions & 16 deletions connect_ext_ppr/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class PrimaryKeyReference(BaseModel):
id: str


class ReferenceSchema(NonNullSchema):
id: str
name: Optional[str]
icon: Optional[str]


class ChoicesSchema(BaseModel):
choices: Optional[List[PrimaryKeyReference]] = []
all: bool
Expand All @@ -74,23 +80,11 @@ def check_choices_exists_if_all_is_false(cls, values):
return values


class VendorSchema(NonNullSchema):
id: str
name: str
icon: Optional[str]


class ProductReferenceSchema(NonNullSchema):
id: str
name: str
icon: Optional[str]


class ProductSchema(NonNullSchema):
id: str
name: str
icon: Optional[str]
owner: VendorSchema
owner: ReferenceSchema


class HubReferenceSchema(NonNullSchema):
Expand All @@ -106,10 +100,10 @@ class HubSchema(NonNullSchema):

class DeploymentSchema(NonNullSchema):
id: str
product: ProductReferenceSchema
product: ReferenceSchema
hub: HubReferenceSchema
account_id: str
owner: VendorSchema
owner: ReferenceSchema
last_sync_at: datetime
status: DeploymentStatusChoices
events: Events
Expand Down Expand Up @@ -167,7 +161,7 @@ class PPRVersionReferenceSchema(NonNullSchema):

class DeploymentReferenceSchema(NonNullSchema):
id: str
product: ProductReferenceSchema
product: ReferenceSchema
hub: HubReferenceSchema


Expand All @@ -184,10 +178,17 @@ class Config:
orm_mode = True


class StreamContextSchema(NonNullSchema):
account: ReferenceSchema
product: ReferenceSchema
marketplace: ReferenceSchema


class StreamSchema(NonNullSchema):
id: str
name: str
status: str
context: StreamContextSchema


class BatchSchema(NonNullSchema):
Expand Down
7 changes: 3 additions & 4 deletions connect_ext_ppr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
PPRVersionReferenceSchema,
PPRVersionSchema,
PrimaryKeyReference,
ProductReferenceSchema,
ProductSchema,
ReferenceSchema,
TaskSchema,
VendorSchema,
)


Expand Down Expand Up @@ -224,7 +223,7 @@ def get_deployment_schema(deployment, product, vendor, hub):

def get_deployment_reference_schema(deployment, hub):
product = deployment.product
product_schema = ProductReferenceSchema(id=product.id, name=product.name, icon=product.logo)
product_schema = ReferenceSchema(id=product.id, name=product.name, icon=product.logo)
hub_id = deployment.hub_id
hub_schema = HubReferenceSchema(id=hub_id, name=hub['name'])
return DeploymentReferenceSchema(id=deployment.id, product=product_schema, hub=hub_schema)
Expand Down Expand Up @@ -392,7 +391,7 @@ def get_product_schema(product):
id=product.id,
name=product.name,
icon=product.logo,
owner=VendorSchema(
owner=ReferenceSchema(
id=vendor.id, name=vendor.name, icon=vendor.logo,
),
)
Expand Down
13 changes: 11 additions & 2 deletions connect_ext_ppr/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from connect_ext_ppr.errors import ExtensionHttpError, ExtensionValidationError
from connect_ext_ppr.filters import (
DeploymentFilter, DeploymentRequestExtendedFilter, DeploymentRequestFilter,
MarketplaceConfigurationFilter, PPRVersionFilter,
MarketplaceConfigurationFilter, PPRVersionFilter, PricingBatchFilter,
)
from connect_ext_ppr.models.configuration import Configuration
from connect_ext_ppr.models.deployment import (
Expand Down Expand Up @@ -766,6 +766,7 @@ def list_hubs_by_product(
def get_deployment_batches(
self,
deployment_id: str,
b_filter: PricingBatchFilter = FilterDepends(PricingBatchFilter),
db: VerboseBaseSession = Depends(get_db),
client: ConnectClient = Depends(get_installation_client),
installation: dict = Depends(get_installation),
Expand All @@ -776,13 +777,21 @@ def get_deployment_batches(
deployment.hub_id,
)

if b_filter.marketplace_id:
marketplace_ids = [i for i in marketplace_ids if i == b_filter.marketplace_id]

if not marketplace_ids:
return []

batches = list(client('pricing').batches.filter(
R().stream.owner.id.eq(deployment.account_id),
R().stream.context.product.id.eq(deployment.product_id),
R().stream.context.marketplace.id.in_(marketplace_ids),
R().test.ne(True),
R().status.eq('published'),
))
).select('+stream.context'))

print(batches)

return [BatchSchema(**b) for b in batches]

Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
from contextlib import contextmanager
import json
import random
from secrets import randbelow

import pandas as pd
import pytest
Expand Down Expand Up @@ -111,7 +111,7 @@ def _build_product(
version=3,
):
if not id:
id = 'PR-{0}'.format(random.randint(10000, 99999))
id = f'PR-{randbelow(99999):05d}'
product = dbsession.query(Product).filter_by(id=id).first()

q = dbsession.query(Account).filter_by(id=owner_id)
Expand Down Expand Up @@ -183,7 +183,7 @@ def _build_deployment_request(
deployment = deployment_factory(id='DPLR-123-123-123')

if not ppr:
ppr = PPRVersion(id=f'PPR-{random.randint(1000, 9999)}', product_version=1)
ppr = PPRVersion(id=f'PPR-{randbelow(9999):04d}', product_version=1)

dep_req = DeploymentRequest(
deployment_id=deployment.id,
Expand Down Expand Up @@ -248,7 +248,7 @@ def _build_ppr(
status='pending',
):
ppr = PPRVersion(
file=file or file_factory(id='MFL-{0}'.format(random.randint(10000, 99999))).id,
file=file or file_factory(id=f'MFL-{randbelow(99999):05d}').id,
deployment=deployment.id,
configuration=configuration,
summary=summary or {},
Expand Down
7 changes: 3 additions & 4 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
HubReferenceSchema,
NonNullSchema,
PPRVersionSchema,
ProductReferenceSchema,
VendorSchema,
ReferenceSchema,
)


Expand Down Expand Up @@ -70,7 +69,7 @@ def test_deployment_schema(status):
now = datetime.utcnow()
serializer = DeploymentSchema(
id='DPL-000-000-000',
product=ProductReferenceSchema(
product=ReferenceSchema(
id='PRD-000-000-000',
name='Some',
icon='/media/VA-000-000/PRD-000-000-000/media/PRD-000-000-000-logo_cLqk6Vm.png',
Expand All @@ -80,7 +79,7 @@ def test_deployment_schema(status):
name='Hub Hub',
),
account_id='PA-000-000',
owner=VendorSchema(
owner=ReferenceSchema(
id='VA-000-000',
name='Vendor',
icon='/media/VA-000-000/media/icon.png',
Expand Down

0 comments on commit ac00dad

Please sign in to comment.