import { schema } from '@kbn/config-schema' ; import { ElasticsearchClient } from 'kibana/server' ; import { RouteDependencies } from '../../../types' ; import { addBasePath } from '../../../services' ; async function addLifecyclePolicy ( client , indexName , policyName , alias ) { const body = { lifecycle : { name : policyName , rollover_alias : alias , } , } ; return client . indices . putSettings ( { index : indexName , body } ) ; } const bodySchema = schema . object ( { indexName : schema . string ( ) , policyName : schema . string ( ) , alias : schema . maybe ( schema . string ( ) ) , } ) ; export function registerAddPolicyRoute ( { router , license , lib : { handleEsError } , } ) { router . post ( { path : addBasePath ( '/index/add' ) , validate : { body : bodySchema } } , license . guardApiRoute ( async ( context , request , response ) => { const body = request . body as typeof bodySchema . type ; const { indexName , policyName , alias = '' } = body ; try { await addLifecyclePolicy ( context . core . elasticsearch . client . asCurrentUser , indexName , policyName , alias ) ; return response . ok ( ) ; } catch ( error ) { return handleEsError ( { error , response } ) ; } } ) ) ; }