def post()

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


    def post(self, event):
        _LOG.debug(f'Create parent event: {event}')
        validate_params(event, (APPLICATION_ID_ATTR, DESCRIPTION_ATTR,
                                SCOPE_ATTR))
        _LOG.debug(f'Resolving applications')
        applications = self.application_service.resolve_application(
            event=event)

        if not applications:
            _LOG.warning(f'No application found matching given query.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'No application found matching given query.'
            )
        if len(applications) > 1:
            _LOG.error(f'Exactly one application must be identified.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Exactly one application must be identified.'
            )
        application = applications[0]
        _LOG.debug(f'Target application \'{application.application_id}\'')

        customer = application.customer_id
        _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'
            )

        scope = event.get(SCOPE_ATTR).upper()

        if scope not in list(ParentScope):
            _LOG.error(f'Invalid value specified for \'{SCOPE_ATTR}\'. '
                       f'Allowed options: {", ".join(list(ParentScope))}')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Invalid value specified for \'{SCOPE_ATTR}\'. '
                        f'Allowed options: {", ".join(list(ParentScope))}'
            )

        tenant_obj = None
        tenant_name = event.get(TENANT_ATTR)
        if scope != ParentScope.ALL.value:
            if not tenant_name:
                _LOG.error(f'Attribute \'{TENANT_ATTR}\' must be specified if '
                           f'\'{SCOPE_ATTR}\' attribute is set to '
                           f'\'{ParentScope.ALL.value}\'')
                return build_response(
                    code=RESPONSE_BAD_REQUEST_CODE,
                    content=f'Attribute \'{TENANT_ATTR}\' must be specified '
                            f'if \'{SCOPE_ATTR}\' attribute is set to '
                            f'\'{ParentScope.ALL.value}\''
                )
            _LOG.debug(f'Describing tenant \'{tenant_name}\'')
            tenant_obj = self.tenant_service.get(tenant_name=tenant_name)
            if not tenant_obj:
                _LOG.error(f'Tenant \'{tenant_name}\' does not exist.')
                return build_response(
                    code=RESPONSE_BAD_REQUEST_CODE,
                    content=f'Tenant \'{tenant_name}\' does not exist.'
                )
        elif tenant_name:
            _LOG.error(f'\'{TENANT_ATTR}\' attribute must not be used with '
                       f'{ParentScope.ALL.value} scope')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'\'{TENANT_ATTR}\' attribute must not be used with '
                        f'{ParentScope.ALL.value} scope'
            )

        cloud = event.get(CLOUD_ATTR)
        if cloud:
            _LOG.debug(f'Validation cloud: {cloud}')
            if cloud not in CLOUDS:
                _LOG.error(f'Some of the specified clouds are invalid. '
                           f'Available clouds: {", ".join(CLOUDS)}')
                return build_response(
                    code=RESPONSE_BAD_REQUEST_CODE,
                    content=f'Some of the specified clouds are invalid. '
                            f'Available clouds: {", ".join(CLOUDS)}'
                )
        if cloud and scope != ParentScope.ALL.value:
            _LOG.error(f'Parent cloud can only be used with '
                       f'{ParentScope.ALL.value} Parent scope')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Parent cloud can only be used with '
                        f'{ParentScope.ALL.value} Parent scope'
            )

        description = event.get(DESCRIPTION_ATTR)
        if not description:
            _LOG.error('Description can\'t be empty.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content='Description can\'t be empty.'
            )

        _LOG.debug(f'Creating parent')
        parent = self.parent_service.create(
            application_id=application.application_id,
            customer_id=customer,
            parent_type=RIGHTSIZER_PARENT_TYPE,
            description=description,
            meta={},
            scope=scope,
            cloud=cloud,
            tenant_name=tenant_obj.name if tenant_obj else None
        )

        parent_dto = self.parent_service.get_dto(parent=parent)
        _LOG.debug(f'Created parent \'{parent_dto}\'')

        if tenant_obj:
            try:
                _LOG.debug(f'Adding parent \'{parent.parent_id}\' to tenant '
                           f'\'{tenant_obj.name}\' parent map')
                self.tenant_service.add_to_parent_map(
                    tenant=tenant_obj,
                    parent=parent,
                    type_=TENANT_PARENT_MAP_RIGHTSIZER_TYPE
                )
            except ModularException as e:
                _LOG.error(e.content)
                return build_response(
                    code=e.code,
                    content=e.content
                )

        self.parent_service.save(parent=parent)
        _LOG.debug(f'Parent \'{parent.parent_id}\' has been saved')

        return build_response(
            code=RESPONSE_OK_CODE,
            content=parent_dto
        )