diff --git a/x-pack/plugins/index_lifecycle_management/server/plugin.ts b/x-pack/plugins/index_lifecycle_management/server/plugin.ts index 84b8fa35cfe9b..40037d0c1e777 100644 --- a/x-pack/plugins/index_lifecycle_management/server/plugin.ts +++ b/x-pack/plugins/index_lifecycle_management/server/plugin.ts @@ -22,10 +22,10 @@ import { Dependencies } from './types'; import { registerApiRoutes } from './routes'; import { License } from './services'; import { IndexLifecycleManagementConfig } from './config'; -import { isEsError } from './shared_imports'; const indexLifecycleDataEnricher = async ( indicesList: IndexWithoutIlm[], + // TODO replace deprecated ES client after Index Management is updated callAsCurrentUser: LegacyAPICaller ): Promise => { if (!indicesList || !indicesList.length) { @@ -99,9 +99,6 @@ export class IndexLifecycleManagementServerPlugin implements Plugin { @@ -45,17 +41,17 @@ export function registerAddPolicyRoute({ router, license, lib }: RouteDependenci try { await addLifecyclePolicy( - context.core.elasticsearch.legacy.client.callAsCurrentUser, + context.core.elasticsearch.client.asCurrentUser, indexName, policyName, alias ); return response.ok(); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_remove_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_remove_route.ts index 2601775f5d76e..a83a3fa1378c8 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_remove_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_remove_route.ts @@ -5,22 +5,19 @@ */ import { schema } from '@kbn/config-schema'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'kibana/server'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; -async function removeLifecycle(callAsCurrentUser: LegacyAPICaller, indexNames: string[]) { +async function removeLifecycle(client: ElasticsearchClient, indexNames: string[]) { + const options = { + ignore: [404], + }; const responses = []; for (let i = 0; i < indexNames.length; i++) { const indexName = indexNames[i]; - const params = { - method: 'POST', - path: `/${encodeURIComponent(indexName)}/_ilm/remove`, - ignore: [404], - }; - - responses.push(callAsCurrentUser('transport.request', params)); + responses.push(client.ilm.removePolicy({ index: indexName }, options)); } return Promise.all(responses); } @@ -29,7 +26,7 @@ const bodySchema = schema.object({ indexNames: schema.arrayOf(schema.string()), }); -export function registerRemoveRoute({ router, license, lib }: RouteDependencies) { +export function registerRemoveRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/index/remove'), validate: { body: bodySchema } }, license.guardApiRoute(async (context, request, response) => { @@ -37,16 +34,13 @@ export function registerRemoveRoute({ router, license, lib }: RouteDependencies) const { indexNames } = body; try { - await removeLifecycle( - context.core.elasticsearch.legacy.client.callAsCurrentUser, - indexNames - ); + await removeLifecycle(context.core.elasticsearch.client.asCurrentUser, indexNames); return response.ok(); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_retry_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_retry_route.ts index 09fd1d6068285..cdcf5ed4b7ac4 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_retry_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_retry_route.ts @@ -5,22 +5,20 @@ */ import { schema } from '@kbn/config-schema'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'kibana/server'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; -async function retryLifecycle(callAsCurrentUser: LegacyAPICaller, indexNames: string[]) { +async function retryLifecycle(client: ElasticsearchClient, indexNames: string[]) { + const options = { + ignore: [404], + }; const responses = []; for (let i = 0; i < indexNames.length; i++) { const indexName = indexNames[i]; - const params = { - method: 'POST', - path: `/${encodeURIComponent(indexName)}/_ilm/retry`, - ignore: [404], - }; - responses.push(callAsCurrentUser('transport.request', params)); + responses.push(client.ilm.retry({ index: indexName }, options)); } return Promise.all(responses); } @@ -29,7 +27,7 @@ const bodySchema = schema.object({ indexNames: schema.arrayOf(schema.string()), }); -export function registerRetryRoute({ router, license, lib }: RouteDependencies) { +export function registerRetryRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/index/retry'), validate: { body: bodySchema } }, license.guardApiRoute(async (context, request, response) => { @@ -37,16 +35,13 @@ export function registerRetryRoute({ router, license, lib }: RouteDependencies) const { indexNames } = body; try { - await retryLifecycle( - context.core.elasticsearch.legacy.client.callAsCurrentUser, - indexNames - ); + await retryLifecycle(context.core.elasticsearch.client.asCurrentUser, indexNames); return response.ok(); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_details_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_details_route.ts index f8d4a9681b3d8..57034af324ed5 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_details_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_details_route.ts @@ -5,7 +5,6 @@ */ import { schema } from '@kbn/config-schema'; -import { LegacyAPICaller } from 'src/core/server'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; @@ -26,19 +25,11 @@ function findMatchingNodes(stats: any, nodeAttrs: string): any { }, []); } -async function fetchNodeStats(callAsCurrentUser: LegacyAPICaller): Promise { - const params = { - format: 'json', - }; - - return await callAsCurrentUser('nodes.stats', params); -} - const paramsSchema = schema.object({ nodeAttrs: schema.string(), }); -export function registerDetailsRoute({ router, license, lib }: RouteDependencies) { +export function registerDetailsRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/nodes/{nodeAttrs}/details'), validate: { params: paramsSchema } }, license.guardApiRoute(async (context, request, response) => { @@ -46,16 +37,14 @@ export function registerDetailsRoute({ router, license, lib }: RouteDependencies const { nodeAttrs } = params; try { - const stats = await fetchNodeStats( - context.core.elasticsearch.legacy.client.callAsCurrentUser - ); - const okResponse = { body: findMatchingNodes(stats, nodeAttrs) }; + const statsResponse = await context.core.elasticsearch.client.asCurrentUser.nodes.stats(); + const okResponse = { body: findMatchingNodes(statsResponse.body, nodeAttrs) }; return response.ok(okResponse); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_list_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_list_route.ts index 99df70e7df82d..f7f048e809d75 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_list_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_list_route.ts @@ -4,8 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'src/core/server'; - import { ListNodesRouteResponse, NodeDataRole } from '../../../../common/types'; import { RouteDependencies } from '../../../types'; @@ -47,15 +45,7 @@ function convertStatsIntoList( ); } -async function fetchNodeStats(callAsCurrentUser: LegacyAPICaller): Promise { - const params = { - format: 'json', - }; - - return await callAsCurrentUser('nodes.stats', params); -} - -export function registerListRoute({ router, config, license, lib }: RouteDependencies) { +export function registerListRoute({ router, config, license }: RouteDependencies) { const { filteredNodeAttributes } = config; const NODE_ATTRS_KEYS_TO_IGNORE: string[] = [ @@ -74,16 +64,19 @@ export function registerListRoute({ router, config, license, lib }: RouteDepende { path: addBasePath('/nodes/list'), validate: false }, license.guardApiRoute(async (context, request, response) => { try { - const stats = await fetchNodeStats( - context.core.elasticsearch.legacy.client.callAsCurrentUser + const statsResponse = await context.core.elasticsearch.client.asCurrentUser.nodes.stats< + Stats + >(); + const body: ListNodesRouteResponse = convertStatsIntoList( + statsResponse.body, + disallowedNodeAttributes ); - const body: ListNodesRouteResponse = convertStatsIntoList(stats, disallowedNodeAttributes); return response.ok({ body }); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_create_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_create_route.ts index d9e0a6e218de5..359b275622f0c 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_create_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_create_route.ts @@ -5,29 +5,22 @@ */ import { schema } from '@kbn/config-schema'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'kibana/server'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; -async function createPolicy( - callAsCurrentUser: LegacyAPICaller, - name: string, - phases: any -): Promise { +async function createPolicy(client: ElasticsearchClient, name: string, phases: any): Promise { const body = { policy: { phases, }, }; - const params = { - method: 'PUT', - path: `/_ilm/policy/${encodeURIComponent(name)}`, + const options = { ignore: [404], - body, }; - return await callAsCurrentUser('transport.request', params); + return client.ilm.putLifecycle({ policy: name, body }, options); } const minAgeSchema = schema.maybe(schema.string()); @@ -141,7 +134,7 @@ const bodySchema = schema.object({ }), }); -export function registerCreateRoute({ router, license, lib }: RouteDependencies) { +export function registerCreateRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/policies'), validate: { body: bodySchema } }, license.guardApiRoute(async (context, request, response) => { @@ -149,17 +142,13 @@ export function registerCreateRoute({ router, license, lib }: RouteDependencies) const { name, phases } = body; try { - await createPolicy( - context.core.elasticsearch.legacy.client.callAsCurrentUser, - name, - phases - ); + await createPolicy(context.core.elasticsearch.client.asCurrentUser, name, phases); return response.ok(); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts index 992a0fab452ec..cb394c12c46fa 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts @@ -5,30 +5,25 @@ */ import { schema } from '@kbn/config-schema'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'kibana/server'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; -async function deletePolicies( - callAsCurrentUser: LegacyAPICaller, - policyNames: string -): Promise { - const params = { - method: 'DELETE', - path: `/_ilm/policy/${encodeURIComponent(policyNames)}`, +async function deletePolicies(client: ElasticsearchClient, policyName: string): Promise { + const options = { // we allow 404 since they may have no policies ignore: [404], }; - return await callAsCurrentUser('transport.request', params); + return client.ilm.deleteLifecycle({ policy: policyName }, options); } const paramsSchema = schema.object({ policyNames: schema.string(), }); -export function registerDeleteRoute({ router, license, lib }: RouteDependencies) { +export function registerDeleteRoute({ router, license }: RouteDependencies) { router.delete( { path: addBasePath('/policies/{policyNames}'), validate: { params: paramsSchema } }, license.guardApiRoute(async (context, request, response) => { @@ -36,16 +31,13 @@ export function registerDeleteRoute({ router, license, lib }: RouteDependencies) const { policyNames } = params; try { - await deletePolicies( - context.core.elasticsearch.legacy.client.callAsCurrentUser, - policyNames - ); + await deletePolicies(context.core.elasticsearch.client.asCurrentUser, policyNames); return response.ok(); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts index 4fb21ea8c6a62..8cbea25666378 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts @@ -5,22 +5,17 @@ */ import { schema } from '@kbn/config-schema'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'kibana/server'; +import { ApiResponse } from '@elastic/elasticsearch'; import { IndexLifecyclePolicy, PolicyFromES } from '../../../../common/types'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; -type PoliciesMap = { +interface PoliciesMap { [K: string]: Omit; -} & { - status?: number; -}; +} function formatPolicies(policiesMap: PoliciesMap): PolicyFromES[] { - if (policiesMap.status === 404) { - return []; - } - return Object.keys(policiesMap).reduce((accum: PolicyFromES[], lifecycleName: string) => { const policyEntry = policiesMap[lifecycleName]; accum.push({ @@ -31,31 +26,25 @@ function formatPolicies(policiesMap: PoliciesMap): PolicyFromES[] { }, []); } -async function fetchPolicies(callAsCurrentUser: LegacyAPICaller): Promise { - const params = { - method: 'GET', - path: '/_ilm/policy', +async function fetchPolicies(client: ElasticsearchClient): Promise> { + const options = { // we allow 404 since they may have no policies ignore: [404], }; - return await callAsCurrentUser('transport.request', params); + return client.ilm.getLifecycle({}, options); } -async function addLinkedIndices(callAsCurrentUser: LegacyAPICaller, policiesMap: PoliciesMap) { - if (policiesMap.status === 404) { - return policiesMap; - } - const params = { - method: 'GET', - path: '/*/_ilm/explain', +async function addLinkedIndices(client: ElasticsearchClient, policiesMap: PoliciesMap) { + const options = { // we allow 404 since they may have no policies ignore: [404], }; - const policyExplanation: { + const response = await client.ilm.explainLifecycle<{ indices: { [indexName: string]: IndexLifecyclePolicy }; - } = await callAsCurrentUser('transport.request', params); + }>({ index: '*' }, options); + const policyExplanation = response.body; Object.entries(policyExplanation.indices).forEach(([indexName, { policy }]) => { if (policy && policiesMap[policy]) { policiesMap[policy].linkedIndices = policiesMap[policy].linkedIndices || []; @@ -68,26 +57,29 @@ const querySchema = schema.object({ withIndices: schema.boolean({ defaultValue: false }), }); -export function registerFetchRoute({ router, license, lib }: RouteDependencies) { +export function registerFetchRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/policies'), validate: { query: querySchema } }, license.guardApiRoute(async (context, request, response) => { const query = request.query as typeof querySchema.type; const { withIndices } = query; - const { callAsCurrentUser } = context.core.elasticsearch.legacy.client; + const { asCurrentUser } = context.core.elasticsearch.client; try { - const policiesMap = await fetchPolicies(callAsCurrentUser); + const policiesResponse = await fetchPolicies(asCurrentUser); + if (policiesResponse.statusCode === 404) { + return response.ok({ body: [] }); + } + const { body: policiesMap } = policiesResponse; if (withIndices) { - await addLinkedIndices(callAsCurrentUser, policiesMap); + await addLinkedIndices(asCurrentUser, policiesMap); } - const okResponse = { body: formatPolicies(policiesMap) }; - return response.ok(okResponse); + return response.ok({ body: formatPolicies(policiesMap) }); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/snapshot_policies/register_fetch_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/snapshot_policies/register_fetch_route.ts index 7a52648e29ee8..869be3d557040 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/snapshot_policies/register_fetch_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/snapshot_policies/register_fetch_route.ts @@ -4,34 +4,30 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'kibana/server'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; -async function fetchSnapshotPolicies(callAsCurrentUser: LegacyAPICaller): Promise { - const params = { - method: 'GET', - path: '/_slm/policy', - }; - - return await callAsCurrentUser('transport.request', params); +async function fetchSnapshotPolicies(client: ElasticsearchClient): Promise { + const response = await client.slm.getLifecycle(); + return response.body; } -export function registerFetchRoute({ router, license, lib }: RouteDependencies) { +export function registerFetchRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/snapshot_policies'), validate: false }, license.guardApiRoute(async (context, request, response) => { try { const policiesByName = await fetchSnapshotPolicies( - context.core.elasticsearch.legacy.client.callAsCurrentUser + context.core.elasticsearch.client.asCurrentUser ); return response.ok({ body: Object.keys(policiesByName) }); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_add_policy_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_add_policy_route.ts index b47f346c6492d..7e7f3f1f725f8 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_add_policy_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_add_policy_route.ts @@ -6,7 +6,7 @@ import { merge } from 'lodash'; import { schema, TypeOf } from '@kbn/config-schema'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'kibana/server'; import { i18n } from '@kbn/i18n'; import { TemplateFromEs, TemplateSerialized } from '../../../../../index_management/common/types'; @@ -15,32 +15,37 @@ import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../../services'; async function getLegacyIndexTemplate( - callAsCurrentUser: LegacyAPICaller, + client: ElasticsearchClient, templateName: string ): Promise { - const response = await callAsCurrentUser('indices.getTemplate', { name: templateName }); - return response[templateName]; + const response = await client.indices.getTemplate({ name: templateName }); + return response.body[templateName]; } async function getIndexTemplate( - callAsCurrentUser: LegacyAPICaller, + client: ElasticsearchClient, templateName: string ): Promise { - const params = { - method: 'GET', - path: `/_index_template/${encodeURIComponent(templateName)}`, + const options = { // we allow 404 incase the user shutdown security in-between the check and now ignore: [404], }; - const { index_templates: templates } = await callAsCurrentUser<{ + const response = await client.indices.getIndexTemplate<{ index_templates: TemplateFromEs[]; - }>('transport.request', params); + }>( + { + name: templateName, + }, + options + ); + + const { index_templates: templates } = response.body; return templates?.find((template) => template.name === templateName)?.index_template; } async function updateIndexTemplate( - callAsCurrentUser: LegacyAPICaller, + client: ElasticsearchClient, isLegacy: boolean, templateName: string, policyName: string, @@ -56,8 +61,8 @@ async function updateIndexTemplate( }; const indexTemplate = isLegacy - ? await getLegacyIndexTemplate(callAsCurrentUser, templateName) - : await getIndexTemplate(callAsCurrentUser, templateName); + ? await getLegacyIndexTemplate(client, templateName) + : await getIndexTemplate(client, templateName); if (!indexTemplate) { return false; } @@ -71,15 +76,10 @@ async function updateIndexTemplate( }); } - const pathPrefix = isLegacy ? '/_template/' : '/_index_template/'; - const params = { - method: 'PUT', - path: `${pathPrefix}${encodeURIComponent(templateName)}`, - ignore: [404], - body: indexTemplate, - }; - - return await callAsCurrentUser('transport.request', params); + if (isLegacy) { + return client.indices.putTemplate({ name: templateName, body: indexTemplate }); + } + return client.indices.putIndexTemplate({ name: templateName, body: indexTemplate }); } const bodySchema = schema.object({ @@ -92,7 +92,7 @@ const querySchema = schema.object({ legacy: schema.maybe(schema.oneOf([schema.literal('true'), schema.literal('false')])), }); -export function registerAddPolicyRoute({ router, license, lib }: RouteDependencies) { +export function registerAddPolicyRoute({ router, license }: RouteDependencies) { router.post( { path: addBasePath('/template'), validate: { body: bodySchema, query: querySchema } }, license.guardApiRoute(async (context, request, response) => { @@ -101,7 +101,7 @@ export function registerAddPolicyRoute({ router, license, lib }: RouteDependenci const isLegacy = (request.query as TypeOf).legacy === 'true'; try { const updatedTemplate = await updateIndexTemplate( - context.core.elasticsearch.legacy.client.callAsCurrentUser, + context.core.elasticsearch.client.asCurrentUser, isLegacy, templateName, policyName, @@ -119,10 +119,10 @@ export function registerAddPolicyRoute({ router, license, lib }: RouteDependenci } return response.ok(); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_fetch_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_fetch_route.ts index b60892428b969..fbd102d3be1eb 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_fetch_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/templates/register_fetch_route.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; import { schema, TypeOf } from '@kbn/config-schema'; import { IndexSettings, @@ -60,42 +60,43 @@ function filterTemplates( } async function fetchTemplates( - callAsCurrentUser: LegacyAPICaller, + client: ElasticsearchClient, isLegacy: boolean ): Promise< { index_templates: TemplateFromEs[] } | { [templateName: string]: LegacyTemplateSerialized } > { - const params = { - method: 'GET', - path: isLegacy ? '/_template' : '/_index_template', + const options = { // we allow 404 incase the user shutdown security in-between the check and now ignore: [404], }; - return await callAsCurrentUser('transport.request', params); + const response = isLegacy + ? await client.indices.getTemplate({}, options) + : await client.indices.getIndexTemplate({}, options); + return response.body; } const querySchema = schema.object({ legacy: schema.maybe(schema.oneOf([schema.literal('true'), schema.literal('false')])), }); -export function registerFetchRoute({ router, license, lib }: RouteDependencies) { +export function registerFetchRoute({ router, license }: RouteDependencies) { router.get( { path: addBasePath('/templates'), validate: { query: querySchema } }, license.guardApiRoute(async (context, request, response) => { const isLegacy = (request.query as TypeOf).legacy === 'true'; try { const templates = await fetchTemplates( - context.core.elasticsearch.legacy.client.callAsCurrentUser, + context.core.elasticsearch.client.asCurrentUser, isLegacy ); const okResponse = { body: filterTemplates(templates, isLegacy) }; return response.ok(okResponse); } catch (e) { - if (lib.isEsError(e)) { + if (e.name === 'ResponseError') { return response.customError({ statusCode: e.statusCode, - body: e, + body: { message: e.body.error?.reason }, }); } // Case: default diff --git a/x-pack/plugins/index_lifecycle_management/server/shared_imports.ts b/x-pack/plugins/index_lifecycle_management/server/shared_imports.ts deleted file mode 100644 index 454beda5394c7..0000000000000 --- a/x-pack/plugins/index_lifecycle_management/server/shared_imports.ts +++ /dev/null @@ -1,7 +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. - */ - -export { isEsError } from '../../../../src/plugins/es_ui_shared/server'; diff --git a/x-pack/plugins/index_lifecycle_management/server/types.ts b/x-pack/plugins/index_lifecycle_management/server/types.ts index d3a2e0124949e..e34dc8e4b1a52 100644 --- a/x-pack/plugins/index_lifecycle_management/server/types.ts +++ b/x-pack/plugins/index_lifecycle_management/server/types.ts @@ -11,7 +11,6 @@ import { LicensingPluginSetup } from '../../licensing/server'; import { IndexManagementPluginSetup } from '../../index_management/server'; import { License } from './services'; import { IndexLifecycleManagementConfig } from './config'; -import { isEsError } from './shared_imports'; export interface Dependencies { licensing: LicensingPluginSetup; @@ -23,7 +22,4 @@ export interface RouteDependencies { router: IRouter; config: IndexLifecycleManagementConfig; license: License; - lib: { - isEsError: typeof isEsError; - }; } diff --git a/x-pack/test/api_integration/apis/management/index_lifecycle_management/indices.js b/x-pack/test/api_integration/apis/management/index_lifecycle_management/indices.js index af9ff4bf1bd9a..87a67d0b6f6e6 100644 --- a/x-pack/test/api_integration/apis/management/index_lifecycle_management/indices.js +++ b/x-pack/test/api_integration/apis/management/index_lifecycle_management/indices.js @@ -13,7 +13,7 @@ import { getPolicyPayload } from './fixtures'; export default function ({ getService }) { const supertest = getService('supertest'); - const es = getService('legacyEs'); + const es = getService('es'); const { getIndex, createIndex, cleanUp: cleanUpEsResources } = initElasticsearchHelpers(es); @@ -89,7 +89,7 @@ export default function ({ getService }) { // As there is no easy way to set the index in the ERROR state to be able to retry // we validate that the error returned *is* coming from the ES "_ilm/retry" endpoint const { body } = await retryPolicyOnIndex(indexName); - const expected = `[illegal_argument_exception] cannot retry an action for an index [${indexName}] that has not encountered an error when running a Lifecycle Policy`; + const expected = `cannot retry an action for an index [${indexName}] that has not encountered an error when running a Lifecycle Policy`; expect(body.message).to.be(expected); }); }); diff --git a/x-pack/test/api_integration/apis/management/index_lifecycle_management/lib/elasticsearch.js b/x-pack/test/api_integration/apis/management/index_lifecycle_management/lib/elasticsearch.js index dcfc86d1d03b9..358e54d8738f6 100644 --- a/x-pack/test/api_integration/apis/management/index_lifecycle_management/lib/elasticsearch.js +++ b/x-pack/test/api_integration/apis/management/index_lifecycle_management/lib/elasticsearch.js @@ -15,7 +15,7 @@ export const initElasticsearchHelpers = (es) => { let templatesCreated = []; // Indices - const getIndex = (index) => es.indices.get({ index }); + const getIndex = (index) => es.indices.get({ index }).then(({ body }) => body); const createIndex = (index = getRandomString()) => { indicesCreated.push(index); @@ -54,7 +54,7 @@ export const initElasticsearchHelpers = (es) => { const cleanUp = () => Promise.all([deleteAllIndices(), deleteAllTemplates()]); - const getNodesStats = () => es.nodes.stats(); + const getNodesStats = () => es.nodes.stats().then(({ body }) => body); return { getIndex, diff --git a/x-pack/test/api_integration/apis/management/index_lifecycle_management/nodes.js b/x-pack/test/api_integration/apis/management/index_lifecycle_management/nodes.js index bb35f6fd96429..c859037597821 100644 --- a/x-pack/test/api_integration/apis/management/index_lifecycle_management/nodes.js +++ b/x-pack/test/api_integration/apis/management/index_lifecycle_management/nodes.js @@ -13,7 +13,7 @@ import { initElasticsearchHelpers } from './lib'; export default function ({ getService }) { const supertest = getService('supertest'); - const es = getService('legacyEs'); + const es = getService('es'); const { getNodesStats } = initElasticsearchHelpers(es); const { loadNodes, getNodeDetails } = registerHelpers({ supertest }); diff --git a/x-pack/test/api_integration/apis/management/index_lifecycle_management/policies.js b/x-pack/test/api_integration/apis/management/index_lifecycle_management/policies.js index fad7fb848122d..1589baabb1ded 100644 --- a/x-pack/test/api_integration/apis/management/index_lifecycle_management/policies.js +++ b/x-pack/test/api_integration/apis/management/index_lifecycle_management/policies.js @@ -15,7 +15,7 @@ import { DEFAULT_POLICY_NAME } from './constants'; export default function ({ getService }) { const supertest = getService('supertest'); - const es = getService('legacyEs'); + const es = getService('es'); const { createIndex, cleanUp: cleanUpEsResources } = initElasticsearchHelpers(es); diff --git a/x-pack/test/api_integration/apis/management/index_lifecycle_management/templates.js b/x-pack/test/api_integration/apis/management/index_lifecycle_management/templates.js index 7fb9b35b8475e..9e0d32b96af98 100644 --- a/x-pack/test/api_integration/apis/management/index_lifecycle_management/templates.js +++ b/x-pack/test/api_integration/apis/management/index_lifecycle_management/templates.js @@ -12,7 +12,7 @@ import { registerHelpers as registerPoliciesHelpers } from './policies.helpers'; export default function ({ getService }) { const supertest = getService('supertest'); - const es = getService('legacyEs'); + const es = getService('es'); const { createIndexTemplate, cleanUp: cleanUpEsResources } = initElasticsearchHelpers(es);