Skip to content

Commit

Permalink
LITE-28561 Implement sync PPR to marketplaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Hairash committed Sep 27, 2023
1 parent 36f9c58 commit f7f614f
Show file tree
Hide file tree
Showing 6 changed files with 668 additions and 19 deletions.
1 change: 1 addition & 0 deletions connect_ext_ppr/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@
}
PPR_FILE_NAME = "PPR_{product_id}_v{version}_{timestamp}.xlsx"
PPR_FILE_NAME_DELEGATION_L2 = "{dr_id}-L2Delegation-{ppr_id}-{timestamp}.xlsx"
PPR_FILE_NAME_UPDATE_MARKETPLACES = "{dr_id}-MkplUpdate-{ppr_id}-{timestamp}.xlsx"
DESCRIPTION_TEMPLATE = """
**Description**
{description}
Expand Down
99 changes: 89 additions & 10 deletions connect_ext_ppr/tasks_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
from sqlalchemy.orm import joinedload, selectinload

from connect_ext_ppr.client.exception import ClientError
from connect_ext_ppr.constants import PPR_FILE_NAME_DELEGATION_L2
from connect_ext_ppr.constants import PPR_FILE_NAME_DELEGATION_L2, PPR_FILE_NAME_UPDATE_MARKETPLACES
from connect_ext_ppr.db import get_cbc_extension_db, get_db_ctx_manager
from connect_ext_ppr.models.configuration import Configuration
from connect_ext_ppr.models.enums import CBCTaskLogStatus
from connect_ext_ppr.models.enums import (
DeploymentRequestStatusChoices,
DeploymentStatusChoices,
TasksStatusChoices,
TaskTypesChoices,
)
from connect_ext_ppr.models.deployment import DeploymentRequest
from connect_ext_ppr.models.deployment import DeploymentRequest, MarketplaceConfiguration
from connect_ext_ppr.models.ppr import PPRVersion
from connect_ext_ppr.models.task import Task
from connect_ext_ppr.services.cbc_extension import get_hub_credentials
from connect_ext_ppr.services.cbc_hub import CBCService
from connect_ext_ppr.utils import (
create_dr_file_to_media,
get_base_workbook,
get_configuration_from_media,
get_file_size,
get_mps_to_update_for_apply_ppr_and_delegate_to_marketplaces,
get_ppr_from_media,
process_ppr_file_for_apply_ppr_and_delegate_to_marketplaces,
process_ppr_file_for_delelegate_l2,
)

Expand Down Expand Up @@ -97,14 +101,15 @@ def prepare_ppr_file_for_task(
deployment_request,
deployment,
process_func,
**process_args,
):
file, writer, wb = get_base_workbook(file_data)

try:
ws_list = []
for sheet_name in wb.sheet_names:
ws = wb.parse(sheet_name)
process_func(sheet_name, ws)
process_func(sheet_name, ws, **process_args)
ws.name = sheet_name
ws_list.append(ws)

Expand Down Expand Up @@ -133,6 +138,8 @@ def prepare_ppr_file_for_task(

except (FileNotFoundError, ValueError, KeyError) as e:
raise TaskException(f'Error while processing PPR file: {e}')
except ClientError as e:
raise TaskException(f'Error while connecting to Connect: {e.message}')


def check_and_update_product(deployment_request, cbc_service, **kwargs):
Expand All @@ -158,7 +165,75 @@ def check_and_update_product(deployment_request, cbc_service, **kwargs):
return True


def apply_ppr_and_delegate_to_marketplaces(deployment_request, **kwargs):
def apply_ppr_and_delegate_to_marketplaces(
deployment_request,
cbc_service,
connect_client,
db,
**kwargs,
):
dr_marketplaces = db.query(MarketplaceConfiguration).filter_by(
active=True,
deployment_request_id=deployment_request.id,
).all()
dep_marketplaces = db.query(MarketplaceConfiguration).filter_by(
active=True,
deployment_id=deployment_request.deployment_id,
).filter(
MarketplaceConfiguration.marketplace.in_([m.marketplace for m in dr_marketplaces]),
).all()
marketplaces_to_update_dict = {m.marketplace: None for m in dr_marketplaces}

if not deployment_request.manually:
ppr_file_id = deployment_request.ppr.file
deployment = deployment_request.deployment
configuration = (
db.query(Configuration)
.filter_by(
deployment=deployment.id,
state=Configuration.STATE.active,
)
.one_or_none()
)
try:
file_data = get_ppr_from_media(
connect_client,
deployment.account_id,
deployment.id,
ppr_file_id,
)
config_json = get_configuration_from_media(
connect_client, deployment.account_id, deployment.id, configuration.file,
)
except ClientError as e:
raise TaskException(f'Error while connecting to Connect: {e.message}')

marketplaces_to_update_dict = get_mps_to_update_for_apply_ppr_and_delegate_to_marketplaces(
ppr_file_data=file_data,
config_json=config_json,
dr_marketplaces=dr_marketplaces,
)

file = prepare_ppr_file_for_task(
connect_client=connect_client,
file_data=file_data,
file_name_template=PPR_FILE_NAME_UPDATE_MARKETPLACES,
deployment_request=deployment_request,
deployment=deployment,
process_func=process_ppr_file_for_apply_ppr_and_delegate_to_marketplaces,
columns_to_set_false=marketplaces_to_update_dict.values(),
)

tracking_id = _send_ppr(cbc_service, file)
file.close()
_check_cbc_task_status(cbc_service, tracking_id)

for marketplace in dr_marketplaces + dep_marketplaces:
if marketplace.marketplace in marketplaces_to_update_dict:
marketplace.ppr_id = deployment_request.ppr_id
db.add(marketplace)

db.commit()
return True


Expand All @@ -168,12 +243,15 @@ def delegate_to_l2(deployment_request, cbc_service, connect_client, **kwargs):

ppr_file_id = deployment_request.ppr.file
deployment = deployment_request.deployment
file_data = get_ppr_from_media(
connect_client,
deployment.account_id,
deployment.id,
ppr_file_id,
)
try:
file_data = get_ppr_from_media(
connect_client,
deployment.account_id,
deployment.id,
ppr_file_id,
)
except ClientError as e:
raise TaskException(f'Error while connecting to Connect: {e.message}')

file = prepare_ppr_file_for_task(
connect_client=connect_client,
Expand Down Expand Up @@ -213,6 +291,7 @@ def execute_tasks(db, config, tasks, connect_client):
deployment_request=task.deployment_request,
cbc_service=cbc_service,
connect_client=connect_client,
db=db,
)
task.status = TasksStatusChoices.done
if not was_succesfull:
Expand Down
53 changes: 53 additions & 0 deletions connect_ext_ppr/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from collections import defaultdict
from functools import partial
from io import BytesIO
Expand Down Expand Up @@ -660,3 +661,55 @@ def process_ppr_file_for_delelegate_l2(sheet_name, ws):
ws['Published'] = 'TRUE'
elif sheet_name == 'ServicePlans':
ws['Published'] = 'FALSE'


def get_mps_to_update_for_apply_ppr_and_delegate_to_marketplaces(
ppr_file_data,
config_json,
dr_marketplaces,
):
wb = pd.ExcelFile(ppr_file_data)
ws = wb.parse('ServicePlans')

marketplace_mapping = config_json.get('marketplace_mapping', {})
cbc_marketplace_ids_dict = {}
for dr_marketplace in dr_marketplaces:
if cbc_marketplace_id := marketplace_mapping.get(dr_marketplace.marketplace):
cbc_marketplace_ids_dict[cbc_marketplace_id] = dr_marketplace.marketplace

cbc_mp_ids_found = set()
for col in ws.columns.tolist():
if re.match(r'^OpUnit_[A-Z]{2}$', col):
cbc_marketplace_id = col.split('_')[1]
cbc_mp_ids_found.add(cbc_marketplace_id)

marketplaces_to_update_dict = {
cbc_marketplace_ids_dict[cbc_id]: f'OpUnit_{cbc_id}' for cbc_id in cbc_marketplace_ids_dict
if cbc_id in cbc_mp_ids_found
}
# {'MP-06811': 'OpUnit_DE', ...}
return marketplaces_to_update_dict


def process_service_plans_for_apply_ppr_and_delegate_to_marketplaces(
ws,
columns_to_set_false,
):
for col in ws.columns.tolist():
if re.match(r'^OpUnit_[A-Z]{2}$', col) and col not in columns_to_set_false:
ws[col] = 'FALSE'


def process_ppr_file_for_apply_ppr_and_delegate_to_marketplaces(
sheet_name,
ws,
columns_to_set_false,
):
if sheet_name == 'ServicePlans':
process_service_plans_for_apply_ppr_and_delegate_to_marketplaces(
ws,
columns_to_set_false,
)

elif sheet_name == 'OpUnitServicePlans':
ws['Published'] = 'FALSE'
Binary file modified tests/fixtures/test_PPR_file_delegate_l2.xlsx
Binary file not shown.
Loading

0 comments on commit f7f614f

Please sign in to comment.