def post()

in src/lambdas/r8s_api_handler/processors/shape_price_processor.py [0:0]


    def post(self, event):
        _LOG.debug(f'Create shape price event: {event}')
        validate_params(event, (NAME_ATTR, CLOUD_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.'
                )
            if customer != DEFAULT_CUSTOMER and not \
                    self.customer_service.get(name=customer):
                _LOG.error(f'Customer \'{customer}\' does not exist.')
                return build_response(
                    code=RESPONSE_BAD_REQUEST_CODE,
                    content=f'Customer \'{customer}\' does not exist.'
                )
        elif event.get(CUSTOMER_ATTR) and event.get(CUSTOMER_ATTR) != customer:
            _LOG.error(f'You\'re not allowed to create prices for '
                       f'customer \'{event.get(CUSTOMER_ATTR)}\'')
            return build_response(
                code=RESPONSE_FORBIDDEN_CODE,
                content=f'You\'re not allowed to create prices for '
                        f'customer \'{event.get(CUSTOMER_ATTR)}\''
            )

        cloud = event.get(CLOUD_ATTR)
        if cloud not in CloudEnum.list():
            _LOG.warning(f'Unsupported cloud specified \'{cloud}\'. '
                         f'Available clouds: {", ".join(CloudEnum.list())}')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Unsupported cloud specified \'{cloud}\'. '
                        f'Available clouds: {", ".join(CloudEnum.list())}'
            )

        os = event.get(OS_ATTR)
        if os not in OSEnum.list():
            _LOG.warning(f'Unsupported os specified \'{cloud}\'. '
                         f'Available options: {", ".join(OSEnum.list())}')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Unsupported cloud specified \'{cloud}\'. '
                        f'Available options: {", ".join(OSEnum.list())}'
            )
        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.'
            )

        shape_price_exists = self.shape_price_service.get(
            customer=customer, region=event.get(REGION_ATTR),
            os=os, name=event.get(NAME_ATTR),
            use_default_if_missing=False)
        if shape_price_exists:
            _LOG.error(f'Shape price for the given query already exists.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Shape price for the given query already exists.'
            )

        shape_price_data = {
            CUSTOMER_ATTR: customer,
            NAME_ATTR: event.get(NAME_ATTR),
            CLOUD_ATTR: cloud,
            REGION_ATTR: event.get(REGION_ATTR),
            OS_ATTR: os,
            ON_DEMAND_ATTR: on_demand
        }
        _LOG.debug(f'Creating shape price from data \'{shape_price_data}\'')

        shape_price = self.shape_price_service.create(
            shape_price_data=shape_price_data)

        _LOG.debug(f'Saving 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
        )