Skip to content

Commit d317b17

Browse files
fix
1 parent ddcc194 commit d317b17

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

packages/models-library/src/models_library/api_schemas_resource_usage_tracker/pricing_plans.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RutPricingUnitGet(BaseModel):
2727

2828
model_config = ConfigDict(
2929
json_schema_extra={
30-
"examples": [ # type: ignore [dict-item]
30+
"examples": [
3131
{
3232
"pricing_unit_id": 1,
3333
"unit_name": "SMALL",

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/resource_usage_tracker/errors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ class LicensedItemCheckoutNotFoundError(LicensesBaseError):
3434

3535
class WalletTransactionError(OsparcErrorMixin, Exception):
3636
msg_template = "{msg}"
37+
38+
39+
### Pricing Plans Error
40+
41+
42+
class PricingPlanBaseError(OsparcErrorMixin, Exception):
43+
...
44+
45+
46+
class PricingUnitDuplicationError(PricingPlanBaseError):
47+
msg_template = "Pricing unit with that name already exists in given product."

services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/api/rpc/_pricing_plans.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
)
1717
from models_library.services import ServiceKey, ServiceVersion
1818
from servicelib.rabbitmq import RPCRouter
19+
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.errors import (
20+
PricingUnitDuplicationError,
21+
)
1922

2023
from ...services import pricing_plans, pricing_units
2124

@@ -103,7 +106,7 @@ async def get_pricing_unit(
103106
)
104107

105108

106-
@router.expose(reraise_if_error_type=())
109+
@router.expose(reraise_if_error_type=(PricingUnitDuplicationError,))
107110
async def create_pricing_unit(
108111
app: FastAPI,
109112
*,

services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/services/modules/db/pricing_plans_db.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
PricingUnitWithCostUpdate,
1414
)
1515
from models_library.services import ServiceKey, ServiceVersion
16+
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.errors import (
17+
PricingUnitDuplicationError,
18+
)
1619
from simcore_postgres_database.models.resource_tracker_pricing_plan_to_service import (
1720
resource_tracker_pricing_plan_to_service,
1821
)
@@ -27,6 +30,7 @@
2730
)
2831
from simcore_postgres_database.utils_repos import transaction_context
2932
from sqlalchemy.dialects.postgresql import ARRAY, INTEGER
33+
from sqlalchemy.exc import IntegrityError as SqlAlchemyIntegrityError
3034
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
3135

3236
from ....exceptions.errors import (
@@ -559,7 +563,10 @@ async def create_pricing_unit_with_cost(
559563
)
560564
.returning(resource_tracker_pricing_units.c.pricing_unit_id)
561565
)
562-
result = await conn.execute(insert_stmt)
566+
try:
567+
result = await conn.execute(insert_stmt)
568+
except SqlAlchemyIntegrityError as exc:
569+
raise PricingUnitDuplicationError from exc
563570
row = result.first()
564571
if row is None:
565572
raise PricingUnitNotCreatedDBError(data=data)

services/resource-usage-tracker/tests/unit/with_dbs/test_api_pricing_plans_rpc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
pricing_plans,
2828
pricing_units,
2929
)
30+
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.errors import (
31+
PricingUnitDuplicationError,
32+
)
3033
from simcore_postgres_database.models.resource_tracker_pricing_plan_to_service import (
3134
resource_tracker_pricing_plan_to_service,
3235
)
@@ -212,6 +215,23 @@ async def test_rpc_pricing_plans_with_units_workflow(
212215
_first_pricing_unit_id = result.pricing_unit_id
213216
_current_cost_per_unit_id = result.current_cost_per_unit_id
214217

218+
with pytest.raises(PricingUnitDuplicationError):
219+
await pricing_units.create_pricing_unit(
220+
rpc_client,
221+
product_name="osparc",
222+
data=PricingUnitWithCostCreate(
223+
pricing_plan_id=_pricing_plan_id,
224+
unit_name="SMALL",
225+
unit_extra_info=UnitExtraInfoTier.model_config["json_schema_extra"][
226+
"examples"
227+
][0],
228+
default=True,
229+
specific_info=SpecificInfo(aws_ec2_instances=[]),
230+
cost_per_unit=Decimal(10),
231+
comment=faker.sentence(),
232+
),
233+
)
234+
215235
# Get pricing plan
216236
result = await pricing_plans.get_pricing_plan(
217237
rpc_client,

services/web/server/src/simcore_service_webserver/resource_usage/_pricing_plans_admin_rest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
from servicelib.aiohttp.typing_extension import Handler
3434
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
3535
from servicelib.rabbitmq._errors import RPCServerError
36+
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.errors import (
37+
PricingUnitDuplicationError,
38+
)
3639
from servicelib.rest_constants import RESPONSE_MODEL_POLICY
3740

3841
from .._meta import API_VTAG as VTAG
@@ -54,7 +57,7 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
5457
try:
5558
return await handler(request)
5659

57-
except ValueError as exc:
60+
except (ValueError, PricingUnitDuplicationError) as exc:
5861
raise web.HTTPBadRequest(reason=f"{exc}") from exc
5962

6063
except RPCServerError as exc:

0 commit comments

Comments
 (0)