Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ILM] Replace legacy ES client with the new client #78416

Merged
merged 13 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this TODO! It may also be worth adding a comment to #73973 as a reminder to circle back around to this.

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