import { Client , CreateDocumentParams } from 'elasticsearch' ; import { ToolingLog , OsdClient } from '@osd/dev-utils' ; import { Stats } from '../stats' ; import { deleteIndex } from './delete_index' ; /**
 * Deletes all indices that start with `.kibana`
 */ export async function deleteOpenSearchDashboardsIndices ( { client , stats , log , } ) { const indexNames = await fetchOpenSearchDashboardsIndices ( client ) ; if ( ! indexNames . length ) { return ; } await client . indices . putSettings ( { index : indexNames , body : { index : { blocks : { read_only : false } } } , } ) ; await deleteIndex ( { client , stats , index : indexNames , log , } ) ; return indexNames ; } /**
 * Given an opensearch client, and a logger, migrates the `.kibana` index. This
 * builds up an object that implements just enough of the osdMigrations interface
 * as is required by migrations.
 */ export async function migrateOpenSearchDashboardsIndex ( { client , osdClient , } ) { await client . indices . putMapping ( { index : '.kibana' , body : { dynamic : true , } , } as any ) ; await osdClient . savedObjects . migrate ( ) ; } /**
 * Migrations mean that the OpenSearch Dashboards index will look something like:
 * .kibana, .kibana_1, .kibana_323, etc. This finds all indices starting
 * with .kibana then filters out any that aren't actually OpenSearch Dashboards's core
 * index (e.g. we don't want to remove .opensearch_dashboards_task_manager or the like).
 */ async function fetchOpenSearchDashboardsIndices ( client ) { const opensearchDashboardsIndices = await client . cat . indices ( { index : '.kibana*' , format : 'json' , } ) ; const isOpenSearchDashboardsIndex = ( index ) => /^\.kibana(:?_\d*)?$/ . test ( index ) ; return opensearchDashboardsIndices . map ( ( x ) => x . index ) . filter ( isOpenSearchDashboardsIndex ) ; } export async function cleanOpenSearchDashboardsIndices ( { client , stats , log , opensearchDashboardsPluginIds , } ) { if ( ! opensearchDashboardsPluginIds . includes ( 'spaces' ) ) { return await deleteOpenSearchDashboardsIndices ( { client , stats , log , } ) ; } while ( true ) { const resp = await client . deleteByQuery ( { index : `.kibana` , body : { query : { bool : { must_not : { ids : { type : '_doc' , values : [ 'space:default' ] , } , } , } , } , } , ignore : [ 409 ] , } ) ; if ( resp . total !== resp . deleted ) { log . warning ( 'delete by query deleted %d of %d total documents, trying again' , resp . deleted , resp . total ) ; continue ; } break ; } log . warning ( `since spaces are enabled, all objects other than the default space were deleted from ` + `.kibana rather than deleting the whole index` ) ; stats . deletedIndex ( '.kibana' ) ; } export async function createDefaultSpace ( { index , client } ) { await client . create ( { index , type : '_doc' , id : 'space:default' , ignore : 409 , body : { type : 'space' , updated_at : new Date ( ) . toISOString ( ) , space : { name : 'Default Space' , description : 'This is the default space' , disabledFeatures : [ ] , _reserved : true , } , } , } as CreateDocumentParams ) ; }