import { ElasticsearchClient } from 'kibana/server' ; import type { TransportResult } from '@elastic/elasticsearch' ; import { PolicyFromES , SerializedPolicy } from '../../../../common/types' ; import { RouteDependencies } from '../../../types' ; import { addBasePath } from '../../../services' ; interface PoliciesMap { [ K ] : { modified_date : string ; policy : SerializedPolicy ; version : number ; in_use_by : { indices : string [ ] ; data_streams : string [ ] ; composable_templates : string [ ] ; } ; } ; } function formatPolicies ( policiesMap ) { return Object . keys ( policiesMap ) . reduce ( ( accum , lifecycleName ) => { const policyEntry = policiesMap [ lifecycleName ] ; const { in_use_by : { indices , data_streams : dataStreams , composable_templates : indexTemplates } , modified_date : modifiedDate , policy , version , } = policyEntry ; accum . push ( { name : lifecycleName , modifiedDate , version , policy , indices , dataStreams , indexTemplates , } ) ; return accum ; } , [ ] ) ; } async function fetchPolicies ( client ) { const options = { ignore : [ 404 ] , } ; return client . ilm . getLifecycle ( { } , options ) ; } export function registerFetchRoute ( { router , license , lib : { handleEsError } } ) { router . get ( { path : addBasePath ( '/policies' ) , validate : false } , license . guardApiRoute ( async ( context , request , response ) => { const { asCurrentUser } = context . core . elasticsearch . client ; try { const policiesResponse = await fetchPolicies ( asCurrentUser ) ; if ( policiesResponse . statusCode === 404 ) { return response . ok ( { body : [ ] } ) ; } return response . ok ( { body : formatPolicies ( policiesResponse . body ) } ) ; } catch ( error ) { return handleEsError ( { error , response } ) ; } } ) ) ; }