From b22e9ab50be155527f9505952597b9b62cb334cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 14:06:13 +0530 Subject: [PATCH 01/30] Move server code to "np_ready" folder --- .../cross_cluster_replication/index.js | 2 +- .../common/constants/plugin.ts | 8 +- .../{server => common}/index.ts | 5 +- .../legacy/plugins/index_management/index.ts | 39 +++----- .../index_management/server/np_ready/index.ts | 12 +++ .../{ => np_ready}/index_management_data.ts | 0 .../{ => np_ready}/lib/fetch_aliases.test.ts | 0 .../{ => np_ready}/lib/fetch_aliases.ts | 0 .../{ => np_ready}/lib/fetch_indices.ts | 1 + .../lib/get_managed_templates.ts | 0 .../server/np_ready/plugin.ts | 95 +++++++++++++++++++ .../server/np_ready/routes/api/index.ts | 9 ++ .../routes/api/indices/index.ts | 0 .../api/indices/register_clear_cache_route.ts | 36 +++++++ .../api/indices/register_close_route.ts | 0 .../api/indices/register_delete_route.ts | 0 .../api/indices/register_flush_route.ts | 0 .../api/indices/register_forcemerge_route.ts | 0 .../api/indices/register_freeze_route.ts | 0 .../api/indices/register_indices_routes.ts | 32 +++++++ .../routes/api/indices/register_list_route.ts | 16 ++++ .../routes/api/indices/register_open_route.ts | 0 .../api/indices/register_refresh_route.ts | 0 .../api/indices/register_reload_route.ts | 0 .../api/indices/register_unfreeze_route.ts | 0 .../routes/api/mapping/index.ts | 0 .../api/mapping/register_mapping_route.ts | 0 .../routes/api/settings/index.ts | 0 .../api/settings/register_load_route.ts | 0 .../api/settings/register_settings_routes.ts | 0 .../api/settings/register_update_route.ts | 0 .../{ => np_ready}/routes/api/stats/index.ts | 0 .../routes/api/stats/register_stats_route.ts | 0 .../routes/api/templates/index.ts | 0 .../api/templates/register_create_route.ts | 0 .../api/templates/register_delete_route.ts | 0 .../api/templates/register_get_routes.ts | 0 .../api/templates/register_template_routes.ts | 0 .../api/templates/register_update_route.ts | 0 .../server/np_ready/routes/index.ts | 21 ++++ .../index_management/server/np_ready/types.ts | 27 ++++++ .../plugins/index_management/server/plugin.ts | 50 ---------- .../api/indices/register_clear_cache_route.ts | 28 ------ .../api/indices/register_indices_routes.ts | 32 ------- .../routes/api/indices/register_list_route.ts | 16 ---- 45 files changed, 270 insertions(+), 159 deletions(-) rename x-pack/legacy/plugins/index_management/{server => common}/index.ts (69%) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/index.ts rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/index_management_data.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/lib/fetch_aliases.test.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/lib/fetch_aliases.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/lib/fetch_indices.ts (99%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/lib/get_managed_templates.ts (100%) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/routes/api/index.ts rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/index.ts (100%) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_close_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_delete_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_flush_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_forcemerge_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_freeze_route.ts (100%) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_open_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_refresh_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_reload_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/indices/register_unfreeze_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/mapping/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/mapping/register_mapping_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/settings/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/settings/register_load_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/settings/register_settings_routes.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/settings/register_update_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/stats/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/stats/register_stats_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/templates/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/templates/register_create_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/templates/register_delete_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/templates/register_get_routes.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/templates/register_template_routes.ts (100%) rename x-pack/legacy/plugins/index_management/server/{ => np_ready}/routes/api/templates/register_update_route.ts (100%) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/types.ts delete mode 100644 x-pack/legacy/plugins/index_management/server/plugin.ts delete mode 100644 x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts delete mode 100644 x-pack/legacy/plugins/index_management/server/routes/api/indices/register_indices_routes.ts delete mode 100644 x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts diff --git a/x-pack/legacy/plugins/cross_cluster_replication/index.js b/x-pack/legacy/plugins/cross_cluster_replication/index.js index 22e8e73963ccd..953ada1809cf8 100644 --- a/x-pack/legacy/plugins/cross_cluster_replication/index.js +++ b/x-pack/legacy/plugins/cross_cluster_replication/index.js @@ -9,7 +9,7 @@ import { PLUGIN } from './common/constants'; import { registerLicenseChecker } from './server/lib/register_license_checker'; import { registerRoutes } from './server/routes/register_routes'; import { ccrDataEnricher } from './cross_cluster_replication_data'; -import { addIndexManagementDataEnricher } from '../index_management/server/index_management_data'; +import { addIndexManagementDataEnricher } from '../index_management/server/np_ready/index_management_data'; export function crossClusterReplication(kibana) { return new kibana.Plugin({ id: PLUGIN.ID, diff --git a/x-pack/legacy/plugins/index_management/common/constants/plugin.ts b/x-pack/legacy/plugins/index_management/common/constants/plugin.ts index 1f283464df9a0..2cd137f62d3db 100644 --- a/x-pack/legacy/plugins/index_management/common/constants/plugin.ts +++ b/x-pack/legacy/plugins/index_management/common/constants/plugin.ts @@ -4,13 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LICENSE_TYPE_BASIC } from '../../../../common/constants'; +import { LicenseType } from '../../../../../plugins/licensing/common/types'; + +const basicLicense: LicenseType = 'basic'; export const PLUGIN = { - ID: 'index_management', + id: 'index_management', + minimumLicenseType: basicLicense, getI18nName: (i18n: any): string => i18n.translate('xpack.idxMgmt.appTitle', { defaultMessage: 'Index Management', }), - MINIMUM_LICENSE_REQUIRED: LICENSE_TYPE_BASIC, }; diff --git a/x-pack/legacy/plugins/index_management/server/index.ts b/x-pack/legacy/plugins/index_management/common/index.ts similarity index 69% rename from x-pack/legacy/plugins/index_management/server/index.ts rename to x-pack/legacy/plugins/index_management/common/index.ts index c405f7816337d..0cc4ba79711ce 100644 --- a/x-pack/legacy/plugins/index_management/server/index.ts +++ b/x-pack/legacy/plugins/index_management/common/index.ts @@ -3,8 +3,5 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { IndexMgmtPlugin } from './plugin'; -export function plugin() { - return new IndexMgmtPlugin(); -} +export { PLUGIN, API_BASE_PATH } from './constants'; diff --git a/x-pack/legacy/plugins/index_management/index.ts b/x-pack/legacy/plugins/index_management/index.ts index f2a543337199f..c3e4d54975c0b 100644 --- a/x-pack/legacy/plugins/index_management/index.ts +++ b/x-pack/legacy/plugins/index_management/index.ts @@ -5,19 +5,15 @@ */ import { resolve } from 'path'; -import { i18n } from '@kbn/i18n'; import { Legacy } from 'kibana'; -import { createRouter } from '../../server/lib/create_router'; -import { registerLicenseChecker } from '../../server/lib/register_license_checker'; -import { PLUGIN, API_BASE_PATH } from './common/constants'; -import { LegacySetup } from './server/plugin'; -import { plugin as initServerPlugin } from './server'; +import { PLUGIN } from './common/constants'; +import { plugin as initServerPlugin, Dependencies } from './server/np_ready'; export type ServerFacade = Legacy.Server; export function indexManagement(kibana: any) { return new kibana.Plugin({ - id: PLUGIN.ID, + id: PLUGIN.id, configPrefix: 'xpack.index_management', publicDir: resolve(__dirname, 'public'), require: ['kibana', 'elasticsearch', 'xpack_main'], @@ -30,26 +26,19 @@ export function indexManagement(kibana: any) { init(server: ServerFacade) { const coreSetup = server.newPlatform.setup.core; - const pluginsSetup = {}; - - const __LEGACY: LegacySetup = { - router: createRouter(server, PLUGIN.ID, `${API_BASE_PATH}/`), - plugins: { - license: { - registerLicenseChecker: registerLicenseChecker.bind( - null, - server, - PLUGIN.ID, - PLUGIN.getI18nName(i18n), - PLUGIN.MINIMUM_LICENSE_REQUIRED as 'basic' - ), - }, - elasticsearch: server.plugins.elasticsearch, - }, + const pluginDependencies: Dependencies = { + elasticsearch: server.plugins.elasticsearch, + licensing: {} as any, }; - const serverPlugin = initServerPlugin(); - const indexMgmtSetup = serverPlugin.setup(coreSetup, pluginsSetup, __LEGACY); + const mockLogger = { + get: () => ({ + info() {}, + }), + }; + + const serverPlugin = initServerPlugin({ logger: mockLogger } as any); + const indexMgmtSetup = serverPlugin.setup(coreSetup, pluginDependencies); server.expose( 'addIndexManagementDataEnricher', diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/index.ts new file mode 100644 index 0000000000000..866b374740d3b --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/index.ts @@ -0,0 +1,12 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { PluginInitializerContext } from 'src/core/server'; +import { IndexMgmtServerPlugin } from './plugin'; + +export const plugin = (ctx: PluginInitializerContext) => new IndexMgmtServerPlugin(ctx); + +export { Dependencies } from './types'; diff --git a/x-pack/legacy/plugins/index_management/server/index_management_data.ts b/x-pack/legacy/plugins/index_management/server/np_ready/index_management_data.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/index_management_data.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/index_management_data.ts diff --git a/x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.test.ts b/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.test.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.test.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.test.ts diff --git a/x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.ts b/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.ts diff --git a/x-pack/legacy/plugins/index_management/server/lib/fetch_indices.ts b/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts similarity index 99% rename from x-pack/legacy/plugins/index_management/server/lib/fetch_indices.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts index 19a5cd81c919b..72263fe576a26 100644 --- a/x-pack/legacy/plugins/index_management/server/lib/fetch_indices.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts @@ -5,6 +5,7 @@ */ import { fetchAliases } from './fetch_aliases'; import { getIndexManagementDataEnrichers } from '../index_management_data'; + interface Hit { health: string; status: string; diff --git a/x-pack/legacy/plugins/index_management/server/lib/get_managed_templates.ts b/x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/lib/get_managed_templates.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts new file mode 100644 index 0000000000000..2bc1423d5e9da --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts @@ -0,0 +1,95 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { i18n } from '@kbn/i18n'; +import { CoreSetup, Plugin, Logger, PluginInitializerContext } from 'src/core/server'; + +import { LICENSE_CHECK_STATE } from '../../../../../plugins/licensing/common/types'; +import { PLUGIN } from '../../common'; +import { Dependencies, RouteDependencies, LicenseStatus } from './types'; +// import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch'; + +// import { Router } from '../../../../server/lib/create_router'; +import { addIndexManagementDataEnricher } from './index_management_data'; +import { registerApiRoutes } from './routes'; + +// export interface LegacySetup { +// router: Router; +// plugins: { +// elasticsearch: ElasticsearchPlugin; +// license: { +// registerLicenseChecker: () => void; +// }; +// }; +// } + +export interface IndexMgmtSetup { + addIndexManagementDataEnricher: (enricher: any) => void; +} + +export class IndexMgmtServerPlugin implements Plugin { + licenseStatus: LicenseStatus; + log: Logger; + + constructor({ logger }: PluginInitializerContext) { + this.log = logger.get(); + this.licenseStatus = { isValid: false }; + } + + setup({ http }: CoreSetup, { licensing, elasticsearch }: Dependencies): IndexMgmtSetup { + // const serverFacade = { + // plugins: { + // elasticsearch: __LEGACY.plugins.elasticsearch, + // }, + // }; + + // license.registerLicenseChecker(); + + const router = http.createRouter(); + + const routeDependencies: RouteDependencies = { + router, + plugins: { + elasticsearch, + license: { + getStatus: () => this.licenseStatus, + }, + }, + }; + + registerApiRoutes(routeDependencies); + + // TODO: Remove if condition when migration is done and moved to "plugins" folder + if (licensing.license$) { + licensing.license$.subscribe(license => { + const { state, message } = license.check(PLUGIN.id, PLUGIN.minimumLicenseType); + const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid; + if (hasRequiredLicense) { + this.licenseStatus = { isValid: true }; + } else { + this.licenseStatus = { + isValid: false, + message: + message || + // Ensure that there is a message when license check fails + i18n.translate('xpack.idxMgmt.licenseCheckErrorMessage', { + defaultMessage: 'License check failed', + }), + }; + if (message) { + this.log.info(message); + } + } + }); + } + + return { + addIndexManagementDataEnricher, + }; + } + + start() {} + stop() {} +} diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/index.ts new file mode 100644 index 0000000000000..59a3b5d7f68a9 --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { API_BASE_PATH } from '../../../../common'; + +export const addBasePath = (uri: string): string => API_BASE_PATH + uri; diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/index.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts new file mode 100644 index 0000000000000..bda4d743084b6 --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { schema } from '@kbn/config-schema'; + +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { + indices: string[]; +} + +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); + +export function registerClearCacheRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/clear_cache'), validate: { body: bodySchema } }, + async (context, req, res) => { + const payload = req.body as ReqBody; + const { indices = [] } = payload; + + const params = { + expandWildcards: 'none', + format: 'json', + index: indices, + }; + + await context.core.elasticsearch.adminClient.callAsInternalUser('indices.clearCache', params); + return res.ok(); + } + ); +} diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts new file mode 100644 index 0000000000000..f04173fcc6a2a --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { RouteDependencies } from '../../../types'; + +import { registerClearCacheRoute } from './register_clear_cache_route'; +// import { registerCloseRoute } from './register_close_route'; +// import { registerFlushRoute } from './register_flush_route'; +// import { registerForcemergeRoute } from './register_forcemerge_route'; +import { registerListRoute } from './register_list_route'; +// import { registerOpenRoute } from './register_open_route'; +// import { registerRefreshRoute } from './register_refresh_route'; +// import { registerReloadRoute } from './register_reload_route'; +// import { registerDeleteRoute } from './register_delete_route'; +// import { registerFreezeRoute } from './register_freeze_route'; +// import { registerUnfreezeRoute } from './register_unfreeze_route'; + +export function registerIndicesRoutes(dependencies: RouteDependencies) { + registerClearCacheRoute(dependencies); + // registerCloseRoute(dependencies); + // registerFlushRoute(dependencies); + // registerForcemergeRoute(dependencies); + registerListRoute(dependencies); + // registerOpenRoute(dependencies); + // registerRefreshRoute(dependencies); + // registerReloadRoute(dependencies); + // registerDeleteRoute(dependencies); + // registerFreezeRoute(dependencies); + // registerUnfreezeRoute(dependencies); +} diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts new file mode 100644 index 0000000000000..73b7246a52d8a --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts @@ -0,0 +1,16 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { fetchIndices } from '../../../lib/fetch_indices'; +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +export function registerListRoute({ router }: RouteDependencies) { + router.get({ path: addBasePath('/indices'), validate: false }, async (ctx, req, res) => { + const indices = await fetchIndices(ctx.core.elasticsearch.dataClient.callAsInternalUser); + return res.ok({ body: indices }); + }); +} diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/mapping/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/mapping/index.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/settings/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/settings/index.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_settings_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/settings/register_settings_routes.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/stats/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/stats/index.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/templates/index.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/templates/register_delete_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_template_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/templates/register_template_routes.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts rename to x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts new file mode 100644 index 0000000000000..e1fa726ac5f7b --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts @@ -0,0 +1,21 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { RouteDependencies } from '../types'; + +import { registerIndicesRoutes } from './api/indices'; +// import { registerTemplateRoutes } from './api/templates'; +// import { registerMappingRoute } from './api/mapping'; +// import { registerSettingsRoutes } from './api/settings'; +// import { registerStatsRoute } from './api/stats'; + +export const registerApiRoutes = (dependencies: RouteDependencies) => { + registerIndicesRoutes(dependencies); + // registerTemplateRoutes(dependencies); + // registerSettingsRoutes(dependencies); + // registerStatsRoute(dependencies); + // registerMappingRoute(dependencies); +}; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts new file mode 100644 index 0000000000000..deb573d0d196a --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { IRouter } from 'src/core/server'; +import { LicensingPluginSetup } from '../../../../../plugins/licensing/server'; +import { ElasticsearchPlugin } from '../../../../../../src/legacy/core_plugins/elasticsearch'; + +export interface Dependencies { + licensing: LicensingPluginSetup; + elasticsearch: ElasticsearchPlugin; +} + +export interface RouteDependencies { + router: IRouter; + plugins: { + elasticsearch: ElasticsearchPlugin; + license: { + getStatus: () => LicenseStatus; + }; + }; +} +export interface LicenseStatus { + isValid: boolean; + message?: string; +} diff --git a/x-pack/legacy/plugins/index_management/server/plugin.ts b/x-pack/legacy/plugins/index_management/server/plugin.ts deleted file mode 100644 index cbe19adcd58be..0000000000000 --- a/x-pack/legacy/plugins/index_management/server/plugin.ts +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -import { CoreSetup } from 'src/core/server'; -import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch'; -import { Router } from '../../../server/lib/create_router'; -import { addIndexManagementDataEnricher } from './index_management_data'; -import { registerIndicesRoutes } from './routes/api/indices'; -import { registerTemplateRoutes } from './routes/api/templates'; -import { registerMappingRoute } from './routes/api/mapping'; -import { registerSettingsRoutes } from './routes/api/settings'; -import { registerStatsRoute } from './routes/api/stats'; - -export interface LegacySetup { - router: Router; - plugins: { - elasticsearch: ElasticsearchPlugin; - license: { - registerLicenseChecker: () => void; - }; - }; -} - -export interface IndexMgmtSetup { - addIndexManagementDataEnricher: (enricher: any) => void; -} - -export class IndexMgmtPlugin { - public setup(core: CoreSetup, plugins: {}, __LEGACY: LegacySetup): IndexMgmtSetup { - const serverFacade = { - plugins: { - elasticsearch: __LEGACY.plugins.elasticsearch, - }, - }; - - __LEGACY.plugins.license.registerLicenseChecker(); - - registerIndicesRoutes(__LEGACY.router); - registerTemplateRoutes(__LEGACY.router, serverFacade); - registerSettingsRoutes(__LEGACY.router); - registerStatsRoute(__LEGACY.router); - registerMappingRoute(__LEGACY.router); - - return { - addIndexManagementDataEnricher, - }; - } -} diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts deleted file mode 100644 index 8bd370a3eb3b8..0000000000000 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; - -interface ReqPayload { - indices: string[]; -} - -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const payload = request.payload as ReqPayload; - const { indices = [] } = payload; - - const params = { - expandWildcards: 'none', - format: 'json', - index: indices, - }; - - await callWithRequest('indices.clearCache', params); - return h.response(); -}; - -export function registerClearCacheRoute(router: Router) { - router.post('indices/clear_cache', handler); -} diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_indices_routes.ts deleted file mode 100644 index 977ef689f44b9..0000000000000 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_indices_routes.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -import { Router } from '../../../../../../server/lib/create_router'; - -import { registerClearCacheRoute } from './register_clear_cache_route'; -import { registerCloseRoute } from './register_close_route'; -import { registerFlushRoute } from './register_flush_route'; -import { registerForcemergeRoute } from './register_forcemerge_route'; -import { registerListRoute } from './register_list_route'; -import { registerOpenRoute } from './register_open_route'; -import { registerRefreshRoute } from './register_refresh_route'; -import { registerReloadRoute } from './register_reload_route'; -import { registerDeleteRoute } from './register_delete_route'; -import { registerFreezeRoute } from './register_freeze_route'; -import { registerUnfreezeRoute } from './register_unfreeze_route'; - -export function registerIndicesRoutes(router: Router) { - registerClearCacheRoute(router); - registerCloseRoute(router); - registerFlushRoute(router); - registerForcemergeRoute(router); - registerListRoute(router); - registerOpenRoute(router); - registerRefreshRoute(router); - registerReloadRoute(router); - registerDeleteRoute(router); - registerFreezeRoute(router); - registerUnfreezeRoute(router); -} diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts deleted file mode 100644 index d8b8018a975c4..0000000000000 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; - -import { fetchIndices } from '../../../lib/fetch_indices'; - -const handler: RouterRouteHandler = async (request, callWithRequest) => { - return fetchIndices(callWithRequest); -}; - -export function registerListRoute(router: Router) { - router.get('indices', handler); -} From ee474e814c362b4ed3734183826659f339cc714b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 15:54:48 +0530 Subject: [PATCH 02/30] Add reload() api route --- .../api/indices/register_clear_cache_route.ts | 4 +-- .../api/indices/register_indices_routes.ts | 4 +-- .../routes/api/indices/register_list_route.ts | 2 +- .../api/indices/register_reload_route.ts | 28 +++++++++++++------ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts index bda4d743084b6..cd163b7fbf364 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts @@ -19,7 +19,7 @@ const bodySchema = schema.object({ export function registerClearCacheRoute({ router }: RouteDependencies) { router.post( { path: addBasePath('/indices/clear_cache'), validate: { body: bodySchema } }, - async (context, req, res) => { + async (ctx, req, res) => { const payload = req.body as ReqBody; const { indices = [] } = payload; @@ -29,7 +29,7 @@ export function registerClearCacheRoute({ router }: RouteDependencies) { index: indices, }; - await context.core.elasticsearch.adminClient.callAsInternalUser('indices.clearCache', params); + await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.clearCache', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts index f04173fcc6a2a..8f1d5cda7b1ba 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts @@ -12,7 +12,7 @@ import { registerClearCacheRoute } from './register_clear_cache_route'; import { registerListRoute } from './register_list_route'; // import { registerOpenRoute } from './register_open_route'; // import { registerRefreshRoute } from './register_refresh_route'; -// import { registerReloadRoute } from './register_reload_route'; +import { registerReloadRoute } from './register_reload_route'; // import { registerDeleteRoute } from './register_delete_route'; // import { registerFreezeRoute } from './register_freeze_route'; // import { registerUnfreezeRoute } from './register_unfreeze_route'; @@ -25,7 +25,7 @@ export function registerIndicesRoutes(dependencies: RouteDependencies) { registerListRoute(dependencies); // registerOpenRoute(dependencies); // registerRefreshRoute(dependencies); - // registerReloadRoute(dependencies); + registerReloadRoute(dependencies); // registerDeleteRoute(dependencies); // registerFreezeRoute(dependencies); // registerUnfreezeRoute(dependencies); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts index 73b7246a52d8a..52ee5d0217e1a 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts @@ -10,7 +10,7 @@ import { addBasePath } from '../index'; export function registerListRoute({ router }: RouteDependencies) { router.get({ path: addBasePath('/indices'), validate: false }, async (ctx, req, res) => { - const indices = await fetchIndices(ctx.core.elasticsearch.dataClient.callAsInternalUser); + const indices = await fetchIndices(ctx.core.elasticsearch.dataClient.callAsCurrentUser); return res.ok({ body: indices }); }); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts index 7371cc1c2d9f1..6bc5e933ae4fc 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts @@ -3,19 +3,31 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; import { fetchIndices } from '../../../lib/fetch_indices'; -interface ReqPayload { +interface ReqBody { indexNames: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest) => { - const { indexNames = [] } = request.payload as ReqPayload; - return fetchIndices(callWithRequest, indexNames); -}; +const bodySchema = schema.object({ + indexNames: schema.arrayOf(schema.string()), +}); -export function registerReloadRoute(router: Router) { - router.post('indices/reload', handler); +export function registerReloadRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/reload'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const { indexNames = [] } = req.body as ReqBody; + + const indices = await fetchIndices( + ctx.core.elasticsearch.adminClient.callAsCurrentUser, + indexNames + ); + return res.ok({ body: indices }); + } + ); } From 88192c572ee7ad95625e02c6e6b403a3133add09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 16:05:28 +0530 Subject: [PATCH 03/30] Add indexClose() & indexOpen() api route --- .../api/indices/register_close_route.ts | 38 +++++++++++-------- .../api/indices/register_indices_routes.ts | 8 ++-- .../routes/api/indices/register_open_route.ts | 38 +++++++++++-------- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts index 6e304f1762acc..836f510788b1f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts @@ -3,26 +3,34 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ReqPayload { +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { indices: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const payload = request.payload as ReqPayload; - const { indices = [] } = payload; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); - const params = { - expandWildcards: 'none', - format: 'json', - index: indices, - }; +export function registerCloseRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/close'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const payload = req.body as ReqBody; + const { indices = [] } = payload; - await callWithRequest('indices.close', params); - return h.response(); -}; + const params = { + expandWildcards: 'none', + format: 'json', + index: indices, + }; -export function registerCloseRoute(router: Router) { - router.post('indices/close', handler); + await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.close', params); + return res.ok(); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts index 8f1d5cda7b1ba..49d4f854d280a 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts @@ -6,11 +6,11 @@ import { RouteDependencies } from '../../../types'; import { registerClearCacheRoute } from './register_clear_cache_route'; -// import { registerCloseRoute } from './register_close_route'; +import { registerCloseRoute } from './register_close_route'; // import { registerFlushRoute } from './register_flush_route'; // import { registerForcemergeRoute } from './register_forcemerge_route'; import { registerListRoute } from './register_list_route'; -// import { registerOpenRoute } from './register_open_route'; +import { registerOpenRoute } from './register_open_route'; // import { registerRefreshRoute } from './register_refresh_route'; import { registerReloadRoute } from './register_reload_route'; // import { registerDeleteRoute } from './register_delete_route'; @@ -19,11 +19,11 @@ import { registerReloadRoute } from './register_reload_route'; export function registerIndicesRoutes(dependencies: RouteDependencies) { registerClearCacheRoute(dependencies); - // registerCloseRoute(dependencies); + registerCloseRoute(dependencies); // registerFlushRoute(dependencies); // registerForcemergeRoute(dependencies); registerListRoute(dependencies); - // registerOpenRoute(dependencies); + registerOpenRoute(dependencies); // registerRefreshRoute(dependencies); registerReloadRoute(dependencies); // registerDeleteRoute(dependencies); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts index 50c2540ec0045..edc99f2992073 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts @@ -3,25 +3,33 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ReqPayload { +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { indices: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const payload = request.payload as ReqPayload; - const { indices = [] } = payload; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); - const params = { - expandWildcards: 'none', - format: 'json', - index: indices, - }; - await callWithRequest('indices.open', params); - return h.response(); -}; +export function registerOpenRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/open'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const body = req.body as ReqBody; + const { indices = [] } = body; -export function registerOpenRoute(router: Router) { - router.post('indices/open', handler); + const params = { + expandWildcards: 'none', + format: 'json', + index: indices, + }; + await await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.open', params); + return res.ok(); + } + ); } From 65f1ec2c778dcecd9b07f59c8585b4868b4ba1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 16:15:36 +0530 Subject: [PATCH 04/30] Add indexRefresh() api route --- .../api/indices/register_indices_routes.ts | 4 +- .../api/indices/register_refresh_route.ts | 39 ++++++++++++------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts index 49d4f854d280a..5b3a1c1e25038 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts @@ -11,7 +11,7 @@ import { registerCloseRoute } from './register_close_route'; // import { registerForcemergeRoute } from './register_forcemerge_route'; import { registerListRoute } from './register_list_route'; import { registerOpenRoute } from './register_open_route'; -// import { registerRefreshRoute } from './register_refresh_route'; +import { registerRefreshRoute } from './register_refresh_route'; import { registerReloadRoute } from './register_reload_route'; // import { registerDeleteRoute } from './register_delete_route'; // import { registerFreezeRoute } from './register_freeze_route'; @@ -24,7 +24,7 @@ export function registerIndicesRoutes(dependencies: RouteDependencies) { // registerForcemergeRoute(dependencies); registerListRoute(dependencies); registerOpenRoute(dependencies); - // registerRefreshRoute(dependencies); + registerRefreshRoute(dependencies); registerReloadRoute(dependencies); // registerDeleteRoute(dependencies); // registerFreezeRoute(dependencies); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts index 093075652821b..0d3d21bbb8360 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts @@ -4,25 +4,34 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ReqPayload { +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { indices: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const payload = request.payload as ReqPayload; - const { indices = [] } = payload; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); + +export function registerRefreshRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/refresh'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const body = req.body as ReqBody; + const { indices = [] } = body; - const params = { - expandWildcards: 'none', - format: 'json', - index: indices, - }; - await callWithRequest('indices.refresh', params); - return h.response(); -}; + const params = { + expandWildcards: 'none', + format: 'json', + index: indices, + }; -export function registerRefreshRoute(router: Router) { - router.post('indices/refresh', handler); + await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.refresh', params); + return res.ok(); + } + ); } From bd74ed21d4c94eb2bd3cbcb1d5b587d5a7b9c33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 16:24:02 +0530 Subject: [PATCH 05/30] Add freeze() & unfreeze() api route --- .../api/indices/register_freeze_route.ts | 36 +++++++++++-------- .../api/indices/register_indices_routes.ts | 8 ++--- .../api/indices/register_unfreeze_route.ts | 34 +++++++++++------- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts index 658a904f08fe7..c966749872214 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts @@ -4,25 +4,33 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ReqPayload { +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { indices: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const payload = request.payload as ReqPayload; - const { indices = [] } = payload; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); - const params = { - path: `/${encodeURIComponent(indices.join(','))}/_freeze`, - method: 'POST', - }; +export function registerFreezeRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/freeze'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const body = req.body as ReqBody; + const { indices = [] } = body; - await callWithRequest('transport.request', params); - return h.response(); -}; + const params = { + path: `/${encodeURIComponent(indices.join(','))}/_freeze`, + method: 'POST', + }; -export function registerFreezeRoute(router: Router) { - router.post('indices/freeze', handler); + await await ctx.core.elasticsearch.adminClient.callAsCurrentUser('transport.request', params); + return res.ok(); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts index 5b3a1c1e25038..993c4b19a8ceb 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts @@ -14,8 +14,8 @@ import { registerOpenRoute } from './register_open_route'; import { registerRefreshRoute } from './register_refresh_route'; import { registerReloadRoute } from './register_reload_route'; // import { registerDeleteRoute } from './register_delete_route'; -// import { registerFreezeRoute } from './register_freeze_route'; -// import { registerUnfreezeRoute } from './register_unfreeze_route'; +import { registerFreezeRoute } from './register_freeze_route'; +import { registerUnfreezeRoute } from './register_unfreeze_route'; export function registerIndicesRoutes(dependencies: RouteDependencies) { registerClearCacheRoute(dependencies); @@ -27,6 +27,6 @@ export function registerIndicesRoutes(dependencies: RouteDependencies) { registerRefreshRoute(dependencies); registerReloadRoute(dependencies); // registerDeleteRoute(dependencies); - // registerFreezeRoute(dependencies); - // registerUnfreezeRoute(dependencies); + registerFreezeRoute(dependencies); + registerUnfreezeRoute(dependencies); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts index 0db882c5171e8..a1322b9b004a0 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts @@ -4,23 +4,31 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ReqPayload { +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { indices: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const { indices = [] } = request.payload as ReqPayload; - const params = { - path: `/${encodeURIComponent(indices.join(','))}/_unfreeze`, - method: 'POST', - }; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); - await callWithRequest('transport.request', params); - return h.response(); -}; +export function registerUnfreezeRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/unfreeze'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const { indices = [] } = req.body as ReqBody; + const params = { + path: `/${encodeURIComponent(indices.join(','))}/_unfreeze`, + method: 'POST', + }; -export function registerUnfreezeRoute(router: Router) { - router.post('indices/unfreeze', handler); + await ctx.core.elasticsearch.adminClient.callAsCurrentUser('transport.request', params); + return res.ok(); + } + ); } From a69e7f8acf43a8d1550fc9502149bd4c6643d200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 16:39:12 +0530 Subject: [PATCH 06/30] Add flush() & forceMerge() api route --- .../api/indices/register_flush_route.ts | 38 +++++++------ .../api/indices/register_forcemerge_route.ts | 54 +++++++++++-------- .../api/indices/register_indices_routes.ts | 8 +-- 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts index 0623d80305719..cccdaa61ca0d1 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts @@ -4,26 +4,34 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ReqPayload { +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { indices: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const payload = request.payload as ReqPayload; - const { indices = [] } = payload; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); - const params = { - expandWildcards: 'none', - format: 'json', - index: indices, - }; +export function registerFlushRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/flush'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const body = req.body as ReqBody; + const { indices = [] } = body; - await callWithRequest('indices.flush', params); - return h.response(); -}; + const params = { + expandWildcards: 'none', + format: 'json', + index: indices, + }; -export function registerFlushRoute(router: Router) { - router.post('indices/flush', handler); + await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.flush', params); + return res.ok(); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts index c0a0ae48c34b8..ca43b3f1ad591 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts @@ -4,34 +4,42 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ForceMergeReqPayload { - maxNumSegments: number; - indices: string[]; -} +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; -interface Params { - expandWildcards: string; - index: ForceMergeReqPayload['indices']; - max_num_segments?: ForceMergeReqPayload['maxNumSegments']; +interface ReqBody { + indices: string[]; + maxNumSegments: number; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const { maxNumSegments, indices = [] } = request.payload as ForceMergeReqPayload; - const params: Params = { - expandWildcards: 'none', - index: indices, - }; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), + maxNumSegments: schema.number(), +}); - if (maxNumSegments) { - params.max_num_segments = maxNumSegments; - } +export function registerForcemergeRoute({ router }: RouteDependencies) { + router.post( + { + path: addBasePath('/indices/forcemerge'), + validate: { + body: bodySchema, + }, + }, + async (ctx, req, res) => { + const { maxNumSegments, indices = [] } = req.body as ReqBody; + const params = { + expandWildcards: 'none', + index: indices, + }; - await callWithRequest('indices.forcemerge', params); - return h.response(); -}; + if (maxNumSegments) { + (params as any).max_num_segments = maxNumSegments; + } -export function registerForcemergeRoute(router: Router) { - router.post('indices/forcemerge', handler); + await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.forcemerge', params); + return res.ok(); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts index 993c4b19a8ceb..b9a99e55e9c37 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts @@ -7,8 +7,8 @@ import { RouteDependencies } from '../../../types'; import { registerClearCacheRoute } from './register_clear_cache_route'; import { registerCloseRoute } from './register_close_route'; -// import { registerFlushRoute } from './register_flush_route'; -// import { registerForcemergeRoute } from './register_forcemerge_route'; +import { registerFlushRoute } from './register_flush_route'; +import { registerForcemergeRoute } from './register_forcemerge_route'; import { registerListRoute } from './register_list_route'; import { registerOpenRoute } from './register_open_route'; import { registerRefreshRoute } from './register_refresh_route'; @@ -20,8 +20,8 @@ import { registerUnfreezeRoute } from './register_unfreeze_route'; export function registerIndicesRoutes(dependencies: RouteDependencies) { registerClearCacheRoute(dependencies); registerCloseRoute(dependencies); - // registerFlushRoute(dependencies); - // registerForcemergeRoute(dependencies); + registerFlushRoute(dependencies); + registerForcemergeRoute(dependencies); registerListRoute(dependencies); registerOpenRoute(dependencies); registerRefreshRoute(dependencies); From ba2c389fd0ddb8d049622685e66d0961017ab676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 16:43:52 +0530 Subject: [PATCH 07/30] Add deleteIndex() api route --- .../api/indices/register_delete_route.ts | 39 ++++++++++++------- .../api/indices/register_indices_routes.ts | 4 +- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts index 0d2268eca179d..429ec1ee65186 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts @@ -4,25 +4,34 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -interface ReqPayload { +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +interface ReqBody { indices: string[]; } -const handler: RouterRouteHandler = async (request, callWithRequest, h) => { - const payload = request.payload as ReqPayload; - const { indices = [] } = payload; +const bodySchema = schema.object({ + indices: schema.arrayOf(schema.string()), +}); + +export function registerDeleteRoute({ router }: RouteDependencies) { + router.post( + { path: addBasePath('/indices/delete'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const body = req.body as ReqBody; + const { indices = [] } = body; - const params = { - expandWildcards: 'none', - format: 'json', - index: indices, - }; - await callWithRequest('indices.delete', params); - return h.response(); -}; + const params = { + expandWildcards: 'none', + format: 'json', + index: indices, + }; -export function registerDeleteRoute(router: Router) { - router.post('indices/delete', handler); + await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.delete', params); + return res.ok(); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts index b9a99e55e9c37..e1165b5d689a0 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts @@ -13,7 +13,7 @@ import { registerListRoute } from './register_list_route'; import { registerOpenRoute } from './register_open_route'; import { registerRefreshRoute } from './register_refresh_route'; import { registerReloadRoute } from './register_reload_route'; -// import { registerDeleteRoute } from './register_delete_route'; +import { registerDeleteRoute } from './register_delete_route'; import { registerFreezeRoute } from './register_freeze_route'; import { registerUnfreezeRoute } from './register_unfreeze_route'; @@ -26,7 +26,7 @@ export function registerIndicesRoutes(dependencies: RouteDependencies) { registerOpenRoute(dependencies); registerRefreshRoute(dependencies); registerReloadRoute(dependencies); - // registerDeleteRoute(dependencies); + registerDeleteRoute(dependencies); registerFreezeRoute(dependencies); registerUnfreezeRoute(dependencies); } From 55a1c7843f3c7b84e080564c27f2d25494006ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 16:52:42 +0530 Subject: [PATCH 08/30] Add indexMapping() api route --- .../api/mapping/register_mapping_route.ts | 39 ++++++++++++------- .../server/np_ready/routes/index.ts | 4 +- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts index 86600aab76580..b116cbb421f00 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts @@ -3,7 +3,14 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; + +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +const paramsSchema = schema.object({ + indexName: schema.string(), +}); function formatHit(hit: { [key: string]: { mappings: any } }, indexName: string) { const mapping = hit[indexName].mappings; @@ -12,18 +19,22 @@ function formatHit(hit: { [key: string]: { mappings: any } }, indexName: string) }; } -const handler: RouterRouteHandler = async (request, callWithRequest) => { - const { indexName } = request.params; - const params = { - expand_wildcards: 'none', - index: indexName, - }; - - const hit = await callWithRequest('indices.getMapping', params); - const response = formatHit(hit, indexName); - return response; -}; +export function registerMappingRoute({ router }: RouteDependencies) { + router.get( + { path: addBasePath('/mapping/{indexName}'), validate: { params: paramsSchema } }, + async (ctx, req, res) => { + const { indexName } = req.params; + const params = { + expand_wildcards: 'none', + index: indexName, + }; -export function registerMappingRoute(router: Router) { - router.get('mapping/{indexName}', handler); + const hit = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + 'indices.getMapping', + params + ); + const response = formatHit(hit, indexName); + return res.ok({ body: response }); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts index e1fa726ac5f7b..6e673aab305a8 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts @@ -8,7 +8,7 @@ import { RouteDependencies } from '../types'; import { registerIndicesRoutes } from './api/indices'; // import { registerTemplateRoutes } from './api/templates'; -// import { registerMappingRoute } from './api/mapping'; +import { registerMappingRoute } from './api/mapping'; // import { registerSettingsRoutes } from './api/settings'; // import { registerStatsRoute } from './api/stats'; @@ -17,5 +17,5 @@ export const registerApiRoutes = (dependencies: RouteDependencies) => { // registerTemplateRoutes(dependencies); // registerSettingsRoutes(dependencies); // registerStatsRoute(dependencies); - // registerMappingRoute(dependencies); + registerMappingRoute(dependencies); }; From 7e53791e46ddf5c1e112be37e24521faacece959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 3 Feb 2020 17:07:43 +0530 Subject: [PATCH 09/30] Add index settings Api routes --- .../api/settings/register_load_route.ts | 42 +++++++++++------ .../api/settings/register_settings_routes.ts | 8 ++-- .../api/settings/register_update_route.ts | 46 +++++++++++++------ .../server/np_ready/routes/index.ts | 4 +- 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts index 70b96c3912e72..156e51b90a404 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts @@ -3,7 +3,14 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; + +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +const paramsSchema = schema.object({ + indexName: schema.string(), +}); // response comes back as { [indexName]: { ... }} // so plucking out the embedded object @@ -12,19 +19,24 @@ function formatHit(hit: { [key: string]: {} }) { return hit[key]; } -const handler: RouterRouteHandler = async (request, callWithRequest) => { - const { indexName } = request.params; - const params = { - expandWildcards: 'none', - flatSettings: false, - local: false, - includeDefaults: true, - index: indexName, - }; +export function registerLoadRoute({ router }: RouteDependencies) { + router.get( + { path: addBasePath('/settings/{indexName}'), validate: { params: paramsSchema } }, + async (ctx, req, res) => { + const { indexName } = req.params; + const params = { + expandWildcards: 'none', + flatSettings: false, + local: false, + includeDefaults: true, + index: indexName, + }; - const hit = await callWithRequest('indices.getSettings', params); - return formatHit(hit); -}; -export function registerLoadRoute(router: Router) { - router.get('settings/{indexName}', handler); + const hit = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + 'indices.getSettings', + params + ); + return res.ok({ body: formatHit(hit) }); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts index 2fe1786f266bd..501566f8b62d8 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts @@ -3,12 +3,12 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router } from '../../../../../../server/lib/create_router'; +import { RouteDependencies } from '../../../types'; import { registerLoadRoute } from './register_load_route'; import { registerUpdateRoute } from './register_update_route'; -export function registerSettingsRoutes(router: Router) { - registerLoadRoute(router); - registerUpdateRoute(router); +export function registerSettingsRoutes(dependencies: RouteDependencies) { + registerLoadRoute(dependencies); + registerUpdateRoute(dependencies); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts index 4d28b5b4ac3bf..c4f184e481e1d 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts @@ -3,20 +3,38 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; -const handler: RouterRouteHandler = async (request, callWithRequest) => { - const { indexName } = request.params; - const params = { - ignoreUnavailable: true, - allowNoIndices: false, - expandWildcards: 'none', - index: indexName, - body: request.payload, - }; +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; - return await callWithRequest('indices.putSettings', params); -}; -export function registerUpdateRoute(router: Router) { - router.put('settings/{indexName}', handler); +const bodySchema = schema.any(); + +const paramsSchema = schema.object({ + indexName: schema.string(), +}); + +export function registerUpdateRoute({ router }: RouteDependencies) { + router.put( + { + path: addBasePath('/settings/{indexName}'), + validate: { body: bodySchema, params: paramsSchema }, + }, + async (ctx, req, res) => { + const { indexName } = req.params; + const params = { + ignoreUnavailable: true, + allowNoIndices: false, + expandWildcards: 'none', + index: indexName, + body: req.body, + }; + + const response = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + 'indices.putSettings', + params + ); + return res.ok({ body: response }); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts index 6e673aab305a8..5440748de0810 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts @@ -9,13 +9,13 @@ import { RouteDependencies } from '../types'; import { registerIndicesRoutes } from './api/indices'; // import { registerTemplateRoutes } from './api/templates'; import { registerMappingRoute } from './api/mapping'; -// import { registerSettingsRoutes } from './api/settings'; +import { registerSettingsRoutes } from './api/settings'; // import { registerStatsRoute } from './api/stats'; export const registerApiRoutes = (dependencies: RouteDependencies) => { registerIndicesRoutes(dependencies); // registerTemplateRoutes(dependencies); - // registerSettingsRoutes(dependencies); + registerSettingsRoutes(dependencies); // registerStatsRoute(dependencies); registerMappingRoute(dependencies); }; From 09542fee6ff3cd318c0a9b126d40bab1c950aeea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 08:21:46 +0530 Subject: [PATCH 10/30] Add indexStats() api route --- .../routes/api/stats/register_stats_route.ts | 37 ++++++++++++------- .../server/np_ready/routes/index.ts | 4 +- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts index 33d0df53e079b..787fc498459e5 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts @@ -3,7 +3,14 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { schema } from '@kbn/config-schema'; + +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; + +const paramsSchema = schema.object({ + indexName: schema.string(), +}); function formatHit(hit: { _shards: any; indices: { [key: string]: any } }, indexName: string) { const { _shards, indices } = hit; @@ -14,17 +21,21 @@ function formatHit(hit: { _shards: any; indices: { [key: string]: any } }, index }; } -const handler: RouterRouteHandler = async (request, callWithRequest) => { - const { indexName } = request.params; - const params = { - expand_wildcards: 'none', - index: indexName, - }; - const hit = await callWithRequest('indices.stats', params); - const response = formatHit(hit, indexName); +export function registerStatsRoute({ router }: RouteDependencies) { + router.get( + { path: addBasePath('/stats/{indexName}'), validate: { params: paramsSchema } }, + async (ctx, req, res) => { + const { indexName } = req.params; + const params = { + expand_wildcards: 'none', + index: indexName, + }; + const hit = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + 'indices.stats', + params + ); - return response; -}; -export function registerStatsRoute(router: Router) { - router.get('stats/{indexName}', handler); + return res.ok({ body: formatHit(hit, indexName) }); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts index 5440748de0810..4e158fff6cf15 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts @@ -10,12 +10,12 @@ import { registerIndicesRoutes } from './api/indices'; // import { registerTemplateRoutes } from './api/templates'; import { registerMappingRoute } from './api/mapping'; import { registerSettingsRoutes } from './api/settings'; -// import { registerStatsRoute } from './api/stats'; +import { registerStatsRoute } from './api/stats'; export const registerApiRoutes = (dependencies: RouteDependencies) => { registerIndicesRoutes(dependencies); // registerTemplateRoutes(dependencies); registerSettingsRoutes(dependencies); - // registerStatsRoute(dependencies); + registerStatsRoute(dependencies); registerMappingRoute(dependencies); }; From 8425911b95e5bc08209b6f09d21751f6630a2e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 08:40:39 +0530 Subject: [PATCH 11/30] Add get template(s) api routes --- .../np_ready/lib/get_managed_templates.ts | 4 +- .../api/templates/register_get_routes.ts | 60 +++++++++++-------- .../api/templates/register_template_routes.ts | 21 +++---- .../server/np_ready/routes/index.ts | 4 +- 4 files changed, 49 insertions(+), 40 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts b/x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts index ebffe73eb23a4..2fdb21ea4b0d6 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts @@ -7,10 +7,10 @@ // Cloud has its own system for managing templates and we want to make // this clear in the UI when a template is used in a Cloud deployment. export const getManagedTemplatePrefix = async ( - callWithInternalUser: any + callAsCurrentUser: any ): Promise => { try { - const { persistent, transient, defaults } = await callWithInternalUser('cluster.getSettings', { + const { persistent, transient, defaults } = await callAsCurrentUser('cluster.getSettings', { filterPath: '*.*managed_index_templates', flatSettings: true, includeDefaults: true, diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts index b450f75d1cc53..0847adf4283c1 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts @@ -3,37 +3,45 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import { schema } from '@kbn/config-schema'; -import { deserializeTemplate, deserializeTemplateList } from '../../../../common/lib'; -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; +import { deserializeTemplate, deserializeTemplateList } from '../../../../../common/lib'; import { getManagedTemplatePrefix } from '../../../lib/get_managed_templates'; +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; -let callWithInternalUser: any; +export function registerGetAllRoute({ router }: RouteDependencies) { + router.get({ path: addBasePath('/templates'), validate: false }, async (ctx, req, res) => { + const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; + const managedTemplatePrefix = await getManagedTemplatePrefix(callAsCurrentUser); -const allHandler: RouterRouteHandler = async (_req, callWithRequest) => { - const managedTemplatePrefix = await getManagedTemplatePrefix(callWithInternalUser); + const indexTemplatesByName = await callAsCurrentUser('indices.getTemplate'); - const indexTemplatesByName = await callWithRequest('indices.getTemplate'); - - return deserializeTemplateList(indexTemplatesByName, managedTemplatePrefix); -}; - -const oneHandler: RouterRouteHandler = async (req, callWithRequest) => { - const { name } = req.params; - const managedTemplatePrefix = await getManagedTemplatePrefix(callWithInternalUser); - const indexTemplateByName = await callWithRequest('indices.getTemplate', { name }); - - if (indexTemplateByName[name]) { - return deserializeTemplate({ ...indexTemplateByName[name], name }, managedTemplatePrefix); - } -}; - -export function registerGetAllRoute(router: Router, server: any) { - callWithInternalUser = server.plugins.elasticsearch.getCluster('data').callWithInternalUser; - router.get('templates', allHandler); + return res.ok({ body: deserializeTemplateList(indexTemplatesByName, managedTemplatePrefix) }); + }); } -export function registerGetOneRoute(router: Router, server: any) { - callWithInternalUser = server.plugins.elasticsearch.getCluster('data').callWithInternalUser; - router.get('templates/{name}', oneHandler); +const paramsSchema = schema.object({ + name: schema.string(), +}); + +export function registerGetOneRoute({ router }: RouteDependencies) { + router.get( + { path: addBasePath('/templates/{name}'), validate: { params: paramsSchema } }, + async (ctx, req, res) => { + const { name } = req.params; + const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; + + const managedTemplatePrefix = await getManagedTemplatePrefix(callAsCurrentUser); + const indexTemplateByName = await callAsCurrentUser('indices.getTemplate', { name }); + + if (indexTemplateByName[name]) { + return res.ok({ + body: deserializeTemplate({ ...indexTemplateByName[name], name }, managedTemplatePrefix), + }); + } + + return res.notFound(); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts index b1dcad3f4c362..47bbc1a9bd86c 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts @@ -4,16 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Router } from '../../../../../../server/lib/create_router'; +import { RouteDependencies } from '../../../types'; + import { registerGetAllRoute, registerGetOneRoute } from './register_get_routes'; -import { registerDeleteRoute } from './register_delete_route'; -import { registerCreateRoute } from './register_create_route'; -import { registerUpdateRoute } from './register_update_route'; +// import { registerDeleteRoute } from './register_delete_route'; +// import { registerCreateRoute } from './register_create_route'; +// import { registerUpdateRoute } from './register_update_route'; -export function registerTemplateRoutes(router: Router, server: any) { - registerGetAllRoute(router, server); - registerGetOneRoute(router, server); - registerDeleteRoute(router); - registerCreateRoute(router); - registerUpdateRoute(router); +export function registerTemplateRoutes(dependencies: RouteDependencies) { + registerGetAllRoute(dependencies); + registerGetOneRoute(dependencies); + // registerDeleteRoute(router); + // registerCreateRoute(router); + // registerUpdateRoute(router); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts index 4e158fff6cf15..07b0ec1612b3c 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts @@ -7,14 +7,14 @@ import { RouteDependencies } from '../types'; import { registerIndicesRoutes } from './api/indices'; -// import { registerTemplateRoutes } from './api/templates'; +import { registerTemplateRoutes } from './api/templates'; import { registerMappingRoute } from './api/mapping'; import { registerSettingsRoutes } from './api/settings'; import { registerStatsRoute } from './api/stats'; export const registerApiRoutes = (dependencies: RouteDependencies) => { registerIndicesRoutes(dependencies); - // registerTemplateRoutes(dependencies); + registerTemplateRoutes(dependencies); registerSettingsRoutes(dependencies); registerStatsRoute(dependencies); registerMappingRoute(dependencies); From c212847e1848693696478d63e09ccf745805483f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 08:48:23 +0530 Subject: [PATCH 12/30] Clean-up server plugin dependencies --- .../legacy/plugins/index_management/index.ts | 7 +- .../server/np_ready/plugin.ts | 69 ++++++------------- .../index_management/server/np_ready/types.ts | 8 --- 3 files changed, 25 insertions(+), 59 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/index.ts b/x-pack/legacy/plugins/index_management/index.ts index c3e4d54975c0b..e5b9174ba0d87 100644 --- a/x-pack/legacy/plugins/index_management/index.ts +++ b/x-pack/legacy/plugins/index_management/index.ts @@ -27,8 +27,11 @@ export function indexManagement(kibana: any) { const coreSetup = server.newPlatform.setup.core; const pluginDependencies: Dependencies = { - elasticsearch: server.plugins.elasticsearch, - licensing: {} as any, + licensing: { + license$: { + subscribe() {}, + }, + } as any, }; const mockLogger = { diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts index 2bc1423d5e9da..398e4f4c13a49 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts @@ -9,22 +9,10 @@ import { CoreSetup, Plugin, Logger, PluginInitializerContext } from 'src/core/se import { LICENSE_CHECK_STATE } from '../../../../../plugins/licensing/common/types'; import { PLUGIN } from '../../common'; import { Dependencies, RouteDependencies, LicenseStatus } from './types'; -// import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch'; -// import { Router } from '../../../../server/lib/create_router'; import { addIndexManagementDataEnricher } from './index_management_data'; import { registerApiRoutes } from './routes'; -// export interface LegacySetup { -// router: Router; -// plugins: { -// elasticsearch: ElasticsearchPlugin; -// license: { -// registerLicenseChecker: () => void; -// }; -// }; -// } - export interface IndexMgmtSetup { addIndexManagementDataEnricher: (enricher: any) => void; } @@ -38,52 +26,35 @@ export class IndexMgmtServerPlugin implements Plugin this.licenseStatus, - }, - }, }; registerApiRoutes(routeDependencies); - // TODO: Remove if condition when migration is done and moved to "plugins" folder - if (licensing.license$) { - licensing.license$.subscribe(license => { - const { state, message } = license.check(PLUGIN.id, PLUGIN.minimumLicenseType); - const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid; - if (hasRequiredLicense) { - this.licenseStatus = { isValid: true }; - } else { - this.licenseStatus = { - isValid: false, - message: - message || - // Ensure that there is a message when license check fails - i18n.translate('xpack.idxMgmt.licenseCheckErrorMessage', { - defaultMessage: 'License check failed', - }), - }; - if (message) { - this.log.info(message); - } + licensing.license$.subscribe(license => { + const { state, message } = license.check(PLUGIN.id, PLUGIN.minimumLicenseType); + const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid; + if (hasRequiredLicense) { + this.licenseStatus = { isValid: true }; + } else { + this.licenseStatus = { + isValid: false, + message: + message || + // Ensure that there is a message when license check fails + i18n.translate('xpack.idxMgmt.licenseCheckErrorMessage', { + defaultMessage: 'License check failed', + }), + }; + if (message) { + this.log.info(message); } - }); - } + } + }); return { addIndexManagementDataEnricher, diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts index deb573d0d196a..11190aa40f608 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts @@ -5,21 +5,13 @@ */ import { IRouter } from 'src/core/server'; import { LicensingPluginSetup } from '../../../../../plugins/licensing/server'; -import { ElasticsearchPlugin } from '../../../../../../src/legacy/core_plugins/elasticsearch'; export interface Dependencies { licensing: LicensingPluginSetup; - elasticsearch: ElasticsearchPlugin; } export interface RouteDependencies { router: IRouter; - plugins: { - elasticsearch: ElasticsearchPlugin; - license: { - getStatus: () => LicenseStatus; - }; - }; } export interface LicenseStatus { isValid: boolean; From 59ac5da877ecad43de7d6e1aa5420cb6fc4652fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 14:29:16 +0530 Subject: [PATCH 13/30] Add create & update template api routes --- .../api/templates/register_create_route.ts | 101 +++++++++--------- .../api/templates/register_template_routes.ts | 8 +- .../api/templates/register_update_route.ts | 75 ++++++++----- .../routes/api/templates/validate_schemas.ts | 23 ++++ 4 files changed, 126 insertions(+), 81 deletions(-) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts index e134a97dd029e..2efb73026f586 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts @@ -5,60 +5,63 @@ */ import { i18n } from '@kbn/i18n'; -import { - Router, - RouterRouteHandler, - wrapCustomError, -} from '../../../../../../server/lib/create_router'; -import { Template, TemplateEs } from '../../../../common/types'; -import { serializeTemplate } from '../../../../common/lib'; -const handler: RouterRouteHandler = async (req, callWithRequest) => { - const template = req.payload as Template; - const serializedTemplate = serializeTemplate(template) as TemplateEs; +import { Template, TemplateEs } from '../../../../../common/types'; +import { serializeTemplate } from '../../../../../common/lib'; +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; +import { templateSchema } from './validate_schemas'; - const { name, order, index_patterns, version, settings, mappings, aliases } = serializedTemplate; +const bodySchema = templateSchema; - const conflictError = wrapCustomError( - new Error( - i18n.translate('xpack.idxMgmt.createRoute.duplicateTemplateIdErrorMessage', { - defaultMessage: "There is already a template with name '{name}'.", - values: { - name, - }, - }) - ), - 409 - ); +export function registerCreateRoute({ router }: RouteDependencies) { + router.put( + { path: addBasePath('/templates'), validate: { body: bodySchema } }, + async (ctx, req, res) => { + const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; + const template = req.body as Template; + const serializedTemplate = serializeTemplate(template) as TemplateEs; - // Check that template with the same name doesn't already exist - try { - const templateExists = await callWithRequest('indices.existsTemplate', { name }); + const { + name, + order, + index_patterns, + version, + settings, + mappings, + aliases, + } = serializedTemplate; - if (templateExists) { - throw conflictError; - } - } catch (e) { - // Rethrow conflict error but silently swallow all others - if (e === conflictError) { - throw e; - } - } + // Check that template with the same name doesn't already exist + const templateExists = await callAsCurrentUser('indices.existsTemplate', { name }); - // Otherwise create new index template - return await callWithRequest('indices.putTemplate', { - name, - order, - body: { - index_patterns, - version, - settings, - mappings, - aliases, - }, - }); -}; + if (templateExists) { + return res.conflict({ + body: new Error( + i18n.translate('xpack.idxMgmt.createRoute.duplicateTemplateIdErrorMessage', { + defaultMessage: "There is already a template with name '{name}'.", + values: { + name, + }, + }) + ), + }); + } -export function registerCreateRoute(router: Router) { - router.put('templates', handler); + // Otherwise create new index template + const response = await callAsCurrentUser('indices.putTemplate', { + name, + order, + body: { + index_patterns, + version, + settings, + mappings, + aliases, + }, + }); + + return res.ok({ body: response }); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts index 47bbc1a9bd86c..6d02a4e254d11 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts @@ -8,13 +8,13 @@ import { RouteDependencies } from '../../../types'; import { registerGetAllRoute, registerGetOneRoute } from './register_get_routes'; // import { registerDeleteRoute } from './register_delete_route'; -// import { registerCreateRoute } from './register_create_route'; -// import { registerUpdateRoute } from './register_update_route'; +import { registerCreateRoute } from './register_create_route'; +import { registerUpdateRoute } from './register_update_route'; export function registerTemplateRoutes(dependencies: RouteDependencies) { registerGetAllRoute(dependencies); registerGetOneRoute(dependencies); // registerDeleteRoute(router); - // registerCreateRoute(router); - // registerUpdateRoute(router); + registerCreateRoute(dependencies); + registerUpdateRoute(dependencies); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts index 15590e2acbe71..e33774c668e4a 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts @@ -3,35 +3,54 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import { schema } from '@kbn/config-schema'; -import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router'; -import { Template, TemplateEs } from '../../../../common/types'; -import { serializeTemplate } from '../../../../common/lib'; - -const handler: RouterRouteHandler = async (req, callWithRequest) => { - const { name } = req.params; - const template = req.payload as Template; - const serializedTemplate = serializeTemplate(template) as TemplateEs; - - const { order, index_patterns, version, settings, mappings, aliases } = serializedTemplate; - - // Verify the template exists (ES will throw 404 if not) - await callWithRequest('indices.existsTemplate', { name }); - - // Next, update index template - return await callWithRequest('indices.putTemplate', { - name, - order, - body: { - index_patterns, - version, - settings, - mappings, - aliases, +import { Template, TemplateEs } from '../../../../../common/types'; +import { serializeTemplate } from '../../../../../common/lib'; +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; +import { templateSchema } from './validate_schemas'; + +const bodySchema = templateSchema; +const paramsSchema = schema.object({ + name: schema.string(), +}); + +export function registerUpdateRoute({ router }: RouteDependencies) { + router.put( + { + path: addBasePath('/templates/{name}'), + validate: { body: bodySchema, params: paramsSchema }, }, - }); -}; + async (ctx, req, res) => { + const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; + const { name } = req.params; + const template = req.body as Template; + const serializedTemplate = serializeTemplate(template) as TemplateEs; + + const { order, index_patterns, version, settings, mappings, aliases } = serializedTemplate; + + // Verify the template exists (ES will throw 404 if not) + const doesExist = await callAsCurrentUser('indices.existsTemplate', { name }); + + if (!doesExist) { + return res.notFound(); + } + + // Next, update index template + const response = await callAsCurrentUser('indices.putTemplate', { + name, + order, + body: { + index_patterns, + version, + settings, + mappings, + aliases, + }, + }); -export function registerUpdateRoute(router: Router) { - router.put('templates/{name}', handler); + return res.ok({ body: response }); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts new file mode 100644 index 0000000000000..5d9ab368ee0d3 --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts @@ -0,0 +1,23 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; + +export const templateSchema = schema.object({ + name: schema.string(), + indexPatterns: schema.arrayOf(schema.string()), + version: schema.maybe(schema.number()), + order: schema.maybe(schema.number()), + settings: schema.maybe(schema.object({}, { allowUnknowns: true })), + aliases: schema.maybe(schema.object({}, { allowUnknowns: true })), + mappings: schema.maybe(schema.object({}, { allowUnknowns: true })), + ilmPolicy: schema.maybe( + schema.object({ + name: schema.maybe(schema.string()), + }) + ), + isManaged: schema.maybe(schema.boolean()), +}); From d5e61777ff5b0d9de58c1afa904a490aa26a8e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 14:30:45 +0530 Subject: [PATCH 14/30] Use data client instead of admin client --- .../np_ready/routes/api/indices/register_clear_cache_route.ts | 2 +- .../server/np_ready/routes/api/indices/register_close_route.ts | 2 +- .../server/np_ready/routes/api/indices/register_delete_route.ts | 2 +- .../server/np_ready/routes/api/indices/register_flush_route.ts | 2 +- .../np_ready/routes/api/indices/register_forcemerge_route.ts | 2 +- .../server/np_ready/routes/api/indices/register_freeze_route.ts | 2 +- .../server/np_ready/routes/api/indices/register_open_route.ts | 2 +- .../np_ready/routes/api/indices/register_refresh_route.ts | 2 +- .../server/np_ready/routes/api/indices/register_reload_route.ts | 2 +- .../np_ready/routes/api/indices/register_unfreeze_route.ts | 2 +- .../np_ready/routes/api/mapping/register_mapping_route.ts | 2 +- .../server/np_ready/routes/api/settings/register_load_route.ts | 2 +- .../np_ready/routes/api/settings/register_update_route.ts | 2 +- .../server/np_ready/routes/api/stats/register_stats_route.ts | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts index cd163b7fbf364..0a0fc97aa092f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts @@ -29,7 +29,7 @@ export function registerClearCacheRoute({ router }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.clearCache', params); + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.clearCache', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts index 836f510788b1f..21139a355a988 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts @@ -29,7 +29,7 @@ export function registerCloseRoute({ router }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.close', params); + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.close', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts index 429ec1ee65186..dd6b6fd5c37c7 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts @@ -30,7 +30,7 @@ export function registerDeleteRoute({ router }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.delete', params); + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.delete', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts index cccdaa61ca0d1..642b16b23cb61 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts @@ -30,7 +30,7 @@ export function registerFlushRoute({ router }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.flush', params); + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.flush', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts index ca43b3f1ad591..01e6030bc6fbb 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts @@ -38,7 +38,7 @@ export function registerForcemergeRoute({ router }: RouteDependencies) { (params as any).max_num_segments = maxNumSegments; } - await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.forcemerge', params); + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.forcemerge', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts index c966749872214..55bfc8dec3408 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts @@ -29,7 +29,7 @@ export function registerFreezeRoute({ router }: RouteDependencies) { method: 'POST', }; - await await ctx.core.elasticsearch.adminClient.callAsCurrentUser('transport.request', params); + await await ctx.core.elasticsearch.dataClient.callAsCurrentUser('transport.request', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts index edc99f2992073..2e82fb114d9b1 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts @@ -28,7 +28,7 @@ export function registerOpenRoute({ router }: RouteDependencies) { format: 'json', index: indices, }; - await await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.open', params); + await await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.open', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts index 0d3d21bbb8360..9a82c22d59596 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts @@ -30,7 +30,7 @@ export function registerRefreshRoute({ router }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.adminClient.callAsCurrentUser('indices.refresh', params); + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.refresh', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts index 6bc5e933ae4fc..22b1b459232b5 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts @@ -24,7 +24,7 @@ export function registerReloadRoute({ router }: RouteDependencies) { const { indexNames = [] } = req.body as ReqBody; const indices = await fetchIndices( - ctx.core.elasticsearch.adminClient.callAsCurrentUser, + ctx.core.elasticsearch.dataClient.callAsCurrentUser, indexNames ); return res.ok({ body: indices }); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts index a1322b9b004a0..6bb80414bec81 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts @@ -27,7 +27,7 @@ export function registerUnfreezeRoute({ router }: RouteDependencies) { method: 'POST', }; - await ctx.core.elasticsearch.adminClient.callAsCurrentUser('transport.request', params); + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('transport.request', params); return res.ok(); } ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts index b116cbb421f00..ada46e0df1b88 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts @@ -29,7 +29,7 @@ export function registerMappingRoute({ router }: RouteDependencies) { index: indexName, }; - const hit = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( 'indices.getMapping', params ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts index 156e51b90a404..d1f69446ca313 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts @@ -32,7 +32,7 @@ export function registerLoadRoute({ router }: RouteDependencies) { index: indexName, }; - const hit = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( 'indices.getSettings', params ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts index c4f184e481e1d..44bae5a0fd569 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts @@ -30,7 +30,7 @@ export function registerUpdateRoute({ router }: RouteDependencies) { body: req.body, }; - const response = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + const response = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( 'indices.putSettings', params ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts index 787fc498459e5..3f9f1a24d3f2e 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts @@ -30,7 +30,7 @@ export function registerStatsRoute({ router }: RouteDependencies) { expand_wildcards: 'none', index: indexName, }; - const hit = await ctx.core.elasticsearch.adminClient.callAsCurrentUser( + const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( 'indices.stats', params ); From 3f7ffe55c22287a067e1afc33e470ce9374ccf9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 14:49:07 +0530 Subject: [PATCH 15/30] Add delete template api routes and bring in wrapEsError helper --- .../api/templates/register_delete_route.ts | 68 +++++++++++-------- .../api/templates/register_template_routes.ts | 4 +- .../server/np_ready/routes/helpers.ts | 58 ++++++++++++++++ 3 files changed, 98 insertions(+), 32 deletions(-) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/routes/helpers.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts index b48354127b9f9..5d768f684e3e7 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts @@ -4,38 +4,46 @@ * you may not use this file except in compliance with the Elastic License. */ -import { - Router, - RouterRouteHandler, - wrapEsError, -} from '../../../../../../server/lib/create_router'; -import { Template } from '../../../../common/types'; +import { schema } from '@kbn/config-schema'; -const handler: RouterRouteHandler = async (req, callWithRequest) => { - const { names } = req.params; - const templateNames = names.split(','); - const response: { templatesDeleted: Array; errors: any[] } = { - templatesDeleted: [], - errors: [], - }; +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; +import { wrapEsError } from '../../helpers'; - await Promise.all( - templateNames.map(async name => { - try { - await callWithRequest('indices.deleteTemplate', { name }); - return response.templatesDeleted.push(name); - } catch (e) { - return response.errors.push({ - name, - error: wrapEsError(e), - }); - } - }) - ); +import { Template } from '../../../../../common/types'; + +const paramsSchema = schema.object({ + names: schema.string(), +}); - return response; -}; +export function registerDeleteRoute({ router }: RouteDependencies) { + router.delete( + { path: addBasePath('/templates/{names}'), validate: { params: paramsSchema } }, + async (ctx, req, res) => { + const { names } = req.params; + const templateNames = names.split(','); + const response: { templatesDeleted: Array; errors: any[] } = { + templatesDeleted: [], + errors: [], + }; -export function registerDeleteRoute(router: Router) { - router.delete('templates/{names}', handler); + await Promise.all( + templateNames.map(async name => { + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.deleteTemplate', { + name, + }); + return response.templatesDeleted.push(name); + } catch (e) { + return response.errors.push({ + name, + error: wrapEsError(e), + }); + } + }) + ); + + return res.ok({ body: response }); + } + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts index 6d02a4e254d11..2b657346a2f82 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts @@ -7,14 +7,14 @@ import { RouteDependencies } from '../../../types'; import { registerGetAllRoute, registerGetOneRoute } from './register_get_routes'; -// import { registerDeleteRoute } from './register_delete_route'; +import { registerDeleteRoute } from './register_delete_route'; import { registerCreateRoute } from './register_create_route'; import { registerUpdateRoute } from './register_update_route'; export function registerTemplateRoutes(dependencies: RouteDependencies) { registerGetAllRoute(dependencies); registerGetOneRoute(dependencies); - // registerDeleteRoute(router); + registerDeleteRoute(dependencies); registerCreateRoute(dependencies); registerUpdateRoute(dependencies); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/helpers.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/helpers.ts new file mode 100644 index 0000000000000..6cd4b0dc80e22 --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/helpers.ts @@ -0,0 +1,58 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +const extractCausedByChain = (causedBy: any = {}, accumulator: any[] = []): any => { + const { reason, caused_by } = causedBy; // eslint-disable-line @typescript-eslint/camelcase + + if (reason) { + accumulator.push(reason); + } + + // eslint-disable-next-line @typescript-eslint/camelcase + if (caused_by) { + return extractCausedByChain(caused_by, accumulator); + } + + return accumulator; +}; + +/** + * Wraps an error thrown by the ES JS client into a Boom error response and returns it + * + * @param err Object Error thrown by ES JS client + * @param statusCodeToMessageMap Object Optional map of HTTP status codes => error messages + * @return Object Boom error response + */ +export const wrapEsError = (err: any, statusCodeToMessageMap: any = {}) => { + const { statusCode, response } = err; + + const { + error: { + root_cause = [], // eslint-disable-line @typescript-eslint/camelcase + caused_by = {}, // eslint-disable-line @typescript-eslint/camelcase + } = {}, + } = JSON.parse(response); + + // If no custom message if specified for the error's status code, just + // wrap the error as a Boom error response, include the additional information from ES, and return it + if (!statusCodeToMessageMap[statusCode]) { + // const boomError = Boom.boomify(err, { statusCode }); + const error: any = { statusCode }; + + // The caused_by chain has the most information so use that if it's available. If not then + // settle for the root_cause. + const causedByChain = extractCausedByChain(caused_by); + const defaultCause = root_cause.length ? extractCausedByChain(root_cause[0]) : undefined; + + error.cause = causedByChain.length ? causedByChain : defaultCause; + return error; + } + + // Otherwise, use the custom message to create a Boom error response and + // return it + const message = statusCodeToMessageMap[statusCode]; + return { message, statusCode }; +}; From d4616bb1572e30063c8019afaf6982ef2737fa92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 16:33:27 +0530 Subject: [PATCH 16/30] Use class to declare ApiRoutes --- .../server/np_ready/plugin.ts | 14 ++++++++++---- .../server/np_ready/routes/index.ts | 19 ++++++++++++------- .../index_management/server/np_ready/types.ts | 5 +++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts index 398e4f4c13a49..a387a0d06960c 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts @@ -11,15 +11,16 @@ import { PLUGIN } from '../../common'; import { Dependencies, RouteDependencies, LicenseStatus } from './types'; import { addIndexManagementDataEnricher } from './index_management_data'; -import { registerApiRoutes } from './routes'; +import { ApiRoutes } from './routes'; export interface IndexMgmtSetup { addIndexManagementDataEnricher: (enricher: any) => void; } export class IndexMgmtServerPlugin implements Plugin { - licenseStatus: LicenseStatus; - log: Logger; + private readonly apiRoutes = new ApiRoutes(); + private licenseStatus: LicenseStatus; + private log: Logger; constructor({ logger }: PluginInitializerContext) { this.log = logger.get(); @@ -31,9 +32,14 @@ export class IndexMgmtServerPlugin implements Plugin this.licenseStatus, + }, + }, }; - registerApiRoutes(routeDependencies); + this.apiRoutes.setup(routeDependencies); licensing.license$.subscribe(license => { const { state, message } = license.check(PLUGIN.id, PLUGIN.minimumLicenseType); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts index 07b0ec1612b3c..870cfa36ecc6a 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts @@ -12,10 +12,15 @@ import { registerMappingRoute } from './api/mapping'; import { registerSettingsRoutes } from './api/settings'; import { registerStatsRoute } from './api/stats'; -export const registerApiRoutes = (dependencies: RouteDependencies) => { - registerIndicesRoutes(dependencies); - registerTemplateRoutes(dependencies); - registerSettingsRoutes(dependencies); - registerStatsRoute(dependencies); - registerMappingRoute(dependencies); -}; +export class ApiRoutes { + setup(dependencies: RouteDependencies) { + registerIndicesRoutes(dependencies); + registerTemplateRoutes(dependencies); + registerSettingsRoutes(dependencies); + registerStatsRoute(dependencies); + registerMappingRoute(dependencies); + } + + start() {} + stop() {} +} diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts index 11190aa40f608..2aff991720ca4 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts @@ -12,6 +12,11 @@ export interface Dependencies { export interface RouteDependencies { router: IRouter; + plugins: { + license: { + getStatus: () => LicenseStatus; + }; + }; } export interface LicenseStatus { isValid: boolean; From 38e038c2bb23e7a6646c8b158ad799e77fdfb0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 16:50:28 +0530 Subject: [PATCH 17/30] Create License service to check license on routes --- .../server/np_ready/plugin.ts | 44 ++++----------- .../server/np_ready/services/index.ts | 7 +++ .../server/np_ready/services/license.ts | 56 +++++++++++++++++++ .../index_management/server/np_ready/types.ts | 9 +-- 4 files changed, 77 insertions(+), 39 deletions(-) create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts index a387a0d06960c..4c8c020544a2c 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts @@ -3,15 +3,15 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { i18n } from '@kbn/i18n'; + import { CoreSetup, Plugin, Logger, PluginInitializerContext } from 'src/core/server'; -import { LICENSE_CHECK_STATE } from '../../../../../plugins/licensing/common/types'; import { PLUGIN } from '../../common'; -import { Dependencies, RouteDependencies, LicenseStatus } from './types'; +import { Dependencies } from './types'; import { addIndexManagementDataEnricher } from './index_management_data'; import { ApiRoutes } from './routes'; +import { License } from './services'; export interface IndexMgmtSetup { addIndexManagementDataEnricher: (enricher: any) => void; @@ -19,47 +19,27 @@ export interface IndexMgmtSetup { export class IndexMgmtServerPlugin implements Plugin { private readonly apiRoutes = new ApiRoutes(); - private licenseStatus: LicenseStatus; + private readonly license: License; private log: Logger; constructor({ logger }: PluginInitializerContext) { this.log = logger.get(); - this.licenseStatus = { isValid: false }; + this.license = new License(this.log); } setup({ http }: CoreSetup, { licensing }: Dependencies): IndexMgmtSetup { const router = http.createRouter(); - const routeDependencies: RouteDependencies = { + this.license.setup( + { pluginId: PLUGIN.id, minimumLicenseType: PLUGIN.minimumLicenseType }, + { licensing } + ); + + this.apiRoutes.setup({ router, plugins: { - license: { - getStatus: () => this.licenseStatus, - }, + license: this.license, }, - }; - - this.apiRoutes.setup(routeDependencies); - - licensing.license$.subscribe(license => { - const { state, message } = license.check(PLUGIN.id, PLUGIN.minimumLicenseType); - const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid; - if (hasRequiredLicense) { - this.licenseStatus = { isValid: true }; - } else { - this.licenseStatus = { - isValid: false, - message: - message || - // Ensure that there is a message when license check fails - i18n.translate('xpack.idxMgmt.licenseCheckErrorMessage', { - defaultMessage: 'License check failed', - }), - }; - if (message) { - this.log.info(message); - } - } }); return { diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts new file mode 100644 index 0000000000000..b7a45e59549eb --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts @@ -0,0 +1,7 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export { License } from './license'; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts b/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts new file mode 100644 index 0000000000000..0c0f8cc522eec --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts @@ -0,0 +1,56 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { i18n } from '@kbn/i18n'; +import { Logger } from 'src/core/server'; + +import { LicensingPluginSetup } from '../../../../../../plugins/licensing/server'; +import { LicenseType } from '../../../../../../plugins/licensing/common/types'; +import { LICENSE_CHECK_STATE } from '../../../../../../plugins/licensing/common/types'; + +export interface LicenseStatus { + isValid: boolean; + message?: string; +} + +export class License { + private licenseStatus: LicenseStatus; + private log: Logger; + + constructor(logger: Logger) { + this.log = logger.get(); + this.licenseStatus = { isValid: false }; + } + + setup( + { pluginId, minimumLicenseType }: { pluginId: string; minimumLicenseType: LicenseType }, + { licensing }: { licensing: LicensingPluginSetup } + ) { + licensing.license$.subscribe(license => { + const { state, message } = license.check(pluginId, minimumLicenseType); + const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid; + if (hasRequiredLicense) { + this.licenseStatus = { isValid: true }; + } else { + this.licenseStatus = { + isValid: false, + message: + message || + // Ensure that there is a message when license check fails + i18n.translate('xpack.idxMgmt.licenseCheckErrorMessage', { + defaultMessage: 'License check failed', + }), + }; + if (message) { + this.log.info(message); + } + } + }); + } + + getStatus() { + return this.licenseStatus; + } +} diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts index 2aff991720ca4..e2191c01f8dd4 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts @@ -5,6 +5,7 @@ */ import { IRouter } from 'src/core/server'; import { LicensingPluginSetup } from '../../../../../plugins/licensing/server'; +import { License } from './services/license'; export interface Dependencies { licensing: LicensingPluginSetup; @@ -13,12 +14,6 @@ export interface Dependencies { export interface RouteDependencies { router: IRouter; plugins: { - license: { - getStatus: () => LicenseStatus; - }; + license: License; }; } -export interface LicenseStatus { - isValid: boolean; - message?: string; -} From 12c372398bfaa2d908993021f9ca78a93203ec22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 17:46:21 +0530 Subject: [PATCH 18/30] Add route guard to License service --- .../legacy/plugins/index_management/index.ts | 10 +++- .../server/np_ready/plugin.ts | 17 ++++--- .../routes/api/indices/register_list_route.ts | 13 +++-- .../server/np_ready/services/license.ts | 50 +++++++++++++++---- .../index_management/server/np_ready/types.ts | 4 +- 5 files changed, 69 insertions(+), 25 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/index.ts b/x-pack/legacy/plugins/index_management/index.ts index e5b9174ba0d87..65b537990f1d1 100644 --- a/x-pack/legacy/plugins/index_management/index.ts +++ b/x-pack/legacy/plugins/index_management/index.ts @@ -29,7 +29,15 @@ export function indexManagement(kibana: any) { const pluginDependencies: Dependencies = { licensing: { license$: { - subscribe() {}, + subscribe(handler: any) { + return handler({ + check() { + return { + state: 'VALID', + }; + }, + }); + }, }, } as any, }; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts index 4c8c020544a2c..a86d3d9fe062a 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts @@ -3,7 +3,7 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ - +import { i18n } from '@kbn/i18n'; import { CoreSetup, Plugin, Logger, PluginInitializerContext } from 'src/core/server'; import { PLUGIN } from '../../common'; @@ -18,12 +18,13 @@ export interface IndexMgmtSetup { } export class IndexMgmtServerPlugin implements Plugin { - private readonly apiRoutes = new ApiRoutes(); + private readonly apiRoutes: ApiRoutes; private readonly license: License; private log: Logger; constructor({ logger }: PluginInitializerContext) { this.log = logger.get(); + this.apiRoutes = new ApiRoutes(); this.license = new License(this.log); } @@ -31,15 +32,19 @@ export class IndexMgmtServerPlugin implements Plugin { - const indices = await fetchIndices(ctx.core.elasticsearch.dataClient.callAsCurrentUser); - return res.ok({ body: indices }); - }); +export function registerListRoute({ router, license }: RouteDependencies) { + router.get( + { path: addBasePath('/indices'), validate: false }, + license.guardApiRoute(async (ctx, req, res) => { + const indices = await fetchIndices(ctx.core.elasticsearch.dataClient.callAsCurrentUser); + return res.ok({ body: indices }); + }) + ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts b/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts index 0c0f8cc522eec..c7a3a7c2caec4 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts @@ -3,8 +3,13 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { i18n } from '@kbn/i18n'; import { Logger } from 'src/core/server'; +import { + KibanaRequest, + KibanaResponseFactory, + RequestHandler, + RequestHandlerContext, +} from 'kibana/server'; import { LicensingPluginSetup } from '../../../../../../plugins/licensing/server'; import { LicenseType } from '../../../../../../plugins/licensing/common/types'; @@ -15,33 +20,35 @@ export interface LicenseStatus { message?: string; } +interface SetupSettings { + pluginId: string; + minimumLicenseType: LicenseType; + defaultErrorMessage: string; +} + export class License { private licenseStatus: LicenseStatus; private log: Logger; constructor(logger: Logger) { - this.log = logger.get(); - this.licenseStatus = { isValid: false }; + this.log = logger; + this.licenseStatus = { isValid: false, message: 'Invalid License' }; } setup( - { pluginId, minimumLicenseType }: { pluginId: string; minimumLicenseType: LicenseType }, + { pluginId, minimumLicenseType, defaultErrorMessage }: SetupSettings, { licensing }: { licensing: LicensingPluginSetup } ) { licensing.license$.subscribe(license => { const { state, message } = license.check(pluginId, minimumLicenseType); const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid; + if (hasRequiredLicense) { this.licenseStatus = { isValid: true }; } else { this.licenseStatus = { isValid: false, - message: - message || - // Ensure that there is a message when license check fails - i18n.translate('xpack.idxMgmt.licenseCheckErrorMessage', { - defaultMessage: 'License check failed', - }), + message: message || defaultErrorMessage, }; if (message) { this.log.info(message); @@ -50,6 +57,29 @@ export class License { }); } + guardApiRoute(handler: RequestHandler) { + const license = this; + + return function licenseCheck( + ctx: RequestHandlerContext, + request: KibanaRequest, + response: KibanaResponseFactory + ) { + const licenseStatus = license.getStatus(); + + if (!licenseStatus.isValid) { + return response.customError({ + body: { + message: licenseStatus.message || '', + }, + statusCode: 403, + }); + } + + return handler(ctx, request, response); + }; + } + getStatus() { return this.licenseStatus; } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts index e2191c01f8dd4..fa3783ff89d4b 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts @@ -13,7 +13,5 @@ export interface Dependencies { export interface RouteDependencies { router: IRouter; - plugins: { - license: License; - }; + license: License; } From 81410f15e3ac7c595ef3e8a6c319bc7d49987904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 23:18:46 +0530 Subject: [PATCH 19/30] Guard all api route with license checker --- .../api/indices/register_clear_cache_route.ts | 12 ++++-------- .../routes/api/indices/register_close_route.ts | 12 ++++-------- .../routes/api/indices/register_delete_route.ts | 12 ++++-------- .../routes/api/indices/register_flush_route.ts | 12 ++++-------- .../routes/api/indices/register_forcemerge_route.ts | 13 ++++--------- .../routes/api/indices/register_freeze_route.ts | 12 ++++-------- .../routes/api/indices/register_open_route.ts | 12 ++++-------- .../routes/api/indices/register_refresh_route.ts | 12 ++++-------- .../routes/api/indices/register_reload_route.ts | 12 ++++-------- .../routes/api/indices/register_unfreeze_route.ts | 12 ++++-------- .../routes/api/mapping/register_mapping_route.ts | 8 ++++---- .../routes/api/settings/register_load_route.ts | 8 ++++---- .../routes/api/settings/register_update_route.ts | 8 ++++---- .../routes/api/stats/register_stats_route.ts | 8 ++++---- .../routes/api/templates/register_create_route.ts | 6 +++--- .../routes/api/templates/register_delete_route.ts | 8 ++++---- .../routes/api/templates/register_get_routes.ts | 8 ++++---- .../routes/api/templates/register_update_route.ts | 8 ++++---- 18 files changed, 71 insertions(+), 112 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts index 0a0fc97aa092f..bafdb7350b63f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts @@ -8,19 +8,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerClearCacheRoute({ router }: RouteDependencies) { +export function registerClearCacheRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/clear_cache'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const payload = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const payload = req.body as typeof bodySchema.type; const { indices = [] } = payload; const params = { @@ -31,6 +27,6 @@ export function registerClearCacheRoute({ router }: RouteDependencies) { await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.clearCache', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts index 21139a355a988..1972da004996d 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts @@ -8,19 +8,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerCloseRoute({ router }: RouteDependencies) { +export function registerCloseRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/close'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const payload = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const payload = req.body as typeof bodySchema.type; const { indices = [] } = payload; const params = { @@ -31,6 +27,6 @@ export function registerCloseRoute({ router }: RouteDependencies) { await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.close', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts index dd6b6fd5c37c7..0c2bafe562b78 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts @@ -9,19 +9,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerDeleteRoute({ router }: RouteDependencies) { +export function registerDeleteRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/delete'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const body = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const body = req.body as typeof bodySchema.type; const { indices = [] } = body; const params = { @@ -32,6 +28,6 @@ export function registerDeleteRoute({ router }: RouteDependencies) { await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.delete', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts index 642b16b23cb61..75293d291d041 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts @@ -9,19 +9,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerFlushRoute({ router }: RouteDependencies) { +export function registerFlushRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/flush'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const body = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const body = req.body as typeof bodySchema.type; const { indices = [] } = body; const params = { @@ -32,6 +28,6 @@ export function registerFlushRoute({ router }: RouteDependencies) { await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.flush', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts index 01e6030bc6fbb..f2c53e96a57ac 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts @@ -9,17 +9,12 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; - maxNumSegments: number; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), maxNumSegments: schema.number(), }); -export function registerForcemergeRoute({ router }: RouteDependencies) { +export function registerForcemergeRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/forcemerge'), @@ -27,8 +22,8 @@ export function registerForcemergeRoute({ router }: RouteDependencies) { body: bodySchema, }, }, - async (ctx, req, res) => { - const { maxNumSegments, indices = [] } = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const { maxNumSegments, indices = [] } = req.body as typeof bodySchema.type; const params = { expandWildcards: 'none', index: indices, @@ -40,6 +35,6 @@ export function registerForcemergeRoute({ router }: RouteDependencies) { await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.forcemerge', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts index 55bfc8dec3408..551ed37ab6927 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts @@ -9,19 +9,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerFreezeRoute({ router }: RouteDependencies) { +export function registerFreezeRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/freeze'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const body = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const body = req.body as typeof bodySchema.type; const { indices = [] } = body; const params = { @@ -31,6 +27,6 @@ export function registerFreezeRoute({ router }: RouteDependencies) { await await ctx.core.elasticsearch.dataClient.callAsCurrentUser('transport.request', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts index 2e82fb114d9b1..1f44239d54f33 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts @@ -8,19 +8,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerOpenRoute({ router }: RouteDependencies) { +export function registerOpenRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/open'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const body = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const body = req.body as typeof bodySchema.type; const { indices = [] } = body; const params = { @@ -30,6 +26,6 @@ export function registerOpenRoute({ router }: RouteDependencies) { }; await await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.open', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts index 9a82c22d59596..b551cccb7741e 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts @@ -9,19 +9,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerRefreshRoute({ router }: RouteDependencies) { +export function registerRefreshRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/refresh'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const body = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const body = req.body as typeof bodySchema.type; const { indices = [] } = body; const params = { @@ -32,6 +28,6 @@ export function registerRefreshRoute({ router }: RouteDependencies) { await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.refresh', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts index 22b1b459232b5..1007ad16673d0 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts @@ -9,25 +9,21 @@ import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; import { fetchIndices } from '../../../lib/fetch_indices'; -interface ReqBody { - indexNames: string[]; -} - const bodySchema = schema.object({ indexNames: schema.arrayOf(schema.string()), }); -export function registerReloadRoute({ router }: RouteDependencies) { +export function registerReloadRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/reload'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const { indexNames = [] } = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const { indexNames = [] } = req.body as typeof bodySchema.type; const indices = await fetchIndices( ctx.core.elasticsearch.dataClient.callAsCurrentUser, indexNames ); return res.ok({ body: indices }); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts index 6bb80414bec81..262901555928f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts @@ -9,19 +9,15 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -interface ReqBody { - indices: string[]; -} - const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerUnfreezeRoute({ router }: RouteDependencies) { +export function registerUnfreezeRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/indices/unfreeze'), validate: { body: bodySchema } }, - async (ctx, req, res) => { - const { indices = [] } = req.body as ReqBody; + license.guardApiRoute(async (ctx, req, res) => { + const { indices = [] } = req.body as typeof bodySchema.type; const params = { path: `/${encodeURIComponent(indices.join(','))}/_unfreeze`, method: 'POST', @@ -29,6 +25,6 @@ export function registerUnfreezeRoute({ router }: RouteDependencies) { await ctx.core.elasticsearch.dataClient.callAsCurrentUser('transport.request', params); return res.ok(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts index ada46e0df1b88..f8661d9dd899d 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts @@ -19,11 +19,11 @@ function formatHit(hit: { [key: string]: { mappings: any } }, indexName: string) }; } -export function registerMappingRoute({ router }: RouteDependencies) { +export function registerMappingRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/mapping/{indexName}'), validate: { params: paramsSchema } }, - async (ctx, req, res) => { - const { indexName } = req.params; + license.guardApiRoute(async (ctx, req, res) => { + const { indexName } = req.params as typeof paramsSchema.type; const params = { expand_wildcards: 'none', index: indexName, @@ -35,6 +35,6 @@ export function registerMappingRoute({ router }: RouteDependencies) { ); const response = formatHit(hit, indexName); return res.ok({ body: response }); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts index d1f69446ca313..951dc469c923f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts @@ -19,11 +19,11 @@ function formatHit(hit: { [key: string]: {} }) { return hit[key]; } -export function registerLoadRoute({ router }: RouteDependencies) { +export function registerLoadRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/settings/{indexName}'), validate: { params: paramsSchema } }, - async (ctx, req, res) => { - const { indexName } = req.params; + license.guardApiRoute(async (ctx, req, res) => { + const { indexName } = req.params as typeof paramsSchema.type; const params = { expandWildcards: 'none', flatSettings: false, @@ -37,6 +37,6 @@ export function registerLoadRoute({ router }: RouteDependencies) { params ); return res.ok({ body: formatHit(hit) }); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts index 44bae5a0fd569..d5b62605d78d1 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts @@ -14,14 +14,14 @@ const paramsSchema = schema.object({ indexName: schema.string(), }); -export function registerUpdateRoute({ router }: RouteDependencies) { +export function registerUpdateRoute({ router, license }: RouteDependencies) { router.put( { path: addBasePath('/settings/{indexName}'), validate: { body: bodySchema, params: paramsSchema }, }, - async (ctx, req, res) => { - const { indexName } = req.params; + license.guardApiRoute(async (ctx, req, res) => { + const { indexName } = req.params as typeof paramsSchema.type; const params = { ignoreUnavailable: true, allowNoIndices: false, @@ -35,6 +35,6 @@ export function registerUpdateRoute({ router }: RouteDependencies) { params ); return res.ok({ body: response }); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts index 3f9f1a24d3f2e..efab387a6263f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts @@ -21,11 +21,11 @@ function formatHit(hit: { _shards: any; indices: { [key: string]: any } }, index }; } -export function registerStatsRoute({ router }: RouteDependencies) { +export function registerStatsRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/stats/{indexName}'), validate: { params: paramsSchema } }, - async (ctx, req, res) => { - const { indexName } = req.params; + license.guardApiRoute(async (ctx, req, res) => { + const { indexName } = req.params as typeof paramsSchema.type; const params = { expand_wildcards: 'none', index: indexName, @@ -36,6 +36,6 @@ export function registerStatsRoute({ router }: RouteDependencies) { ); return res.ok({ body: formatHit(hit, indexName) }); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts index 2efb73026f586..a87fa749b0fc6 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts @@ -14,10 +14,10 @@ import { templateSchema } from './validate_schemas'; const bodySchema = templateSchema; -export function registerCreateRoute({ router }: RouteDependencies) { +export function registerCreateRoute({ router, license }: RouteDependencies) { router.put( { path: addBasePath('/templates'), validate: { body: bodySchema } }, - async (ctx, req, res) => { + license.guardApiRoute(async (ctx, req, res) => { const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; const template = req.body as Template; const serializedTemplate = serializeTemplate(template) as TemplateEs; @@ -62,6 +62,6 @@ export function registerCreateRoute({ router }: RouteDependencies) { }); return res.ok({ body: response }); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts index 5d768f684e3e7..88f7adf2c3504 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts @@ -16,11 +16,11 @@ const paramsSchema = schema.object({ names: schema.string(), }); -export function registerDeleteRoute({ router }: RouteDependencies) { +export function registerDeleteRoute({ router, license }: RouteDependencies) { router.delete( { path: addBasePath('/templates/{names}'), validate: { params: paramsSchema } }, - async (ctx, req, res) => { - const { names } = req.params; + license.guardApiRoute(async (ctx, req, res) => { + const { names } = req.params as typeof paramsSchema.type; const templateNames = names.split(','); const response: { templatesDeleted: Array; errors: any[] } = { templatesDeleted: [], @@ -44,6 +44,6 @@ export function registerDeleteRoute({ router }: RouteDependencies) { ); return res.ok({ body: response }); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts index 0847adf4283c1..13ad39bcede15 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts @@ -25,11 +25,11 @@ const paramsSchema = schema.object({ name: schema.string(), }); -export function registerGetOneRoute({ router }: RouteDependencies) { +export function registerGetOneRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/templates/{name}'), validate: { params: paramsSchema } }, - async (ctx, req, res) => { - const { name } = req.params; + license.guardApiRoute(async (ctx, req, res) => { + const { name } = req.params as typeof paramsSchema.type; const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; const managedTemplatePrefix = await getManagedTemplatePrefix(callAsCurrentUser); @@ -42,6 +42,6 @@ export function registerGetOneRoute({ router }: RouteDependencies) { } return res.notFound(); - } + }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts index e33774c668e4a..05543a08a799f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts @@ -16,15 +16,15 @@ const paramsSchema = schema.object({ name: schema.string(), }); -export function registerUpdateRoute({ router }: RouteDependencies) { +export function registerUpdateRoute({ router, license }: RouteDependencies) { router.put( { path: addBasePath('/templates/{name}'), validate: { body: bodySchema, params: paramsSchema }, }, - async (ctx, req, res) => { + license.guardApiRoute(async (ctx, req, res) => { const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; - const { name } = req.params; + const { name } = req.params as typeof paramsSchema.type; const template = req.body as Template; const serializedTemplate = serializeTemplate(template) as TemplateEs; @@ -51,6 +51,6 @@ export function registerUpdateRoute({ router }: RouteDependencies) { }); return res.ok({ body: response }); - } + }) ); } From 77188c827c2cab6f6db55ff90b9bd14d1b33e4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 4 Feb 2020 23:32:39 +0530 Subject: [PATCH 20/30] Remove constructor from License and move to setup() call --- .../index_management/server/np_ready/plugin.ts | 7 +++++-- .../server/np_ready/services/license.ts | 15 ++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts index a86d3d9fe062a..2a66e96150d85 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts @@ -25,7 +25,7 @@ export class IndexMgmtServerPlugin implements Plugin { const { state, message } = license.check(pluginId, minimumLicenseType); @@ -51,7 +48,7 @@ export class License { message: message || defaultErrorMessage, }; if (message) { - this.log.info(message); + logger.info(message); } } }); From 3aa9d3ba9f8af0aeeff679b1a40b5ff8e1f4a4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Wed, 5 Feb 2020 13:47:24 +0530 Subject: [PATCH 21/30] Move IndexDataEnricher to its own class --- .../cross_cluster_replication/index.js | 11 +++-- .../legacy/plugins/index_management/index.ts | 5 +-- .../server/np_ready/index_management_data.ts | 15 ------- .../server/np_ready/lib/fetch_indices.ts | 40 +++++++------------ .../server/np_ready/plugin.ts | 21 ++++++---- .../routes/api/indices/register_list_route.ts | 7 +++- .../api/indices/register_reload_route.ts | 5 ++- .../server/np_ready/services/index.ts | 2 + .../np_ready/services/index_data_enricher.ts | 40 +++++++++++++++++++ .../index_management/server/np_ready/types.ts | 21 +++++++++- 10 files changed, 106 insertions(+), 61 deletions(-) delete mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/index_management_data.ts create mode 100644 x-pack/legacy/plugins/index_management/server/np_ready/services/index_data_enricher.ts diff --git a/x-pack/legacy/plugins/cross_cluster_replication/index.js b/x-pack/legacy/plugins/cross_cluster_replication/index.js index 953ada1809cf8..1b5f42fc5107e 100644 --- a/x-pack/legacy/plugins/cross_cluster_replication/index.js +++ b/x-pack/legacy/plugins/cross_cluster_replication/index.js @@ -9,7 +9,7 @@ import { PLUGIN } from './common/constants'; import { registerLicenseChecker } from './server/lib/register_license_checker'; import { registerRoutes } from './server/routes/register_routes'; import { ccrDataEnricher } from './cross_cluster_replication_data'; -import { addIndexManagementDataEnricher } from '../index_management/server/np_ready/index_management_data'; + export function crossClusterReplication(kibana) { return new kibana.Plugin({ id: PLUGIN.ID, @@ -49,8 +49,13 @@ export function crossClusterReplication(kibana) { init: function initCcrPlugin(server) { registerLicenseChecker(server); registerRoutes(server); - if (server.config().get('xpack.ccr.ui.enabled')) { - addIndexManagementDataEnricher(ccrDataEnricher); + + if ( + server.config().get('xpack.ccr.ui.enabled') && + server.plugins.index_management && + server.plugins.index_management.addIndexManagementDataEnricher + ) { + server.plugins.index_management.addIndexManagementDataEnricher(ccrDataEnricher); } }, }); diff --git a/x-pack/legacy/plugins/index_management/index.ts b/x-pack/legacy/plugins/index_management/index.ts index 65b537990f1d1..401bb7923cb9d 100644 --- a/x-pack/legacy/plugins/index_management/index.ts +++ b/x-pack/legacy/plugins/index_management/index.ts @@ -51,10 +51,7 @@ export function indexManagement(kibana: any) { const serverPlugin = initServerPlugin({ logger: mockLogger } as any); const indexMgmtSetup = serverPlugin.setup(coreSetup, pluginDependencies); - server.expose( - 'addIndexManagementDataEnricher', - indexMgmtSetup.addIndexManagementDataEnricher - ); + server.expose('addIndexManagementDataEnricher', indexMgmtSetup.indexDataEnricher.add); }, }); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/index_management_data.ts b/x-pack/legacy/plugins/index_management/server/np_ready/index_management_data.ts deleted file mode 100644 index e730761979e1c..0000000000000 --- a/x-pack/legacy/plugins/index_management/server/np_ready/index_management_data.ts +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -const indexManagementDataEnrichers: any[] = []; - -export const addIndexManagementDataEnricher = (enricher: any) => { - indexManagementDataEnrichers.push(enricher); -}; - -export const getIndexManagementDataEnrichers = () => { - return indexManagementDataEnrichers; -}; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts b/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts index 72263fe576a26..d9f01ee060145 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts @@ -3,8 +3,9 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import { IndexDataEnricher } from '../services'; +import { Index, CallAsCurrentUser } from '../types'; import { fetchAliases } from './fetch_aliases'; -import { getIndexManagementDataEnrichers } from '../index_management_data'; interface Hit { health: string; @@ -28,22 +29,7 @@ interface Params { index?: string[]; } -const enrichResponse = async (response: any, callWithRequest: any) => { - let enrichedResponse = response; - const dataEnrichers = getIndexManagementDataEnrichers(); - for (let i = 0; i < dataEnrichers.length; i++) { - const dataEnricher = dataEnrichers[i]; - try { - const dataEnricherResponse = await dataEnricher(enrichedResponse, callWithRequest); - enrichedResponse = dataEnricherResponse; - } catch (e) { - // silently swallow enricher response errors - } - } - return enrichedResponse; -}; - -function formatHits(hits: Hit[], aliases: Aliases) { +function formatHits(hits: Hit[], aliases: Aliases): Index[] { return hits.map((hit: Hit) => { return { health: hit.health, @@ -60,7 +46,7 @@ function formatHits(hits: Hit[], aliases: Aliases) { }); } -async function fetchIndicesCall(callWithRequest: any, indexNames?: string[]) { +async function fetchIndicesCall(callAsCurrentUser: CallAsCurrentUser, indexNames?: string[]) { const params: Params = { format: 'json', h: 'health,status,index,uuid,pri,rep,docs.count,sth,store.size', @@ -70,13 +56,17 @@ async function fetchIndicesCall(callWithRequest: any, indexNames?: string[]) { params.index = indexNames; } - return await callWithRequest('cat.indices', params); + return await callAsCurrentUser('cat.indices', params); } -export const fetchIndices = async (callWithRequest: any, indexNames?: string[]) => { - const aliases = await fetchAliases(callWithRequest); - const hits = await fetchIndicesCall(callWithRequest, indexNames); - let response = formatHits(hits, aliases); - response = await enrichResponse(response, callWithRequest); - return response; +export const fetchIndices = async ( + callAsCurrentUser: CallAsCurrentUser, + indexDataEnricher: IndexDataEnricher, + indexNames?: string[] +) => { + const aliases = await fetchAliases(callAsCurrentUser); + const hits = await fetchIndicesCall(callAsCurrentUser, indexNames); + const indices = formatHits(hits, aliases); + + return await indexDataEnricher.enrichIndices(indices, callAsCurrentUser); }; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts index 2a66e96150d85..5f76d7ce1fa7f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts @@ -8,24 +8,26 @@ import { CoreSetup, Plugin, Logger, PluginInitializerContext } from 'src/core/se import { PLUGIN } from '../../common'; import { Dependencies } from './types'; - -import { addIndexManagementDataEnricher } from './index_management_data'; import { ApiRoutes } from './routes'; -import { License } from './services'; +import { License, IndexDataEnricher } from './services'; export interface IndexMgmtSetup { - addIndexManagementDataEnricher: (enricher: any) => void; + indexDataEnricher: { + add: IndexDataEnricher['add']; + }; } export class IndexMgmtServerPlugin implements Plugin { private readonly apiRoutes: ApiRoutes; private readonly license: License; - private log: Logger; + private logger: Logger; + private indexDataEnricher: IndexDataEnricher; constructor({ logger }: PluginInitializerContext) { - this.log = logger.get(); + this.logger = logger.get(); this.apiRoutes = new ApiRoutes(); this.license = new License(); + this.indexDataEnricher = new IndexDataEnricher(); } setup({ http }: CoreSetup, { licensing }: Dependencies): IndexMgmtSetup { @@ -41,17 +43,20 @@ export class IndexMgmtServerPlugin implements Plugin { - const indices = await fetchIndices(ctx.core.elasticsearch.dataClient.callAsCurrentUser); + const indices = await fetchIndices( + ctx.core.elasticsearch.dataClient.callAsCurrentUser, + indexDataEnricher + ); return res.ok({ body: indices }); }) ); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts index 1007ad16673d0..e3d9942f0154f 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts @@ -6,14 +6,14 @@ import { schema } from '@kbn/config-schema'; import { RouteDependencies } from '../../../types'; -import { addBasePath } from '../index'; import { fetchIndices } from '../../../lib/fetch_indices'; +import { addBasePath } from '../index'; const bodySchema = schema.object({ indexNames: schema.arrayOf(schema.string()), }); -export function registerReloadRoute({ router, license }: RouteDependencies) { +export function registerReloadRoute({ router, license, indexDataEnricher }: RouteDependencies) { router.post( { path: addBasePath('/indices/reload'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -21,6 +21,7 @@ export function registerReloadRoute({ router, license }: RouteDependencies) { const indices = await fetchIndices( ctx.core.elasticsearch.dataClient.callAsCurrentUser, + indexDataEnricher, indexNames ); return res.ok({ body: indices }); diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts b/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts index b7a45e59549eb..f1a2c2c009939 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts @@ -5,3 +5,5 @@ */ export { License } from './license'; + +export { IndexDataEnricher, Enricher } from './index_data_enricher'; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/index_data_enricher.ts b/x-pack/legacy/plugins/index_management/server/np_ready/services/index_data_enricher.ts new file mode 100644 index 0000000000000..7a62ce9f7a3c3 --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/np_ready/services/index_data_enricher.ts @@ -0,0 +1,40 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { Index, CallAsCurrentUser } from '../types'; + +export type Enricher = (indices: Index[], callAsCurrentUser: CallAsCurrentUser) => Promise; + +export class IndexDataEnricher { + private readonly _enrichers: Enricher[] = []; + + public add(enricher: Enricher) { + this._enrichers.push(enricher); + } + + public enrichIndices = async ( + indices: Index[], + callAsCurrentUser: CallAsCurrentUser + ): Promise => { + let enrichedIndices = indices; + + for (let i = 0; i < this.enrichers.length; i++) { + const dataEnricher = this.enrichers[i]; + try { + const dataEnricherResponse = await dataEnricher(enrichedIndices, callAsCurrentUser); + enrichedIndices = dataEnricherResponse; + } catch (e) { + // silently swallow enricher response errors + } + } + + return enrichedIndices; + }; + + public get enrichers() { + return this._enrichers; + } +} diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts index fa3783ff89d4b..1cf32ead3aefa 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/types.ts @@ -3,9 +3,9 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { IRouter } from 'src/core/server'; +import { ScopedClusterClient, IRouter } from 'src/core/server'; import { LicensingPluginSetup } from '../../../../../plugins/licensing/server'; -import { License } from './services/license'; +import { License, IndexDataEnricher } from './services'; export interface Dependencies { licensing: LicensingPluginSetup; @@ -14,4 +14,21 @@ export interface Dependencies { export interface RouteDependencies { router: IRouter; license: License; + indexDataEnricher: IndexDataEnricher; } + +export interface Index { + health: string; + status: string; + name: string; + uuid: string; + primary: string; + replica: string; + documents: any; + size: any; + isFrozen: boolean; + aliases: string | string[]; + [key: string]: any; +} + +export type CallAsCurrentUser = ScopedClusterClient['callAsCurrentUser']; From a6e227eece1fd703f27d0f8f31a0bee70db2c5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Wed, 5 Feb 2020 13:51:11 +0530 Subject: [PATCH 22/30] Add missing license route guard --- .../routes/api/templates/register_get_routes.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts index 13ad39bcede15..9d4dcfd6bcf3a 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts @@ -10,15 +10,18 @@ import { getManagedTemplatePrefix } from '../../../lib/get_managed_templates'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -export function registerGetAllRoute({ router }: RouteDependencies) { - router.get({ path: addBasePath('/templates'), validate: false }, async (ctx, req, res) => { - const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; - const managedTemplatePrefix = await getManagedTemplatePrefix(callAsCurrentUser); +export function registerGetAllRoute({ router, license }: RouteDependencies) { + router.get( + { path: addBasePath('/templates'), validate: false }, + license.guardApiRoute(async (ctx, req, res) => { + const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; + const managedTemplatePrefix = await getManagedTemplatePrefix(callAsCurrentUser); - const indexTemplatesByName = await callAsCurrentUser('indices.getTemplate'); + const indexTemplatesByName = await callAsCurrentUser('indices.getTemplate'); - return res.ok({ body: deserializeTemplateList(indexTemplatesByName, managedTemplatePrefix) }); - }); + return res.ok({ body: deserializeTemplateList(indexTemplatesByName, managedTemplatePrefix) }); + }) + ); } const paramsSchema = schema.object({ From fec69db1bc42baa19838ec9bf3ee13e768840d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Wed, 5 Feb 2020 14:09:37 +0530 Subject: [PATCH 23/30] Add missing "rollover_alias" prop to ilmPolicy schema object --- .../server/np_ready/routes/api/templates/validate_schemas.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts index 5d9ab368ee0d3..fb5d41870eece 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts +++ b/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts @@ -17,6 +17,7 @@ export const templateSchema = schema.object({ ilmPolicy: schema.maybe( schema.object({ name: schema.maybe(schema.string()), + rollover_alias: schema.maybe(schema.string()), }) ), isManaged: schema.maybe(schema.boolean()), From 78340e64b7101e3c75be4c42f55bed57bead7429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Wed, 5 Feb 2020 14:16:32 +0530 Subject: [PATCH 24/30] Refactor var name plugin public api --- x-pack/legacy/plugins/index_management/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/index.ts b/x-pack/legacy/plugins/index_management/index.ts index 401bb7923cb9d..fdb555dbe5de4 100644 --- a/x-pack/legacy/plugins/index_management/index.ts +++ b/x-pack/legacy/plugins/index_management/index.ts @@ -49,9 +49,9 @@ export function indexManagement(kibana: any) { }; const serverPlugin = initServerPlugin({ logger: mockLogger } as any); - const indexMgmtSetup = serverPlugin.setup(coreSetup, pluginDependencies); + const indexMgmtPublicApi = serverPlugin.setup(coreSetup, pluginDependencies); - server.expose('addIndexManagementDataEnricher', indexMgmtSetup.indexDataEnricher.add); + server.expose('addIndexManagementDataEnricher', indexMgmtPublicApi.indexDataEnricher.add); }, }); } From 5e1b5e7597ff5699e746ecc5cb176657a8d148b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Wed, 5 Feb 2020 14:26:18 +0530 Subject: [PATCH 25/30] Remove "np_ready" folder for server side --- .../legacy/plugins/index_management/index.ts | 32 ++++--------------- .../server/{np_ready => }/index.ts | 0 .../{np_ready => }/lib/fetch_aliases.test.ts | 0 .../{np_ready => }/lib/fetch_aliases.ts | 0 .../{np_ready => }/lib/fetch_indices.ts | 0 .../lib/get_managed_templates.ts | 0 .../server/{np_ready => }/plugin.ts | 2 +- .../server/{np_ready => }/routes/api/index.ts | 2 +- .../routes/api/indices/index.ts | 0 .../api/indices/register_clear_cache_route.ts | 0 .../api/indices/register_close_route.ts | 0 .../api/indices/register_delete_route.ts | 0 .../api/indices/register_flush_route.ts | 0 .../api/indices/register_forcemerge_route.ts | 0 .../api/indices/register_freeze_route.ts | 0 .../api/indices/register_indices_routes.ts | 0 .../routes/api/indices/register_list_route.ts | 0 .../routes/api/indices/register_open_route.ts | 0 .../api/indices/register_refresh_route.ts | 0 .../api/indices/register_reload_route.ts | 0 .../api/indices/register_unfreeze_route.ts | 0 .../routes/api/mapping/index.ts | 0 .../api/mapping/register_mapping_route.ts | 0 .../routes/api/settings/index.ts | 0 .../api/settings/register_load_route.ts | 0 .../api/settings/register_settings_routes.ts | 0 .../api/settings/register_update_route.ts | 0 .../{np_ready => }/routes/api/stats/index.ts | 0 .../routes/api/stats/register_stats_route.ts | 0 .../routes/api/templates/index.ts | 0 .../api/templates/register_create_route.ts | 4 +-- .../api/templates/register_delete_route.ts | 2 +- .../api/templates/register_get_routes.ts | 2 +- .../api/templates/register_template_routes.ts | 0 .../api/templates/register_update_route.ts | 4 +-- .../routes/api/templates/validate_schemas.ts | 0 .../server/{np_ready => }/routes/helpers.ts | 0 .../server/{np_ready => }/routes/index.ts | 0 .../server/{np_ready => }/services/index.ts | 0 .../services/index_data_enricher.ts | 0 .../server/{np_ready => }/services/license.ts | 6 ++-- .../server/{np_ready => }/types.ts | 2 +- 42 files changed, 19 insertions(+), 37 deletions(-) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/lib/fetch_aliases.test.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/lib/fetch_aliases.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/lib/fetch_indices.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/lib/get_managed_templates.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/plugin.ts (97%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/index.ts (85%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_clear_cache_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_close_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_delete_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_flush_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_forcemerge_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_freeze_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_indices_routes.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_list_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_open_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_refresh_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_reload_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/indices/register_unfreeze_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/mapping/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/mapping/register_mapping_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/settings/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/settings/register_load_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/settings/register_settings_routes.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/settings/register_update_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/stats/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/stats/register_stats_route.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/templates/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/templates/register_create_route.ts (93%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/templates/register_delete_route.ts (96%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/templates/register_get_routes.ts (98%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/templates/register_template_routes.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/templates/register_update_route.ts (92%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/api/templates/validate_schemas.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/helpers.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/routes/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/services/index.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/services/index_data_enricher.ts (100%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/services/license.ts (88%) rename x-pack/legacy/plugins/index_management/server/{np_ready => }/types.ts (91%) diff --git a/x-pack/legacy/plugins/index_management/index.ts b/x-pack/legacy/plugins/index_management/index.ts index fdb555dbe5de4..c92b38c0d94be 100644 --- a/x-pack/legacy/plugins/index_management/index.ts +++ b/x-pack/legacy/plugins/index_management/index.ts @@ -7,7 +7,7 @@ import { resolve } from 'path'; import { Legacy } from 'kibana'; import { PLUGIN } from './common/constants'; -import { plugin as initServerPlugin, Dependencies } from './server/np_ready'; +import { plugin as initServerPlugin, Dependencies } from './server'; export type ServerFacade = Legacy.Server; @@ -25,33 +25,15 @@ export function indexManagement(kibana: any) { init(server: ServerFacade) { const coreSetup = server.newPlatform.setup.core; - - const pluginDependencies: Dependencies = { - licensing: { - license$: { - subscribe(handler: any) { - return handler({ - check() { - return { - state: 'VALID', - }; - }, - }); - }, - }, - } as any, - }; - - const mockLogger = { - get: () => ({ - info() {}, - }), + const coreInitializerContext = server.newPlatform.coreContext; + const pluginsSetup: Dependencies = { + licensing: server.newPlatform.setup.plugins.licensing as any, }; - const serverPlugin = initServerPlugin({ logger: mockLogger } as any); - const indexMgmtPublicApi = serverPlugin.setup(coreSetup, pluginDependencies); + const serverPlugin = initServerPlugin(coreInitializerContext as any); + const serverPublicApi = serverPlugin.setup(coreSetup, pluginsSetup); - server.expose('addIndexManagementDataEnricher', indexMgmtPublicApi.indexDataEnricher.add); + server.expose('addIndexManagementDataEnricher', serverPublicApi.indexDataEnricher.add); }, }); } diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/index.ts b/x-pack/legacy/plugins/index_management/server/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/index.ts rename to x-pack/legacy/plugins/index_management/server/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.test.ts b/x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.test.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.test.ts rename to x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.test.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.ts b/x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_aliases.ts rename to x-pack/legacy/plugins/index_management/server/lib/fetch_aliases.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts b/x-pack/legacy/plugins/index_management/server/lib/fetch_indices.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/lib/fetch_indices.ts rename to x-pack/legacy/plugins/index_management/server/lib/fetch_indices.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts b/x-pack/legacy/plugins/index_management/server/lib/get_managed_templates.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/lib/get_managed_templates.ts rename to x-pack/legacy/plugins/index_management/server/lib/get_managed_templates.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts b/x-pack/legacy/plugins/index_management/server/plugin.ts similarity index 97% rename from x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts rename to x-pack/legacy/plugins/index_management/server/plugin.ts index 5f76d7ce1fa7f..8677c075f8b0e 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/plugin.ts @@ -6,7 +6,7 @@ import { i18n } from '@kbn/i18n'; import { CoreSetup, Plugin, Logger, PluginInitializerContext } from 'src/core/server'; -import { PLUGIN } from '../../common'; +import { PLUGIN } from '../common'; import { Dependencies } from './types'; import { ApiRoutes } from './routes'; import { License, IndexDataEnricher } from './services'; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/index.ts b/x-pack/legacy/plugins/index_management/server/routes/api/index.ts similarity index 85% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/index.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/index.ts index 59a3b5d7f68a9..4ed008480c149 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/index.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/index.ts @@ -4,6 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { API_BASE_PATH } from '../../../../common'; +import { API_BASE_PATH } from '../../../common'; export const addBasePath = (uri: string): string => API_BASE_PATH + uri; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/index.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/index.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_clear_cache_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_close_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_delete_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_flush_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_forcemerge_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_freeze_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_indices_routes.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_indices_routes.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_indices_routes.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_list_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_open_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_refresh_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_reload_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/indices/register_unfreeze_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/index.ts b/x-pack/legacy/plugins/index_management/server/routes/api/mapping/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/index.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/mapping/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/mapping/register_mapping_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/index.ts b/x-pack/legacy/plugins/index_management/server/routes/api/settings/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/index.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/settings/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_load_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts b/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_settings_routes.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_settings_routes.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/settings/register_settings_routes.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/settings/register_update_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/index.ts b/x-pack/legacy/plugins/index_management/server/routes/api/stats/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/index.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/stats/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/stats/register_stats_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/index.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/index.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/templates/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts similarity index 93% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts index a87fa749b0fc6..2e70ea8a135d0 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_create_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts @@ -6,8 +6,8 @@ import { i18n } from '@kbn/i18n'; -import { Template, TemplateEs } from '../../../../../common/types'; -import { serializeTemplate } from '../../../../../common/lib'; +import { Template, TemplateEs } from '../../../../common/types'; +import { serializeTemplate } from '../../../../common/lib'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; import { templateSchema } from './validate_schemas'; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_delete_route.ts similarity index 96% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/templates/register_delete_route.ts index 88f7adf2c3504..c9f1995204d8c 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_delete_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_delete_route.ts @@ -10,7 +10,7 @@ import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; import { wrapEsError } from '../../helpers'; -import { Template } from '../../../../../common/types'; +import { Template } from '../../../../common/types'; const paramsSchema = schema.object({ names: schema.string(), diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts similarity index 98% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts index 9d4dcfd6bcf3a..1b710a50af323 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_get_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts @@ -5,7 +5,7 @@ */ import { schema } from '@kbn/config-schema'; -import { deserializeTemplate, deserializeTemplateList } from '../../../../../common/lib'; +import { deserializeTemplate, deserializeTemplateList } from '../../../../common/lib'; import { getManagedTemplatePrefix } from '../../../lib/get_managed_templates'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_template_routes.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_template_routes.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/templates/register_template_routes.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts similarity index 92% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts index 05543a08a799f..cf34d1b0bc3f8 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts @@ -5,8 +5,8 @@ */ import { schema } from '@kbn/config-schema'; -import { Template, TemplateEs } from '../../../../../common/types'; -import { serializeTemplate } from '../../../../../common/lib'; +import { Template, TemplateEs } from '../../../../common/types'; +import { serializeTemplate } from '../../../../common/lib'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; import { templateSchema } from './validate_schemas'; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/validate_schemas.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/api/templates/validate_schemas.ts rename to x-pack/legacy/plugins/index_management/server/routes/api/templates/validate_schemas.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/helpers.ts b/x-pack/legacy/plugins/index_management/server/routes/helpers.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/helpers.ts rename to x-pack/legacy/plugins/index_management/server/routes/helpers.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts b/x-pack/legacy/plugins/index_management/server/routes/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/routes/index.ts rename to x-pack/legacy/plugins/index_management/server/routes/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts b/x-pack/legacy/plugins/index_management/server/services/index.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/services/index.ts rename to x-pack/legacy/plugins/index_management/server/services/index.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/index_data_enricher.ts b/x-pack/legacy/plugins/index_management/server/services/index_data_enricher.ts similarity index 100% rename from x-pack/legacy/plugins/index_management/server/np_ready/services/index_data_enricher.ts rename to x-pack/legacy/plugins/index_management/server/services/index_data_enricher.ts diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts b/x-pack/legacy/plugins/index_management/server/services/license.ts similarity index 88% rename from x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts rename to x-pack/legacy/plugins/index_management/server/services/license.ts index 9257c1d20632c..fc284a0e3eb65 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/services/license.ts +++ b/x-pack/legacy/plugins/index_management/server/services/license.ts @@ -11,9 +11,9 @@ import { RequestHandlerContext, } from 'kibana/server'; -import { LicensingPluginSetup } from '../../../../../../plugins/licensing/server'; -import { LicenseType } from '../../../../../../plugins/licensing/common/types'; -import { LICENSE_CHECK_STATE } from '../../../../../../plugins/licensing/common/types'; +import { LicensingPluginSetup } from '../../../../../plugins/licensing/server'; +import { LicenseType } from '../../../../../plugins/licensing/common/types'; +import { LICENSE_CHECK_STATE } from '../../../../../plugins/licensing/common/types'; export interface LicenseStatus { isValid: boolean; diff --git a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts b/x-pack/legacy/plugins/index_management/server/types.ts similarity index 91% rename from x-pack/legacy/plugins/index_management/server/np_ready/types.ts rename to x-pack/legacy/plugins/index_management/server/types.ts index 1cf32ead3aefa..1385bcbb438b7 100644 --- a/x-pack/legacy/plugins/index_management/server/np_ready/types.ts +++ b/x-pack/legacy/plugins/index_management/server/types.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { ScopedClusterClient, IRouter } from 'src/core/server'; -import { LicensingPluginSetup } from '../../../../../plugins/licensing/server'; +import { LicensingPluginSetup } from '../../../../plugins/licensing/server'; import { License, IndexDataEnricher } from './services'; export interface Dependencies { From 7f8d3052ea4c9c63e00b7d49a60e4116e9d75487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Wed, 5 Feb 2020 14:39:38 +0530 Subject: [PATCH 26/30] Add readonly to Server plugin class --- x-pack/legacy/plugins/index_management/server/plugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/plugin.ts b/x-pack/legacy/plugins/index_management/server/plugin.ts index 8677c075f8b0e..f8da8f6daa415 100644 --- a/x-pack/legacy/plugins/index_management/server/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/plugin.ts @@ -20,8 +20,8 @@ export interface IndexMgmtSetup { export class IndexMgmtServerPlugin implements Plugin { private readonly apiRoutes: ApiRoutes; private readonly license: License; - private logger: Logger; - private indexDataEnricher: IndexDataEnricher; + private readonly logger: Logger; + private readonly indexDataEnricher: IndexDataEnricher; constructor({ logger }: PluginInitializerContext) { this.logger = logger.get(); From 5861ab0f27c7952fcaa3d4117e34351bc710cd54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Fri, 7 Feb 2020 14:26:15 +0530 Subject: [PATCH 27/30] Add lib helpers in route dependencies --- .../index_management/server/lib/is_es_error.ts | 13 +++++++++++++ .../plugins/index_management/server/plugin.ts | 4 ++++ .../legacy/plugins/index_management/server/types.ts | 4 ++++ 3 files changed, 21 insertions(+) create mode 100644 x-pack/legacy/plugins/index_management/server/lib/is_es_error.ts diff --git a/x-pack/legacy/plugins/index_management/server/lib/is_es_error.ts b/x-pack/legacy/plugins/index_management/server/lib/is_es_error.ts new file mode 100644 index 0000000000000..4137293cf39c0 --- /dev/null +++ b/x-pack/legacy/plugins/index_management/server/lib/is_es_error.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as legacyElasticsearch from 'elasticsearch'; + +const esErrorsParent = legacyElasticsearch.errors._Abstract; + +export function isEsError(err: Error) { + return err instanceof esErrorsParent; +} diff --git a/x-pack/legacy/plugins/index_management/server/plugin.ts b/x-pack/legacy/plugins/index_management/server/plugin.ts index f8da8f6daa415..95d27e1cf16ba 100644 --- a/x-pack/legacy/plugins/index_management/server/plugin.ts +++ b/x-pack/legacy/plugins/index_management/server/plugin.ts @@ -10,6 +10,7 @@ import { PLUGIN } from '../common'; import { Dependencies } from './types'; import { ApiRoutes } from './routes'; import { License, IndexDataEnricher } from './services'; +import { isEsError } from './lib/is_es_error'; export interface IndexMgmtSetup { indexDataEnricher: { @@ -51,6 +52,9 @@ export class IndexMgmtServerPlugin implements Plugin Date: Fri, 7 Feb 2020 14:33:12 +0530 Subject: [PATCH 28/30] Wrap all server api response in try catch and return ES errors --- .../api/indices/register_clear_cache_route.ts | 17 ++++++-- .../api/indices/register_close_route.ts | 17 ++++++-- .../api/indices/register_delete_route.ts | 17 ++++++-- .../api/indices/register_flush_route.ts | 17 ++++++-- .../api/indices/register_forcemerge_route.ts | 17 ++++++-- .../api/indices/register_freeze_route.ts | 20 +++++++-- .../routes/api/indices/register_list_route.ts | 23 ++++++++--- .../routes/api/indices/register_open_route.ts | 18 ++++++-- .../api/indices/register_refresh_route.ts | 17 ++++++-- .../api/indices/register_reload_route.ts | 30 ++++++++++---- .../api/indices/register_unfreeze_route.ts | 17 ++++++-- .../api/mapping/register_mapping_route.ts | 25 +++++++---- .../api/settings/register_load_route.ts | 23 ++++++++--- .../api/settings/register_update_route.ts | 23 ++++++++--- .../routes/api/stats/register_stats_route.ts | 23 ++++++++--- .../api/templates/register_create_route.ts | 39 +++++++++++------- .../api/templates/register_get_routes.ts | 32 +++++++++++---- .../api/templates/register_update_route.ts | 41 ++++++++++++------- 18 files changed, 313 insertions(+), 103 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts index bafdb7350b63f..ec42b2aee45a9 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_clear_cache_route.ts @@ -12,7 +12,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerClearCacheRoute({ router, license }: RouteDependencies) { +export function registerClearCacheRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/clear_cache'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -25,8 +25,19 @@ export function registerClearCacheRoute({ router, license }: RouteDependencies) index: indices, }; - await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.clearCache', params); - return res.ok(); + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.clearCache', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts index 1972da004996d..bd243ab3e5de5 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_close_route.ts @@ -12,7 +12,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerCloseRoute({ router, license }: RouteDependencies) { +export function registerCloseRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/close'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -25,8 +25,19 @@ export function registerCloseRoute({ router, license }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.close', params); - return res.ok(); + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.close', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts index 0c2bafe562b78..ffe30b315363a 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_delete_route.ts @@ -13,7 +13,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerDeleteRoute({ router, license }: RouteDependencies) { +export function registerDeleteRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/delete'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -26,8 +26,19 @@ export function registerDeleteRoute({ router, license }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.delete', params); - return res.ok(); + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.delete', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts index 75293d291d041..fee3a0f5278da 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_flush_route.ts @@ -13,7 +13,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerFlushRoute({ router, license }: RouteDependencies) { +export function registerFlushRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/flush'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -26,8 +26,19 @@ export function registerFlushRoute({ router, license }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.flush', params); - return res.ok(); + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.flush', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts index f2c53e96a57ac..a23f8e448f65b 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts @@ -14,7 +14,7 @@ const bodySchema = schema.object({ maxNumSegments: schema.number(), }); -export function registerForcemergeRoute({ router, license }: RouteDependencies) { +export function registerForcemergeRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/forcemerge'), @@ -33,8 +33,19 @@ export function registerForcemergeRoute({ router, license }: RouteDependencies) (params as any).max_num_segments = maxNumSegments; } - await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.forcemerge', params); - return res.ok(); + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.forcemerge', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts index 551ed37ab6927..68bb4b13ef475 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_freeze_route.ts @@ -13,7 +13,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerFreezeRoute({ router, license }: RouteDependencies) { +export function registerFreezeRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/freeze'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -25,8 +25,22 @@ export function registerFreezeRoute({ router, license }: RouteDependencies) { method: 'POST', }; - await await ctx.core.elasticsearch.dataClient.callAsCurrentUser('transport.request', params); - return res.ok(); + try { + await await ctx.core.elasticsearch.dataClient.callAsCurrentUser( + 'transport.request', + params + ); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts index 10dbf8e1a48fa..1f5d8ddf529eb 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_list_route.ts @@ -8,15 +8,26 @@ import { fetchIndices } from '../../../lib/fetch_indices'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; -export function registerListRoute({ router, license, indexDataEnricher }: RouteDependencies) { +export function registerListRoute({ router, license, indexDataEnricher, lib }: RouteDependencies) { router.get( { path: addBasePath('/indices'), validate: false }, license.guardApiRoute(async (ctx, req, res) => { - const indices = await fetchIndices( - ctx.core.elasticsearch.dataClient.callAsCurrentUser, - indexDataEnricher - ); - return res.ok({ body: indices }); + try { + const indices = await fetchIndices( + ctx.core.elasticsearch.dataClient.callAsCurrentUser, + indexDataEnricher + ); + return res.ok({ body: indices }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts index 1f44239d54f33..28dbae0d8864b 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_open_route.ts @@ -12,7 +12,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerOpenRoute({ router, license }: RouteDependencies) { +export function registerOpenRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/open'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -24,8 +24,20 @@ export function registerOpenRoute({ router, license }: RouteDependencies) { format: 'json', index: indices, }; - await await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.open', params); - return res.ok(); + + try { + await await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.open', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts index b551cccb7741e..34fee477662e8 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_refresh_route.ts @@ -13,7 +13,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerRefreshRoute({ router, license }: RouteDependencies) { +export function registerRefreshRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/refresh'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -26,8 +26,19 @@ export function registerRefreshRoute({ router, license }: RouteDependencies) { index: indices, }; - await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.refresh', params); - return res.ok(); + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('indices.refresh', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts index e3d9942f0154f..168e8fec8acd6 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts @@ -13,18 +13,34 @@ const bodySchema = schema.object({ indexNames: schema.arrayOf(schema.string()), }); -export function registerReloadRoute({ router, license, indexDataEnricher }: RouteDependencies) { +export function registerReloadRoute({ + router, + license, + indexDataEnricher, + lib, +}: RouteDependencies) { router.post( { path: addBasePath('/indices/reload'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { const { indexNames = [] } = req.body as typeof bodySchema.type; - const indices = await fetchIndices( - ctx.core.elasticsearch.dataClient.callAsCurrentUser, - indexDataEnricher, - indexNames - ); - return res.ok({ body: indices }); + try { + const indices = await fetchIndices( + ctx.core.elasticsearch.dataClient.callAsCurrentUser, + indexDataEnricher, + indexNames + ); + return res.ok({ body: indices }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts index 262901555928f..67c4a3516d1e6 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_unfreeze_route.ts @@ -13,7 +13,7 @@ const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), }); -export function registerUnfreezeRoute({ router, license }: RouteDependencies) { +export function registerUnfreezeRoute({ router, license, lib }: RouteDependencies) { router.post( { path: addBasePath('/indices/unfreeze'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -23,8 +23,19 @@ export function registerUnfreezeRoute({ router, license }: RouteDependencies) { method: 'POST', }; - await ctx.core.elasticsearch.dataClient.callAsCurrentUser('transport.request', params); - return res.ok(); + try { + await ctx.core.elasticsearch.dataClient.callAsCurrentUser('transport.request', params); + return res.ok(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts index f8661d9dd899d..20d7e6b4d7232 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/mapping/register_mapping_route.ts @@ -19,7 +19,7 @@ function formatHit(hit: { [key: string]: { mappings: any } }, indexName: string) }; } -export function registerMappingRoute({ router, license }: RouteDependencies) { +export function registerMappingRoute({ router, license, lib }: RouteDependencies) { router.get( { path: addBasePath('/mapping/{indexName}'), validate: { params: paramsSchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -29,12 +29,23 @@ export function registerMappingRoute({ router, license }: RouteDependencies) { index: indexName, }; - const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( - 'indices.getMapping', - params - ); - const response = formatHit(hit, indexName); - return res.ok({ body: response }); + try { + const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( + 'indices.getMapping', + params + ); + const response = formatHit(hit, indexName); + return res.ok({ body: response }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts index 951dc469c923f..c31813b4a9f49 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_load_route.ts @@ -19,7 +19,7 @@ function formatHit(hit: { [key: string]: {} }) { return hit[key]; } -export function registerLoadRoute({ router, license }: RouteDependencies) { +export function registerLoadRoute({ router, license, lib }: RouteDependencies) { router.get( { path: addBasePath('/settings/{indexName}'), validate: { params: paramsSchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -32,11 +32,22 @@ export function registerLoadRoute({ router, license }: RouteDependencies) { index: indexName, }; - const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( - 'indices.getSettings', - params - ); - return res.ok({ body: formatHit(hit) }); + try { + const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( + 'indices.getSettings', + params + ); + return res.ok({ body: formatHit(hit) }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts index d5b62605d78d1..9ce5ae7f99393 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/settings/register_update_route.ts @@ -14,7 +14,7 @@ const paramsSchema = schema.object({ indexName: schema.string(), }); -export function registerUpdateRoute({ router, license }: RouteDependencies) { +export function registerUpdateRoute({ router, license, lib }: RouteDependencies) { router.put( { path: addBasePath('/settings/{indexName}'), @@ -30,11 +30,22 @@ export function registerUpdateRoute({ router, license }: RouteDependencies) { body: req.body, }; - const response = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( - 'indices.putSettings', - params - ); - return res.ok({ body: response }); + try { + const response = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( + 'indices.putSettings', + params + ); + return res.ok({ body: response }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts index efab387a6263f..f408fd6584bd5 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/stats/register_stats_route.ts @@ -21,7 +21,7 @@ function formatHit(hit: { _shards: any; indices: { [key: string]: any } }, index }; } -export function registerStatsRoute({ router, license }: RouteDependencies) { +export function registerStatsRoute({ router, license, lib }: RouteDependencies) { router.get( { path: addBasePath('/stats/{indexName}'), validate: { params: paramsSchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -30,12 +30,23 @@ export function registerStatsRoute({ router, license }: RouteDependencies) { expand_wildcards: 'none', index: indexName, }; - const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( - 'indices.stats', - params - ); - return res.ok({ body: formatHit(hit, indexName) }); + try { + const hit = await ctx.core.elasticsearch.dataClient.callAsCurrentUser( + 'indices.stats', + params + ); + return res.ok({ body: formatHit(hit, indexName) }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts index 2e70ea8a135d0..817893976767f 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_create_route.ts @@ -14,7 +14,7 @@ import { templateSchema } from './validate_schemas'; const bodySchema = templateSchema; -export function registerCreateRoute({ router, license }: RouteDependencies) { +export function registerCreateRoute({ router, license, lib }: RouteDependencies) { router.put( { path: addBasePath('/templates'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { @@ -48,20 +48,31 @@ export function registerCreateRoute({ router, license }: RouteDependencies) { }); } - // Otherwise create new index template - const response = await callAsCurrentUser('indices.putTemplate', { - name, - order, - body: { - index_patterns, - version, - settings, - mappings, - aliases, - }, - }); + try { + // Otherwise create new index template + const response = await callAsCurrentUser('indices.putTemplate', { + name, + order, + body: { + index_patterns, + version, + settings, + mappings, + aliases, + }, + }); - return res.ok({ body: response }); + return res.ok({ body: response }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts index 1b710a50af323..d6776d774d3bf 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_get_routes.ts @@ -28,23 +28,37 @@ const paramsSchema = schema.object({ name: schema.string(), }); -export function registerGetOneRoute({ router, license }: RouteDependencies) { +export function registerGetOneRoute({ router, license, lib }: RouteDependencies) { router.get( { path: addBasePath('/templates/{name}'), validate: { params: paramsSchema } }, license.guardApiRoute(async (ctx, req, res) => { const { name } = req.params as typeof paramsSchema.type; const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient; - const managedTemplatePrefix = await getManagedTemplatePrefix(callAsCurrentUser); - const indexTemplateByName = await callAsCurrentUser('indices.getTemplate', { name }); + try { + const managedTemplatePrefix = await getManagedTemplatePrefix(callAsCurrentUser); + const indexTemplateByName = await callAsCurrentUser('indices.getTemplate', { name }); - if (indexTemplateByName[name]) { - return res.ok({ - body: deserializeTemplate({ ...indexTemplateByName[name], name }, managedTemplatePrefix), - }); - } + if (indexTemplateByName[name]) { + return res.ok({ + body: deserializeTemplate( + { ...indexTemplateByName[name], name }, + managedTemplatePrefix + ), + }); + } - return res.notFound(); + return res.notFound(); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts index cf34d1b0bc3f8..e7f541fa67f8a 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/templates/register_update_route.ts @@ -16,7 +16,7 @@ const paramsSchema = schema.object({ name: schema.string(), }); -export function registerUpdateRoute({ router, license }: RouteDependencies) { +export function registerUpdateRoute({ router, license, lib }: RouteDependencies) { router.put( { path: addBasePath('/templates/{name}'), @@ -37,20 +37,31 @@ export function registerUpdateRoute({ router, license }: RouteDependencies) { return res.notFound(); } - // Next, update index template - const response = await callAsCurrentUser('indices.putTemplate', { - name, - order, - body: { - index_patterns, - version, - settings, - mappings, - aliases, - }, - }); - - return res.ok({ body: response }); + try { + // Next, update index template + const response = await callAsCurrentUser('indices.putTemplate', { + name, + order, + body: { + index_patterns, + version, + settings, + mappings, + aliases, + }, + }); + + return res.ok({ body: response }); + } catch (e) { + if (lib.isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } }) ); } From 30a7494bea95843676c083e5646d5f870b86888d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Mon, 10 Feb 2020 15:28:50 +0530 Subject: [PATCH 29/30] Fix API integration tests --- .../routes/api/indices/register_forcemerge_route.ts | 2 +- .../server/routes/api/indices/register_reload_route.ts | 10 ++++++---- .../apis/management/index_management/indices.js | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts index a23f8e448f65b..c39547a3cbd40 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_forcemerge_route.ts @@ -11,7 +11,7 @@ import { addBasePath } from '../index'; const bodySchema = schema.object({ indices: schema.arrayOf(schema.string()), - maxNumSegments: schema.number(), + maxNumSegments: schema.maybe(schema.number()), }); export function registerForcemergeRoute({ router, license, lib }: RouteDependencies) { diff --git a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts index 168e8fec8acd6..22a9d79439ab0 100644 --- a/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts +++ b/x-pack/legacy/plugins/index_management/server/routes/api/indices/register_reload_route.ts @@ -9,9 +9,11 @@ import { RouteDependencies } from '../../../types'; import { fetchIndices } from '../../../lib/fetch_indices'; import { addBasePath } from '../index'; -const bodySchema = schema.object({ - indexNames: schema.arrayOf(schema.string()), -}); +const bodySchema = schema.maybe( + schema.object({ + indexNames: schema.maybe(schema.arrayOf(schema.string())), + }) +); export function registerReloadRoute({ router, @@ -22,7 +24,7 @@ export function registerReloadRoute({ router.post( { path: addBasePath('/indices/reload'), validate: { body: bodySchema } }, license.guardApiRoute(async (ctx, req, res) => { - const { indexNames = [] } = req.body as typeof bodySchema.type; + const { indexNames = [] } = (req.body as typeof bodySchema.type) ?? {}; try { const indices = await fetchIndices( diff --git a/x-pack/test/api_integration/apis/management/index_management/indices.js b/x-pack/test/api_integration/apis/management/index_management/indices.js index 5ffab7e057aac..7195b8680a286 100644 --- a/x-pack/test/api_integration/apis/management/index_management/indices.js +++ b/x-pack/test/api_integration/apis/management/index_management/indices.js @@ -104,7 +104,7 @@ export default function({ getService }) { it('should require index or indices to be provided', async () => { const { body } = await deleteIndex().expect(400); - expect(body.message).to.contain('index / indices is missing'); + expect(body.message).to.contain('expected value of type [string]'); }); }); @@ -144,7 +144,7 @@ export default function({ getService }) { it('should allow to define the number of segments', async () => { const index = await createIndex(); - await forceMerge(index, { max_num_segments: 1 }).expect(200); + await forceMerge(index, { maxNumSegments: 1 }).expect(200); }); }); From 167f075597101e133fa3efa57e408f304f47021b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien?= Date: Tue, 11 Feb 2020 10:02:31 +0530 Subject: [PATCH 30/30] Fix API integration test (2) --- .../apis/management/index_management/templates.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x-pack/test/api_integration/apis/management/index_management/templates.js b/x-pack/test/api_integration/apis/management/index_management/templates.js index b404f7336738a..d9344846ebb91 100644 --- a/x-pack/test/api_integration/apis/management/index_management/templates.js +++ b/x-pack/test/api_integration/apis/management/index_management/templates.js @@ -92,14 +92,16 @@ export default function({ getService }) { await createTemplate(payload).expect(409); }); - it('should handle ES errors', async () => { + it('should validate the request payload', async () => { const templateName = `template-${getRandomString()}`; const payload = getTemplatePayload(templateName); delete payload.indexPatterns; // index patterns are required const { body } = await createTemplate(payload); - expect(body.message).to.contain('index patterns are missing'); + expect(body.message).to.contain( + '[request body.indexPatterns]: expected value of type [array] ' + ); }); });