Skip to content

Commit

Permalink
LITE-28564 pricelist was added to marketplace configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzevaka committed Sep 13, 2023
1 parent c67a6e5 commit 12a593b
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 188 deletions.
3 changes: 2 additions & 1 deletion connect_ext_ppr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ class ExtensionValidationError(ExtensionErrorBase):
0: "{validation_error}", # PPR Schema validation
1: "{field}: {id} not found.",
2: "{field}: This values {values} are invalid.",
3: "At least one choice needs to be specified.",
3: "At least one {field} needs to be specified.",
4: "Cannot applied PPR to {entity} {values}.",
5: "Transition not allowed: can not set {field_name} from `{source}` to"
" '{target}', allowed {field_name} sources for '{target}' are '{allowed}'.",
6: "Pricing batches invalid: {ids}.",
}
4 changes: 3 additions & 1 deletion connect_ext_ppr/models/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class DeploymentRequest(Model):
foreign_keys="DeploymentRequest.deployment_id",
innerjoin=True,
)
marketplaces = relationship('MarketplaceConfiguration', backref='deployment_request', lazy=True)

@transition('status', target=STATUSES.aborting, sources=[STATUSES.pending, STATUSES.processing])
def aborting(self, by):
Expand Down Expand Up @@ -100,8 +101,9 @@ class MarketplaceConfiguration(Model):
id = db.Column(db.Integer(), primary_key=True, autoincrement=True)
marketplace = db.Column(db.String(16))
deployment_id = db.Column(db.ForeignKey(Deployment.id), nullable=True)
deployment_request = db.Column(db.ForeignKey(DeploymentRequest.id), nullable=True)
deployment_request_id = db.Column(db.ForeignKey(DeploymentRequest.id), nullable=True)
ppr_id = db.Column(db.String, db.ForeignKey(PPRVersion.id))
pricelist_id = db.Column(db.String(32), nullable=True)
active = db.Column(db.Boolean(), default=True)

ppr = relationship('PPRVersion', foreign_keys='MarketplaceConfiguration.ppr_id')
8 changes: 7 additions & 1 deletion connect_ext_ppr/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class MarketplaceSchema(NonNullSchema):
external_id: Optional[str]

ppr: Optional[PPRVersionReferenceSchema]
pricelist: Optional[PrimaryKeyReference]


class TaskSchema(NonNullSchema):
Expand All @@ -220,9 +221,14 @@ class TaskSchema(NonNullSchema):
error_message: Optional[str]


class MarketplaceConfigSchema(NonNullSchema):
id: str
pricelist: Optional[PrimaryKeyReference]


class DeploymentRequestCreateSchema(NonNullSchema):
deployment: PrimaryKeyReference
ppr: PrimaryKeyReference
manually: bool
delegate_l2: Optional[bool]
marketplaces: ChoicesSchema
marketplaces: Optional[List[MarketplaceConfigSchema]]
15 changes: 6 additions & 9 deletions connect_ext_ppr/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,11 @@ def add_new_deployment_request(db, dr_data, deployment, account_id, logger):
db.flush()
db.refresh(deployment_request)

marketplaces = [m.id for m in dr_data.marketplaces.choices]
if dr_data.marketplaces.all:
marketplaces = [m.marketplace for m in deployment.marketplaces]

for m_id in marketplaces:
for mp_data in dr_data.marketplaces:
mc = MarketplaceConfiguration(
deployment_request=deployment_request.id,
marketplace=m_id,
deployment_request=deployment_request,
marketplace=mp_data.id,
pricelist_id=mp_data.pricelist.id if mp_data.pricelist else None,
)
db.add(mc)

Expand Down Expand Up @@ -412,11 +409,11 @@ def deactivate_marketplaces(installation, listings, config, logger):
update(MarketplaceConfiguration)
.where(
MarketplaceConfiguration.marketplace == marketplace_id,
MarketplaceConfiguration.deployment_request.in_(
MarketplaceConfiguration.deployment_request_id.in_(
[dr.id for dr in deployments_requests],
))
.values(active=False)
.returning(MarketplaceConfiguration.deployment_request)
.returning(MarketplaceConfiguration.deployment_request_id)
)

result = db.execute(stmt)
Expand Down
5 changes: 4 additions & 1 deletion connect_ext_ppr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
MarketplaceSchema,
PPRVersionReferenceSchema,
PPRVersionSchema,
PrimaryKeyReference,
ProductReferenceSchema,
ProductSchema,
TaskSchema,
Expand Down Expand Up @@ -605,7 +606,7 @@ def process_ppr(wb, product, config_json, items):
return ws_list, summary


def get_marketplace_schema(marketplace, ppr):
def get_marketplace_schema(marketplace, ppr, pricelist_id):
mp_schema = MarketplaceSchema(
id=marketplace['id'],
name=marketplace['name'],
Expand All @@ -614,6 +615,8 @@ def get_marketplace_schema(marketplace, ppr):
)
if ppr:
mp_schema.ppr = PPRVersionReferenceSchema(id=ppr.id, version=ppr.version)
if pricelist_id:
mp_schema.pricelist = PrimaryKeyReference(id=pricelist_id)

return mp_schema

Expand Down
51 changes: 46 additions & 5 deletions connect_ext_ppr/validator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from connect.client import R
from fastapi import status

from connect_ext_ppr.errors import ExtensionValidationError
Expand Down Expand Up @@ -28,11 +29,19 @@ def validate_ppr_version_belongs_to_deployment(ppr, deployment):
)


