Skip to content

Commit

Permalink
[ILM] Replace legacy ES client with the new client (#78416) (#80176)
Browse files Browse the repository at this point in the history
* [ILM] Replace legacy ES client with the new client

* [ILM] Fix type check errors

* [ILM] Fix api integration test

* [ILM] Update error handling and integration test for the new client

* [ILM] Fix api integration test

* [ILM] Fix api integration test

* [ILM] Add a todo comment for Index Management plugin extension

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
yuliacech and kibanamachine committed Oct 13, 2020
1 parent 8e37d9f commit 07e5e8a
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 214 deletions.
5 changes: 1 addition & 4 deletions x-pack/plugins/index_lifecycle_management/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Index[]> => {
if (!indicesList || !indicesList.length) {
Expand Down Expand Up @@ -99,9 +99,6 @@ export class IndexLifecycleManagementServerPlugin implements Plugin<void, void,
router,
config,
license: this.license,
lib: {
isEsError,
},
});

if (config.ui.enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +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 addLifecyclePolicy(
callAsCurrentUser: LegacyAPICaller,
client: ElasticsearchClient,
indexName: string,
policyName: string,
alias: string
) {
const params = {
method: 'PUT',
path: `/${encodeURIComponent(indexName)}/_settings`,
body: {
lifecycle: {
name: policyName,
rollover_alias: alias,
},
const body = {
lifecycle: {
name: policyName,
rollover_alias: alias,
},
};

return callAsCurrentUser('transport.request', params);
return client.indices.putSettings({ index: indexName, body });
}

const bodySchema = schema.object({
Expand All @@ -36,7 +32,7 @@ const bodySchema = schema.object({
alias: schema.maybe(schema.string()),
});

export function registerAddPolicyRoute({ router, license, lib }: RouteDependencies) {
export function registerAddPolicyRoute({ router, license }: RouteDependencies) {
router.post(
{ path: addBasePath('/index/add'), validate: { body: bodySchema } },
license.guardApiRoute(async (context, request, response) => {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -29,24 +26,21 @@ 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) => {
const body = request.body as typeof bodySchema.type;
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -29,24 +27,21 @@ 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) => {
const body = request.body as typeof bodySchema.type;
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { schema } from '@kbn/config-schema';
import { LegacyAPICaller } from 'src/core/server';

import { RouteDependencies } from '../../../types';
import { addBasePath } from '../../../services';
Expand All @@ -26,36 +25,26 @@ function findMatchingNodes(stats: any, nodeAttrs: string): any {
}, []);
}

async function fetchNodeStats(callAsCurrentUser: LegacyAPICaller): Promise<any> {
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) => {
const params = request.params as typeof paramsSchema.type;
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -47,15 +45,7 @@ function convertStatsIntoList(
);
}

async function fetchNodeStats(callAsCurrentUser: LegacyAPICaller): Promise<any> {
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[] = [
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
async function createPolicy(client: ElasticsearchClient, name: string, phases: any): Promise<any> {
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());
Expand Down Expand Up @@ -141,25 +134,21 @@ 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) => {
const body = request.body as typeof bodySchema.type;
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
Expand Down
Loading

0 comments on commit 07e5e8a

Please sign in to comment.