File tree Expand file tree Collapse file tree 6 files changed +48
-4
lines changed
models-library/src/models_library/api_schemas_resource_usage_tracker
service-library/src/servicelib/rabbitmq/rpc_interfaces/resource_usage_tracker
src/simcore_service_resource_usage_tracker
web/server/src/simcore_service_webserver/resource_usage Expand file tree Collapse file tree 6 files changed +48
-4
lines changed Original file line number Diff line number Diff 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" ,
Original file line number Diff line number Diff line change @@ -34,3 +34,14 @@ class LicensedItemCheckoutNotFoundError(LicensesBaseError):
3434
3535class 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."
Original file line number Diff line number Diff line change 1616)
1717from models_library .services import ServiceKey , ServiceVersion
1818from servicelib .rabbitmq import RPCRouter
19+ from servicelib .rabbitmq .rpc_interfaces .resource_usage_tracker .errors import (
20+ PricingUnitDuplicationError ,
21+ )
1922
2023from ...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 , ))
107110async def create_pricing_unit (
108111 app : FastAPI ,
109112 * ,
Original file line number Diff line number Diff line change 1313 PricingUnitWithCostUpdate ,
1414)
1515from models_library .services import ServiceKey , ServiceVersion
16+ from servicelib .rabbitmq .rpc_interfaces .resource_usage_tracker .errors import (
17+ PricingUnitDuplicationError ,
18+ )
1619from simcore_postgres_database .models .resource_tracker_pricing_plan_to_service import (
1720 resource_tracker_pricing_plan_to_service ,
1821)
2730)
2831from simcore_postgres_database .utils_repos import transaction_context
2932from sqlalchemy .dialects .postgresql import ARRAY , INTEGER
33+ from sqlalchemy .exc import IntegrityError as SqlAlchemyIntegrityError
3034from sqlalchemy .ext .asyncio import AsyncConnection , AsyncEngine
3135
3236from ....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 )
Original file line number Diff line number Diff line change 2727 pricing_plans ,
2828 pricing_units ,
2929)
30+ from servicelib .rabbitmq .rpc_interfaces .resource_usage_tracker .errors import (
31+ PricingUnitDuplicationError ,
32+ )
3033from 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 ,
Original file line number Diff line number Diff line change 3333from servicelib .aiohttp .typing_extension import Handler
3434from servicelib .mimetype_constants import MIMETYPE_APPLICATION_JSON
3535from servicelib .rabbitmq ._errors import RPCServerError
36+ from servicelib .rabbitmq .rpc_interfaces .resource_usage_tracker .errors import (
37+ PricingUnitDuplicationError ,
38+ )
3639from servicelib .rest_constants import RESPONSE_MODEL_POLICY
3740
3841from .._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 :
You can’t perform that action at this time.
0 commit comments