in src/lambdas/r8s_api_handler/processors/shape_price_processor.py [0:0]
def patch(self, event):
_LOG.debug(f'Update shape price event: {event}')
validate_params(event, (NAME_ATTR, REGION_ATTR, OS_ATTR,
ON_DEMAND_ATTR))
customer = event.get(PARAM_USER_CUSTOMER)
if customer == 'admin':
customer = event.get(CUSTOMER_ATTR)
if not customer:
_LOG.error(f'\'{CUSTOMER_ATTR}\' must be specified for '
f'admin users.')
return build_response(
code=RESPONSE_BAD_REQUEST_CODE,
content=f'\'{CUSTOMER_ATTR}\' must be specified for '
f'admin users.'
)
elif event.get(CUSTOMER_ATTR) and event.get(CUSTOMER_ATTR) != customer:
_LOG.error(f'You\'re not allowed to update prices for '
f'customer \'{event.get(CUSTOMER_ATTR)}\'')
return build_response(
code=RESPONSE_FORBIDDEN_CODE,
content=f'You\'re not allowed to update prices for '
f'customer \'{event.get(CUSTOMER_ATTR)}\''
)
shape_price = self.shape_price_service.get(
customer=customer,
region=event.get(REGION_ATTR),
os=event.get(OS_ATTR),
name=event.get(NAME_ATTR),
use_default_if_missing=False
)
if not shape_price:
_LOG.error(f'No shape price found matching given query.')
return build_response(
code=RESPONSE_RESOURCE_NOT_FOUND_CODE,
content=f'No shape price found matching given query.'
)
on_demand = event.get(ON_DEMAND_ATTR)
if not isinstance(on_demand, (int, float)) or on_demand <= 0:
return build_response(
code=RESPONSE_BAD_REQUEST_CODE,
content=f'\'{ON_DEMAND_ATTR}\' attribute must be a valid '
f'non-negative number.'
)
_LOG.debug(f'Setting shape price to \'{on_demand}\'')
shape_price.on_demand = on_demand
_LOG.debug(f'Saving updated shape price item')
self.shape_price_service.save(shape_price=shape_price)
_LOG.debug(f'Describing shape price dto')
response = self.shape_price_service.get_dto(shape_price)
_LOG.debug(f'Response: {response}')
return build_response(
code=RESPONSE_OK_CODE,
content=response
)