def post()

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


    def post(self, event):
        _LOG.debug(f'Create licensed parent event: {event}')
        validate_params(event, (APPLICATION_ID_ATTR, DESCRIPTION_ATTR,
                                CLOUD_ATTR, TENANT_LICENSE_KEY_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'
            )

        cloud = event.get(CLOUD_ATTR).upper()
        _LOG.debug(f'Validation cloud: {cloud}')
        if not isinstance(cloud, str) or cloud not in CLOUDS:
            _LOG.error(f'Invalid cloud specified \'{cloud}\'. '
                       f'Available clouds: {", ".join(CLOUDS)}')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Invalid cloud specified \'{cloud}\'. '
                        f'Available clouds: {", ".join(CLOUDS)}'
            )

        tenant_license_key = event.get(TENANT_LICENSE_KEY_ATTR)
        _LOG.debug(f'Activating license \'{tenant_license_key}\' '
                   f'for customer')
        license_obj = self.activate_license(
            tenant_license_key=tenant_license_key,
            customer=customer
        )
        self._execute_license_sync(
            license_obj=license_obj,
            customer=customer
        )
        license_key = license_obj.license_key
        algorithm = license_obj.algorithm_id

        _LOG.debug(f'Validating algorithm \'{algorithm}\'')
        algorithm_obj: Algorithm = self.algorithm_service.get_by_name(
            name=algorithm)
        if not algorithm_obj or algorithm_obj.customer != customer:
            _LOG.error(f'Algorithm \'{algorithm}\' does not exist.')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Algorithm \'{algorithm}\' does not exist.'
            )
        if cloud != CLOUD_ALL and cloud != algorithm_obj.cloud.value:
            _LOG.error(f'Algorithm \'{algorithm}\' is not suitable for '
                       f'cloud \'{cloud}\'. Algorithm\'s cloud: '
                       f'{algorithm_obj.cloud.value}')
            return build_response(
                code=RESPONSE_BAD_REQUEST_CODE,
                content=f'Algorithm \'{algorithm}\' is not suitable for '
                        f'cloud \'{cloud}\'. Algorithm\'s cloud: '
                        f'{algorithm_obj.cloud.value}'
            )

        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.'
            )

        tenants = self.license_service.list_allowed_tenants(
            license_obj=license_obj,
            customer=application.customer_id
        )
        if tenants is None:  # license activation is forbidden
            _LOG.error(f'Activation of license \'{license_obj.license_key}\' '
                       f'is forbidden to customer '
                       f'\'{application.customer_id}\'')
            return build_response(
                code=RESPONSE_FORBIDDEN_CODE,
                content=f'Activation of license \'{license_obj.license_key}\' '
                        f'is forbidden to customer '
                        f'\'{application.customer_id}\''
            )

        meta = LicensesParentMeta(
            cloud=cloud,
            algorithm=algorithm_obj.name,
            license_key=license_key,
        )
        parents = []
        if tenants:
            for index, tenant in enumerate(tenants, start=1):
                _LOG.debug(f'{index}/{len(tenants)} Creating RIGHTSIZER_LICENSES '
                           f'parent for tenant {tenant}')
                parent = self.parent_service.create_tenant_scope(
                    application_id=application.application_id,
                    customer_id=customer,
                    type_=RIGHTSIZER_LICENSES_PARENT_TYPE,
                    description=description,
                    meta=meta.as_dict(),
                    tenant_name=tenant
                )
                parents.append(parent)
        else:
            _LOG.debug(f'Creating RIGHTSIZER_LICENSES parent for all tenants '
                       f'in cloud: {cloud}')
            parent = self.parent_service.create_all_scope(
                application_id=application.application_id,
                customer_id=customer,
                type_=RIGHTSIZER_LICENSES_PARENT_TYPE,
                description=description,
                meta=meta.as_dict(),
                cloud=cloud
            )
            parents.append(parent)

        _LOG.debug(f'Going to save {len(parents)} parent(s).')
        for parent in parents:
            self.parent_service.save(parent=parent)

        parent_dtos = [self.parent_service.get_dto(parent=parent)
                       for parent in parents]
        _LOG.debug(f'Created parents: \'{parent_dtos}\'')

        for parent in parents:
            if not parent.tenant_name:
                continue
            try:
                _LOG.debug(f'Going to activate tenants {tenants} for parent '
                           f'\'{parent.parent_id}\'')
                self.activate_tenant(
                    tenant_name=parent.tenant_name,
                    parent=parent
                )
            except ModularException as e:
                _LOG.error(e.content)
                return build_response(
                    code=e.code,
                    content=e.content
                )

        return build_response(
            code=RESPONSE_OK_CODE,
            content=parent_dtos
        )