def post()

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


    def post(self, event):
        _LOG.debug(f'Create application event: {secure_event(event)}')
        validate_params(event, (CUSTOMER_ATTR, DESCRIPTION_ATTR,
                                INPUT_STORAGE_ATTR, OUTPUT_STORAGE_ATTR,
                                CONNECTION_ATTR))
        description = event.get(DESCRIPTION_ATTR)
        if not description:
            _LOG.error(f'Attribute \'{DESCRIPTION_ATTR}\' can\'t be empty.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Attribute \'{DESCRIPTION_ATTR}\' can\'t be empty.'
            )

        connection = event.get(CONNECTION_ATTR)
        _LOG.debug(f'Validating connection \'{connection}\'')
        if not isinstance(connection, dict):
            _LOG.error(f'\'{CONNECTION_ATTR}\' attribute must be a valid '
                       f'dict.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'\'{CONNECTION_ATTR}\' attribute must be a valid '
                        f'dict.'
            )
        connection = {**DEFAULT_CONNECTION, **connection}
        validate_params(connection, (PORT_ATTR, PROTOCOL_ATTR,
                                     USERNAME_ATTR, PASSWORD_ATTR))
        if not connection.get(HOST_ATTR):
            _LOG.debug(f'\'{HOST_ATTR}\' is not specified and will be '
                       f'resolved automatically')
            r8s_host = self.api_gateway_client.get_r8s_api_host()
            if not r8s_host:
                _LOG.error(f'No RightSizer API found. Please contact '
                           f'the support team.')
                return build_response(
                    code=RESPONSE_SERVICE_UNAVAILABLE_CODE,
                    content=f'No RightSizer API found. Please contact '
                            f'the support team.'
                )
            _LOG.debug(f'Setting host \'{r8s_host}\' into connection')
            connection[HOST_ATTR] = r8s_host

        customer = event.get(CUSTOMER_ATTR)
        user_customer = event.get(PARAM_USER_CUSTOMER)

        _LOG.debug(f'Validating user access to customer \'{customer}\'')
        if not self._is_allowed_customer(user_customer=user_customer,
                                         customer=customer):
            _LOG.warning(f'User is not allowed to create application for '
                         f'customer \'{customer}\'')
            return build_response(
                code=RESPONSE_FORBIDDEN_CODE,
                content=f'You are not allowed to create application for '
                        f'customer \'{customer}\''
            )

        _LOG.debug(f'Validating customer existence \'{customer}\'')
        customer_obj = self.customer_service.get(name=customer)
        if not customer_obj:
            _LOG.warning(f'Customer \'{customer}\' does not exist')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Customer \'{customer}\' does not exist'
            )

        input_storage = event.get(INPUT_STORAGE_ATTR)
        _LOG.debug(f'Validating input storage \'{input_storage}\'')
        input_storage_obj = self.storage_service.get_by_name(
            name=input_storage)

        self._validate_storage(
            storage_name=input_storage,
            storage=input_storage_obj,
            required_type=StorageTypeEnum.DATA_SOURCE.value
        )

        output_storage = event.get(OUTPUT_STORAGE_ATTR)
        _LOG.debug(f'Validating output storage \'{output_storage}\'')
        output_storage_obj = self.storage_service.get_by_name(
            name=output_storage)
        self._validate_storage(
            storage_name=output_storage,
            storage=output_storage_obj,
            required_type=StorageTypeEnum.STORAGE.value
        )

        password = connection.pop(PASSWORD_ATTR, None)
        connection_obj = ConnectionAttribute(**connection)

        try:
            _LOG.debug(f'Creating application')
            application = self.application_service. \
                create_rightsizer_application(
                customer_id=customer,
                description=description,
                input_storage=input_storage_obj,
                output_storage=output_storage_obj,
                connection=connection_obj,
                password=password
            )
        except ModularException as e:
            _LOG.error(f'Exception occurred while creating application: '
                       f'{e.content}')
            return build_response(
                code=e.code,
                content=e.content
            )

        _LOG.debug(f'Saving application '
                   f'\'{application.application_id}\'')
        self.application_service.save(application=application)

        application_dto = self.application_service.get_dto(
            application=application)
        _LOG.debug(f'Response: {application_dto}')
        return build_response(
            code=RESPONSE_OK_CODE,
            content=application_dto
        )