in src/lambdas/r8s_api_handler/processors/algorithm_processor.py [0:0]
def post(self, event):
_LOG.debug(f'Create algorithm event: {event}')
validate_params(event,
(NAME_ATTR, CUSTOMER_ATTR, CLOUD_ATTR,
REQUIRED_DATA_ATTRS_ATTR, METRIC_ATTRS_ATTR,
TIMESTAMP_ATTR))
name = event.get(NAME_ATTR)
if self.algorithm_service.get_by_name(name=name):
_LOG.debug(f'Algorithm with name \'{name}\' already exists')
return build_response(
code=RESPONSE_BAD_REQUEST_CODE,
content=f'Algorithm with name \'{name}\' already exists'
)
customer = event.get(CUSTOMER_ATTR)
user_customer = event.get(PARAM_USER_CUSTOMER)
user_id = event.get(USER_ID_ATTR)
if user_customer != 'admin' and customer != user_customer:
_LOG.warning(f'User \'{user_id}\' is not authorize to affect '
f'customer \'{customer}\' entities.')
return build_response(
code=RESPONSE_FORBIDDEN_CODE,
content=f'User \'{user_id}\' is not authorize to affect '
f'customer \'{customer}\' entities.'
)
customer_obj = self.customer_service.get(name=customer)
if not customer_obj:
_LOG.error(f'Customer with name \'{customer}\' does not exist')
return build_response(
code=RESPONSE_RESOURCE_NOT_FOUND_CODE,
content=f'Customer with name \'{customer}\' does not exist'
)
cloud = event.get(CLOUD_ATTR)
self._validate_cloud(cloud=cloud)
cloud = cloud.upper()
required_data_attrs = event.get(REQUIRED_DATA_ATTRS_ATTR)
self._validate_list_of_str(attr_name=REQUIRED_DATA_ATTRS_ATTR,
value=required_data_attrs)
metric_attrs = event.get(METRIC_ATTRS_ATTR)
self._validate_list_of_str(attr_name=METRIC_ATTRS_ATTR,
value=metric_attrs)
timestamp_attr = event.get(TIMESTAMP_ATTR)
self._validate_timestamp_attr(required_data_attrs=required_data_attrs,
timestamp_attr=timestamp_attr)
algorithm_data = {
NAME_ATTR: name,
CUSTOMER_ATTR: customer,
CLOUD_ATTR: cloud,
REQUIRED_DATA_ATTRS_ATTR: required_data_attrs,
METRIC_ATTRS_ATTR: metric_attrs,
TIMESTAMP_ATTR: timestamp_attr,
LICENSED_ATTR: False
}
_LOG.debug(f'Algorithm data: {algorithm_data}.')
algorithm = self.algorithm_service.create(algorithm_data)
try:
_LOG.debug(f'Saving algorithm')
self.algorithm_service.save(algorithm=algorithm)
except ValidationError as e:
_LOG.error(f'Error occurred while saving algorithm: '
f'{str(e)}')
return build_response(
code=RESPONSE_BAD_REQUEST_CODE,
content=e.message
)
_LOG.debug(f'Getting algorithm dto')
algorithm_dto = algorithm.get_dto()
_LOG.debug(f'Response: {algorithm_dto}')
return build_response(
code=RESPONSE_OK_CODE,
content=algorithm_dto
)