def validate_dr_marketplaces(dr_marketplaces, dep_marketplaces):
def validate_dr_marketplaces(client, product_id, dr_marketplaces, dep_marketplaces):
"""
Validates that all the DR's marketplaces belong to the associated deployment
"""
diff = list(set(dr_marketplaces) - set(dep_marketplaces))
if not dr_marketplaces:
raise ExtensionValidationError.VAL_003(
format_kwargs={'field': 'marketplace'},
)

mp_configs = {mp.id: mp for mp in dr_marketplaces}
dep_ids = {mp.marketplace for mp in dep_marketplaces}

diff = list(mp_configs.keys() - dep_ids)
if diff:
diff.sort()
raise ExtensionValidationError.VAL_002(
Expand All @@ -43,16 +52,48 @@ def validate_dr_marketplaces(dr_marketplaces, dep_marketplaces):
status_code=status.HTTP_400_BAD_REQUEST,
)

validate_pricelist_ids(client, product_id, mp_configs)


def validate_pricelist_ids(client, product_id, mp_configs):
rql_filters = []
pricelist_ids = set()
for mp_id, config in mp_configs.items():
if config.pricelist:
pricelist_ids.add(config.pricelist.id)
rql_filters.append(
R().stream.context.marketplace.id.eq(mp_id)
& R().id.eq(config.pricelist.id),
)

if not pricelist_ids:
return

batches = client('pricing').batches.filter(
R(_op=R.OR, _children=rql_filters),
R().stream.context.product.id.eq(product_id),
R().status.eq('published'),
R().test.ne(True),
)

invalid_ids = pricelist_ids - {b['id'] for b in batches}

if invalid_ids:
raise ExtensionValidationError.VAL_006(format_kwargs={
'ids': ', '.join(sorted(invalid_ids)),
})


def validate_marketplaces_ppr(ppr, dr_marketplaces, dep_marketplaces):
"""
Validates that we can apply the ppr to all marketplaces
"""
dep_mkplc_map = {m.marketplace: m for m in dep_marketplaces}
mkplcs_w_erros = []
for marketplace in dr_marketplaces:
if dep_mkplc_map[marketplace].ppr_id and dep_mkplc_map[marketplace].ppr_id > ppr.id:
mkplcs_w_erros.append(marketplace)
for mp_data in dr_marketplaces:
mp_id = mp_data.id
if dep_mkplc_map[mp_id].ppr_id and dep_mkplc_map[mp_id].ppr_id > ppr.id:
mkplcs_w_erros.append(mp_id)

if mkplcs_w_erros:
raise ExtensionValidationError.VAL_004(
Expand Down
44 changes: 28 additions & 16 deletions connect_ext_ppr/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@ def add_dep_request(
active=True,
deployment_id=deployment.id,
)
dr_marketplaces = [m.id for m in deployment_request.marketplaces.choices]

validate_dr_marketplaces(
dr_marketplaces,
[m.marketplace for m in dep_marketplaces],
client=client,
product_id=deployment.product_id,
dr_marketplaces=deployment_request.marketplaces,
dep_marketplaces=dep_marketplaces,
)

validate_marketplaces_ppr(ppr, dr_marketplaces, dep_marketplaces)
validate_marketplaces_ppr(ppr, deployment_request.marketplaces, dep_marketplaces)

instance = add_new_deployment_request(
db, deployment_request, deployment, account_id, logger,
Expand Down Expand Up @@ -262,14 +263,16 @@ def get_deployment_request(
def list_deployment_request_tasks(
self,
depl_req_id: str,
pagination_params: PaginationParams = Depends(),
response: Response = None,
db: VerboseBaseSession = Depends(get_db),
installation: dict = Depends(get_installation),
):
dr = get_deployment_request_by_id(depl_req_id, db, installation)
if dr:
task_list = []
tasks = db.query(Task).filter_by(deployment_request=dr.id).order_by(Task.id)
for task in tasks:
qs = db.query(Task).filter_by(deployment_request=dr.id).order_by(Task.id)
for task in apply_pagination(qs, db, pagination_params, response):
task_list.append(get_task_schema(task))
return task_list

Expand All @@ -291,19 +294,24 @@ def list_deployment_request_marketplaces(
dr = get_deployment_request_by_id(depl_req_id, db, installation)

marketplaces_list = []
marketplaces = db.query(MarketplaceConfiguration).options(

mp_configs = 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)
).filter_by(deployment_request_id=dr.id)
mp_configs = m_filter.filter(mp_configs)
mp_configs = m_filter.sort(mp_configs)
mp_configs = apply_pagination(mp_configs, db, pagination_params, response)

marketplaces_pprs = {m.marketplace: m.ppr for m in marketplaces}
marketplaces_data = get_marketplaces(client, list(marketplaces_pprs.keys()))
mp_configs = {m.marketplace: m for m in mp_configs}
marketplaces_data = get_marketplaces(client, list(mp_configs.keys()))

for marketplace in marketplaces_data:
marketplaces_list.append(
get_marketplace_schema(marketplace, marketplaces_pprs.get(marketplace['id'])),
get_marketplace_schema(
marketplace,
mp_configs[marketplace['id']].ppr,
mp_configs[marketplace['id']].pricelist_id,
),
)
return marketplaces_list

Expand Down Expand Up @@ -692,7 +700,11 @@ def get_marketplaces_by_deployment(
response_list = []
for mkplc_config in mkplc_configs:
m_data = filter_object_list_by_id(marketplaces, mkplc_config.marketplace)
response_list.append(get_marketplace_schema(m_data, mkplc_config.ppr))
response_list.append(get_marketplace_schema(
m_data,
mkplc_config.ppr,
mkplc_config.pricelist_id,
))
return response_list

@router.get(
Expand Down
Loading

0 comments on commit 12a593b

Please sign in to comment.