import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { asyncForEach } from '@kbn/std' ; import { getIlmPolicy , getIndexTemplate } from './documents' ; import { EsContext } from './context' ; export async function initializeEs ( esContext ) { esContext . logger . debug ( 'initializing elasticsearch resources starting' ) ; try { await initializeEsResources ( esContext ) ; } catch ( err ) { esContext . logger . error ( `error initializing elasticsearch resources: ${ err . message }` ) ; return false ; } esContext . logger . debug ( 'initializing elasticsearch resources complete' ) ; return true ; } async function initializeEsResources ( esContext ) { const steps = new EsInitializationSteps ( esContext ) ; await steps . setExistingAssetsToHidden ( ) ; await steps . createIlmPolicyIfNotExists ( ) ; await steps . createIndexTemplateIfNotExists ( ) ; await steps . createInitialIndexIfNotExists ( ) ; } class EsInitializationSteps { constructor ( private readonly esContext ) { this . esContext = esContext ; } async setExistingIndexTemplatesToHidden ( ) { let indexTemplates = { } ; try { indexTemplates = await this . esContext . esAdapter . getExistingLegacyIndexTemplates ( this . esContext . esNames . indexPattern ) ; } catch ( err ) { this . esContext . logger . error ( `error getting existing index templates - ${ err . message }` ) ; } asyncForEach ( Object . keys ( indexTemplates ) , async ( indexTemplateName ) => { try { const hidden = indexTemplates [ indexTemplateName ] ?. settings ?. index ?. hidden ; if ( hidden !== true && hidden !== 'true' ) { this . esContext . logger . debug ( `setting existing "${ indexTemplateName }" index template to hidden.` ) ; await this . esContext . esAdapter . setLegacyIndexTemplateToHidden ( indexTemplateName , indexTemplates [ indexTemplateName ] ) ; } } catch ( err ) { this . esContext . logger . error ( `error setting existing "${ indexTemplateName }" index template to hidden - ${ err . message }` ) ; } } ) ; } async setExistingIndicesToHidden ( ) { let indices = { } ; try { indices = await this . esContext . esAdapter . getExistingIndices ( this . esContext . esNames . indexPattern ) ; } catch ( err ) { this . esContext . logger . error ( `error getting existing indices - ${ err . message }` ) ; } asyncForEach ( Object . keys ( indices ) , async ( indexName ) => { try { const hidden = indices [ indexName ] ?. settings ?. index ?. hidden ; if ( hidden !== true && hidden !== 'true' ) { this . esContext . logger . debug ( `setting existing ${ indexName } index to hidden.` ) ; await this . esContext . esAdapter . setIndexToHidden ( indexName ) ; } } catch ( err ) { this . esContext . logger . error ( `error setting existing "${ indexName }" index to hidden - ${ err . message }` ) ; } } ) ; } async setExistingIndexAliasesToHidden ( ) { let indexAliases = { } ; try { indexAliases = await this . esContext . esAdapter . getExistingIndexAliases ( this . esContext . esNames . indexPattern ) ; } catch ( err ) { this . esContext . logger . error ( `error getting existing index aliases - ${ err . message }` ) ; } asyncForEach ( Object . keys ( indexAliases ) , async ( indexName ) => { try { const aliases = indexAliases [ indexName ] ?. aliases ; const hasNotHiddenAliases = Object . keys ( aliases ) . some ( ( alias ) => { return ( aliases [ alias ] as estypes . IndicesAlias ) ?. is_hidden !== true ; } ) ; if ( hasNotHiddenAliases ) { this . esContext . logger . debug ( `setting existing "${ indexName }" index aliases to hidden.` ) ; await this . esContext . esAdapter . setIndexAliasToHidden ( indexName , indexAliases [ indexName ] ) ; } } catch ( err ) { this . esContext . logger . error ( `error setting existing "${ indexName }" index aliases - ${ err . message }` ) ; } } ) ; } async setExistingAssetsToHidden ( ) { await this . setExistingIndexTemplatesToHidden ( ) ; await this . setExistingIndicesToHidden ( ) ; await this . setExistingIndexAliasesToHidden ( ) ; } async createIlmPolicyIfNotExists ( ) { const exists = await this . esContext . esAdapter . doesIlmPolicyExist ( this . esContext . esNames . ilmPolicy ) ; if ( ! exists ) { await this . esContext . esAdapter . createIlmPolicy ( this . esContext . esNames . ilmPolicy , getIlmPolicy ( ) ) ; } } async createIndexTemplateIfNotExists ( ) { const exists = await this . esContext . esAdapter . doesIndexTemplateExist ( this . esContext . esNames . indexTemplate ) ; if ( ! exists ) { const templateBody = getIndexTemplate ( this . esContext . esNames ) ; await this . esContext . esAdapter . createIndexTemplate ( this . esContext . esNames . indexTemplate , templateBody ) ; } } async createInitialIndexIfNotExists ( ) { const exists = await this . esContext . esAdapter . doesAliasExist ( this . esContext . esNames . alias ) ; if ( ! exists ) { await this . esContext . esAdapter . createIndex ( this . esContext . esNames . initialIndex , { aliases : { [ this . esContext . esNames . alias ] : { is_write_index : true , is_hidden : true , } , } , } ) ; } } }