import { wrapError } from '../client/error_wrapper' ; import { RouteInitialization , SavedObjectsRouteDeps } from '../types' ; import { checksFactory , syncSavedObjectsFactory } from '../saved_objects' ; import { jobsAndSpaces , jobsAndCurrentSpace , syncJobObjects , syncCheckSchema , canDeleteJobSchema , jobTypeSchema , } from './schemas/saved_objects' ; import { spacesUtilsProvider } from '../lib/spaces_utils' ; import { JobType } from '../../common/types/saved_objects' ; /**
 * Routes for job saved object management
 */ export function savedObjectsRoutes ( { router , routeGuard } , { getSpaces , resolveMlCapabilities } ) { apiGroup api apiName apiDescription router . get ( { path : '/api/ml/saved_objects/status' , validate : false , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , response , jobSavedObjectService } ) => { try { const { checkStatus } = checksFactory ( client , jobSavedObjectService ) ; const status = await checkStatus ( ) ; return response . ok ( { body : status , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/saved_objects/sync' , validate : { query : syncJobObjects , } , options : { tags : [ 'access:ml:canCreateJob' , 'access:ml:canCreateDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , request , response , jobSavedObjectService } ) => { try { const { simulate } = request . query ; const { syncSavedObjects } = syncSavedObjectsFactory ( client , jobSavedObjectService ) ; const savedObjects = await syncSavedObjects ( simulate ) ; return response . ok ( { body : savedObjects , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/saved_objects/initialize' , validate : { query : syncJobObjects , } , options : { tags : [ 'access:ml:canCreateJob' , 'access:ml:canCreateDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , request , response , jobSavedObjectService } ) => { try { const { simulate } = request . query ; const { initSavedObjects } = syncSavedObjectsFactory ( client , jobSavedObjectService ) ; const savedObjects = await initSavedObjects ( simulate ) ; return response . ok ( { body : savedObjects , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . post ( { path : '/api/ml/saved_objects/sync_check' , validate : { body : syncCheckSchema , } , options : { tags : [ 'access:ml:canGetJobs' , 'access:ml:canGetDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , request , response , jobSavedObjectService } ) => { try { const { jobType } = request . body ; const { isSyncNeeded } = syncSavedObjectsFactory ( client , jobSavedObjectService ) ; const result = await isSyncNeeded ( jobType as JobType ) ; return response . ok ( { body : { result } , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/saved_objects/update_jobs_spaces' , validate : { body : jobsAndSpaces , } , options : { tags : [ 'access:ml:canCreateJob' , 'access:ml:canCreateDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { request , response , jobSavedObjectService } ) => { try { const { jobType , jobIds , spacesToAdd , spacesToRemove } = request . body ; const body = await jobSavedObjectService . updateJobsSpaces ( jobType , jobIds , spacesToAdd , spacesToRemove ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/saved_objects/remove_job_from_current_space' , validate : { body : jobsAndCurrentSpace , } , options : { tags : [ 'access:ml:canCreateJob' , 'access:ml:canCreateDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { request , response , jobSavedObjectService } ) => { try { const { jobType , jobIds } = request . body ; const { getCurrentSpaceId } = spacesUtilsProvider ( getSpaces , request ) ; const currentSpaceId = await getCurrentSpaceId ( ) ; if ( currentSpaceId === null ) { return response . ok ( { body : jobIds . map ( ( id ) => ( { [ id ] : { success : false , error : 'Cannot remove current space. Spaces plugin is disabled.' , } , } ) ) , } ) ; } const body = await jobSavedObjectService . updateJobsSpaces ( jobType , jobIds , [ ] , [ currentSpaceId ] ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/saved_objects/jobs_spaces' , validate : false , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { response , jobSavedObjectService , client } ) => { try { const { checkStatus } = checksFactory ( client , jobSavedObjectService ) ; const allStatuses = Object . values ( ( await checkStatus ( ) ) . savedObjects ) . flat ( ) ; const body = allStatuses . filter ( ( s ) => s . checks . jobExists ) . reduce ( ( acc , cur ) => { const type = cur . type ; if ( acc [ type ] === undefined ) { acc [ type ] = { } ; } acc [ type ] [ cur . jobId ] = cur . namespaces ; return acc ; } , { } as { [ id ] : { [ id ] : string [ ] | undefined ; } ; } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema apiSuccessExample router . post ( { path : '/api/ml/saved_objects/can_delete_job/{jobType}' , validate : { params : jobTypeSchema , body : canDeleteJobSchema , } , options : { tags : [ 'access:ml:canGetJobs' , 'access:ml:canGetDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { request , response , jobSavedObjectService , client } ) => { try { const { jobType } = request . params ; const { jobIds } = request . body ; const { canDeleteJobs } = checksFactory ( client , jobSavedObjectService ) ; const body = await canDeleteJobs ( request , jobType , jobIds , getSpaces !== undefined , resolveMlCapabilities ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }