import { HttpSetup } from 'src/core/public' ; import { ESUpgradeStatus , CloudBackupStatus , ClusterUpgradeState , ResponseError , SystemIndicesMigrationStatus , ReindexStatusResponse , } from '../../../common/types' ; import { API_BASE_PATH , CLUSTER_UPGRADE_STATUS_POLL_INTERVAL_MS , DEPRECATION_LOGS_COUNT_POLL_INTERVAL_MS , CLOUD_BACKUP_STATUS_POLL_INTERVAL_MS , } from '../../../common/constants' ; import { UseRequestConfig , SendRequestConfig , SendRequestResponse , sendRequest as _sendRequest , useRequest as _useRequest , } from '../../shared_imports' ; type ClusterUpgradeStateListener = ( clusterUpgradeState : ClusterUpgradeState ) => void ; export class ApiService { private client : HttpSetup | undefined ; private clusterUpgradeStateListeners : ClusterUpgradeStateListener [ ] = [ ] ; private handleClusterUpgradeError ( error ) { const isClusterUpgradeError = Boolean ( error && error . statusCode === 426 ) ; if ( isClusterUpgradeError ) { const clusterUpgradeState = error ! . attributes ! . allNodesUpgraded ? 'isUpgradeComplete' : 'isUpgrading' ; this . clusterUpgradeStateListeners . forEach ( ( listener ) => listener ( clusterUpgradeState ) ) ; } } private useRequest < R = any > ( config ) { if ( ! this . client ) { throw new Error ( 'API service has not been initialized.' ) ; } const response = _useRequest < R , ResponseError > ( this . client , config ) ; this . handleClusterUpgradeError ( response . error ) ; return response ; } private async sendRequest < R = any > ( config ) { if ( ! this . client ) { throw new Error ( 'API service has not been initialized.' ) ; } const response = await _sendRequest < R , ResponseError > ( this . client , config ) ; this . handleClusterUpgradeError ( response . error ) ; return response ; } public setup ( httpClient ) { this . client = httpClient ; } public onClusterUpgradeStateChange ( listener ) { this . clusterUpgradeStateListeners . push ( listener ) ; } public useLoadClusterUpgradeStatus ( ) { return this . useRequest ( { path : `${ API_BASE_PATH }/cluster_upgrade_status` , method : 'get' , pollIntervalMs : CLUSTER_UPGRADE_STATUS_POLL_INTERVAL_MS , } ) ; } public useLoadCloudBackupStatus ( ) { return this . useRequest < CloudBackupStatus > ( { path : `${ API_BASE_PATH }/cloud_backup_status` , method : 'get' , pollIntervalMs : CLOUD_BACKUP_STATUS_POLL_INTERVAL_MS , } ) ; } public useLoadSystemIndicesMigrationStatus ( ) { return this . useRequest < SystemIndicesMigrationStatus > ( { path : `${ API_BASE_PATH }/system_indices_migration` , method : 'get' , } ) ; } public async migrateSystemIndices ( ) { const result = await this . sendRequest ( { path : `${ API_BASE_PATH }/system_indices_migration` , method : 'post' , } ) ; return result ; } public useLoadEsDeprecations ( ) { return this . useRequest < ESUpgradeStatus > ( { path : `${ API_BASE_PATH }/es_deprecations` , method : 'get' , } ) ; } public useLoadDeprecationLogging ( ) { return this . useRequest < { isDeprecationLogIndexingEnabled : boolean ; isDeprecationLoggingEnabled : boolean ; } > ( { path : `${ API_BASE_PATH }/deprecation_logging` , method : 'get' , } ) ; } public async updateDeprecationLogging ( loggingData ) { return await this . sendRequest ( { path : `${ API_BASE_PATH }/deprecation_logging` , method : 'put' , body : JSON . stringify ( loggingData ) , } ) ; } public getDeprecationLogsCount ( from ) { return this . useRequest < { count : number ; } > ( { path : `${ API_BASE_PATH }/deprecation_logging/count` , method : 'get' , query : { from } , pollIntervalMs : DEPRECATION_LOGS_COUNT_POLL_INTERVAL_MS , } ) ; } public deleteDeprecationLogsCache ( ) { return this . sendRequest ( { path : `${ API_BASE_PATH }/deprecation_logging/cache` , method : 'delete' , } ) ; } public async updateIndexSettings ( indexName , settings ) { return await this . sendRequest ( { path : `${ API_BASE_PATH }/${ indexName }/index_settings` , method : 'post' , body : { settings : JSON . stringify ( settings ) , } , } ) ; } public async upgradeMlSnapshot ( body ) { return await this . sendRequest ( { path : `${ API_BASE_PATH }/ml_snapshots` , method : 'post' , body , } ) ; } public async deleteMlSnapshot ( { jobId , snapshotId } ) { return await this . sendRequest ( { path : `${ API_BASE_PATH }/ml_snapshots/${ jobId }/${ snapshotId }` , method : 'delete' , } ) ; } public async getMlSnapshotUpgradeStatus ( { jobId , snapshotId , } ) { return await this . sendRequest ( { path : `${ API_BASE_PATH }/ml_snapshots/${ jobId }/${ snapshotId }` , method : 'get' , } ) ; } public useLoadMlUpgradeMode ( ) { return this . useRequest < { mlUpgradeModeEnabled : boolean ; } > ( { path : `${ API_BASE_PATH }/ml_upgrade_mode` , method : 'get' , } ) ; } public async getReindexStatus ( indexName ) { return await this . sendRequest < ReindexStatusResponse > ( { path : `${ API_BASE_PATH }/reindex/${ indexName }` , method : 'get' , } ) ; } public async startReindexTask ( indexName ) { return await this . sendRequest ( { path : `${ API_BASE_PATH }/reindex/${ indexName }` , method : 'post' , } ) ; } public async cancelReindexTask ( indexName ) { return await this . sendRequest ( { path : `${ API_BASE_PATH }/reindex/${ indexName }/cancel` , method : 'post' , } ) ; } public useLoadUpgradeStatus ( ) { return this . useRequest < { readyForUpgrade : boolean ; details : string ; } > ( { path : `${ API_BASE_PATH }/status` , method : 'get' , } ) ; } } export const apiService = new ApiService ( ) ;