import { get } from 'lodash' ; import { schema , TypeOf } from '@kbn/config-schema' ; import { i18n } from '@kbn/i18n' ; import { RequestHandler } from 'src/core/server' ; import { RouteDependencies } from '../../types' ; import { serializeCluster } from '../../../common/lib' ; import { API_BASE_PATH } from '../../../common/constants' ; import { doesClusterExist } from '../../lib/does_cluster_exist' ; import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory' ; const paramsValidation = schema . object ( { nameOrNames : schema . string ( ) , } ) ; type RouteParams = TypeOf < typeof paramsValidation > ; export const register = ( deps ) : void => { const { router , lib : { handleEsError } , } = deps ; const deleteHandler = async ( ctx , request , response ) => { try { const { client : clusterClient } = ctx . core . elasticsearch ; const { nameOrNames } = request . params ; const names = nameOrNames . split ( ',' ) ; const itemsDeleted = [ ] ; const errors = [ ] ; const { body : clusterSettings } = await clusterClient . asCurrentUser . cluster . getSettings ( ) ; const validateClusterDoesExist = async ( name ) => { try { const existingCluster = await doesClusterExist ( clusterClient , name ) ; if ( ! existingCluster ) { return response . customError ( { statusCode : 404 , body : { message : i18n . translate ( 'xpack.remoteClusters.deleteRemoteCluster.noRemoteClusterErrorMessage' , { defaultMessage : 'There is no remote cluster with that name.' , } ) , } , } ) ; } } catch ( error ) { return response . customError ( { statusCode : 400 , body : error } ) ; } } ; const sendRequestToDeleteCluster = async ( name , hasDeprecatedProxySetting ) => { try { const body = serializeCluster ( { name , hasDeprecatedProxySetting } ) ; const { body : updateClusterResponse } = await clusterClient . asCurrentUser . cluster . putSettings ( { body , } ) ; const acknowledged = get ( updateClusterResponse , 'acknowledged' ) ; const cluster = get ( updateClusterResponse , `persistent.cluster.remote.${ name }` ) ; if ( acknowledged && ! cluster ) { return null ; } return response . customError ( { statusCode : 400 , body : { message : i18n . translate ( 'xpack.remoteClusters.deleteRemoteCluster.unknownRemoteClusterErrorMessage' , { defaultMessage : 'Unable to delete cluster, information still returned from ES.' , } ) , } , } ) ; } catch ( error ) { return handleEsError ( { error , response } ) ; } } ; const deleteCluster = async ( clusterName ) => { let error = await validateClusterDoesExist ( clusterName ) ; if ( ! error ) { const hasDeprecatedProxySetting = Boolean ( get ( clusterSettings , `persistent.cluster.remote[${ clusterName }].proxy` , undefined ) ) ; error = await sendRequestToDeleteCluster ( clusterName , hasDeprecatedProxySetting ) ; } if ( error ) { errors . push ( { name : clusterName , error } ) ; } else { itemsDeleted . push ( clusterName ) ; } } ; await Promise . all ( names . map ( deleteCluster ) ) ; return response . ok ( { body : { itemsDeleted , errors , } , } ) ; } catch ( error ) { return handleEsError ( { error , response } ) ; } } ; router . delete ( { path : `${ API_BASE_PATH }/{nameOrNames}` , validate : { params : paramsValidation , } , } , licensePreRoutingFactory ( deps , deleteHandler ) ) ; } ;