Skip to content

Commit 21c3e04

Browse files
[ILM] Replace legacy ES client with the new client (#78416)
* [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 <[email protected]>
1 parent f532e9d commit 21c3e04

File tree

19 files changed

+137
-214
lines changed

19 files changed

+137
-214
lines changed

x-pack/plugins/index_lifecycle_management/server/plugin.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import { Dependencies } from './types';
2222
import { registerApiRoutes } from './routes';
2323
import { License } from './services';
2424
import { IndexLifecycleManagementConfig } from './config';
25-
import { isEsError } from './shared_imports';
2625

2726
const indexLifecycleDataEnricher = async (
2827
indicesList: IndexWithoutIlm[],
28+
// TODO replace deprecated ES client after Index Management is updated
2929
callAsCurrentUser: LegacyAPICaller
3030
): Promise<Index[]> => {
3131
if (!indicesList || !indicesList.length) {
@@ -99,9 +99,6 @@ export class IndexLifecycleManagementServerPlugin implements Plugin<void, void,
9999
router,
100100
config,
101101
license: this.license,
102-
lib: {
103-
isEsError,
104-
},
105102
});
106103

107104
if (config.ui.enabled) {

x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_add_policy_route.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,25 @@
55
*/
66

77
import { schema } from '@kbn/config-schema';
8-
import { LegacyAPICaller } from 'src/core/server';
8+
import { ElasticsearchClient } from 'kibana/server';
99

1010
import { RouteDependencies } from '../../../types';
1111
import { addBasePath } from '../../../services';
1212

1313
async function addLifecyclePolicy(
14-
callAsCurrentUser: LegacyAPICaller,
14+
client: ElasticsearchClient,
1515
indexName: string,
1616
policyName: string,
1717
alias: string
1818
) {
19-
const params = {
20-
method: 'PUT',
21-
path: `/${encodeURIComponent(indexName)}/_settings`,
22-
body: {
23-
lifecycle: {
24-
name: policyName,
25-
rollover_alias: alias,
26-
},
19+
const body = {
20+
lifecycle: {
21+
name: policyName,
22+
rollover_alias: alias,
2723
},
2824
};
2925

30-
return callAsCurrentUser('transport.request', params);
26+
return client.indices.putSettings({ index: indexName, body });
3127
}
3228

3329
const bodySchema = schema.object({
@@ -36,7 +32,7 @@ const bodySchema = schema.object({
3632
alias: schema.maybe(schema.string()),
3733
});
3834

39-
export function registerAddPolicyRoute({ router, license, lib }: RouteDependencies) {
35+
export function registerAddPolicyRoute({ router, license }: RouteDependencies) {
4036
router.post(
4137
{ path: addBasePath('/index/add'), validate: { body: bodySchema } },
4238
license.guardApiRoute(async (context, request, response) => {
@@ -45,17 +41,17 @@ export function registerAddPolicyRoute({ router, license, lib }: RouteDependenci
4541

4642
try {
4743
await addLifecyclePolicy(
48-
context.core.elasticsearch.legacy.client.callAsCurrentUser,
44+
context.core.elasticsearch.client.asCurrentUser,
4945
indexName,
5046
policyName,
5147
alias
5248
);
5349
return response.ok();
5450
} catch (e) {
55-
if (lib.isEsError(e)) {
51+
if (e.name === 'ResponseError') {
5652
return response.customError({
5753
statusCode: e.statusCode,
58-
body: e,
54+
body: { message: e.body.error?.reason },
5955
});
6056
}
6157
// Case: default

x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_remove_route.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@
55
*/
66

77
import { schema } from '@kbn/config-schema';
8-
import { LegacyAPICaller } from 'src/core/server';
8+
import { ElasticsearchClient } from 'kibana/server';
99

1010
import { RouteDependencies } from '../../../types';
1111
import { addBasePath } from '../../../services';
1212

13-
async function removeLifecycle(callAsCurrentUser: LegacyAPICaller, indexNames: string[]) {
13+
async function removeLifecycle(client: ElasticsearchClient, indexNames: string[]) {
14+
const options = {
15+
ignore: [404],
16+
};
1417
const responses = [];
1518
for (let i = 0; i < indexNames.length; i++) {
1619
const indexName = indexNames[i];
17-
const params = {
18-
method: 'POST',
19-
path: `/${encodeURIComponent(indexName)}/_ilm/remove`,
20-
ignore: [404],
21-
};
22-
23-
responses.push(callAsCurrentUser('transport.request', params));
20+
responses.push(client.ilm.removePolicy({ index: indexName }, options));
2421
}
2522
return Promise.all(responses);
2623
}
@@ -29,24 +26,21 @@ const bodySchema = schema.object({
2926
indexNames: schema.arrayOf(schema.string()),
3027
});
3128

32-
export function registerRemoveRoute({ router, license, lib }: RouteDependencies) {
29+
export function registerRemoveRoute({ router, license }: RouteDependencies) {
3330
router.post(
3431
{ path: addBasePath('/index/remove'), validate: { body: bodySchema } },
3532
license.guardApiRoute(async (context, request, response) => {
3633
const body = request.body as typeof bodySchema.type;
3734
const { indexNames } = body;
3835

3936
try {
40-
await removeLifecycle(
41-
context.core.elasticsearch.legacy.client.callAsCurrentUser,
42-
indexNames
43-
);
37+
await removeLifecycle(context.core.elasticsearch.client.asCurrentUser, indexNames);
4438
return response.ok();
4539
} catch (e) {
46-
if (lib.isEsError(e)) {
40+
if (e.name === 'ResponseError') {
4741
return response.customError({
4842
statusCode: e.statusCode,
49-
body: e,
43+
body: { message: e.body.error?.reason },
5044
});
5145
}
5246
// Case: default

x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_retry_route.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
*/
66

77
import { schema } from '@kbn/config-schema';
8-
import { LegacyAPICaller } from 'src/core/server';
8+
import { ElasticsearchClient } from 'kibana/server';
99

1010
import { RouteDependencies } from '../../../types';
1111
import { addBasePath } from '../../../services';
1212

13-
async function retryLifecycle(callAsCurrentUser: LegacyAPICaller, indexNames: string[]) {
13+
async function retryLifecycle(client: ElasticsearchClient, indexNames: string[]) {
14+
const options = {
15+
ignore: [404],
16+
};
1417
const responses = [];
1518
for (let i = 0; i < indexNames.length; i++) {
1619
const indexName = indexNames[i];
17-
const params = {
18-
method: 'POST',
19-
path: `/${encodeURIComponent(indexName)}/_ilm/retry`,
20-
ignore: [404],
21-
};
2220

23-
responses.push(callAsCurrentUser('transport.request', params));
21+
responses.push(client.ilm.retry({ index: indexName }, options));
2422
}
2523
return Promise.all(responses);
2624
}
@@ -29,24 +27,21 @@ const bodySchema = schema.object({
2927
indexNames: schema.arrayOf(schema.string()),
3028
});
3129

32-
export function registerRetryRoute({ router, license, lib }: RouteDependencies) {
30+
export function registerRetryRoute({ router, license }: RouteDependencies) {
3331
router.post(
3432
{ path: addBasePath('/index/retry'), validate: { body: bodySchema } },
3533
license.guardApiRoute(async (context, request, response) => {
3634
const body = request.body as typeof bodySchema.type;
3735
const { indexNames } = body;
3836

3937
try {
40-
await retryLifecycle(
41-
context.core.elasticsearch.legacy.client.callAsCurrentUser,
42-
indexNames
43-
);
38+
await retryLifecycle(context.core.elasticsearch.client.asCurrentUser, indexNames);
4439
return response.ok();
4540
} catch (e) {
46-
if (lib.isEsError(e)) {
41+
if (e.name === 'ResponseError') {
4742
return response.customError({
4843
statusCode: e.statusCode,
49-
body: e,
44+
body: { message: e.body.error?.reason },
5045
});
5146
}
5247
// Case: default

x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_details_route.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

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

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

29-
async function fetchNodeStats(callAsCurrentUser: LegacyAPICaller): Promise<any> {
30-
const params = {
31-
format: 'json',
32-
};
33-
34-
return await callAsCurrentUser('nodes.stats', params);
35-
}
36-
3728
const paramsSchema = schema.object({
3829
nodeAttrs: schema.string(),
3930
});
4031

41-
export function registerDetailsRoute({ router, license, lib }: RouteDependencies) {
32+
export function registerDetailsRoute({ router, license }: RouteDependencies) {
4233
router.get(
4334
{ path: addBasePath('/nodes/{nodeAttrs}/details'), validate: { params: paramsSchema } },
4435
license.guardApiRoute(async (context, request, response) => {
4536
const params = request.params as typeof paramsSchema.type;
4637
const { nodeAttrs } = params;
4738

4839
try {
49-
const stats = await fetchNodeStats(
50-
context.core.elasticsearch.legacy.client.callAsCurrentUser
51-
);
52-
const okResponse = { body: findMatchingNodes(stats, nodeAttrs) };
40+
const statsResponse = await context.core.elasticsearch.client.asCurrentUser.nodes.stats();
41+
const okResponse = { body: findMatchingNodes(statsResponse.body, nodeAttrs) };
5342
return response.ok(okResponse);
5443
} catch (e) {
55-
if (lib.isEsError(e)) {
44+
if (e.name === 'ResponseError') {
5645
return response.customError({
5746
statusCode: e.statusCode,
58-
body: e,
47+
body: { message: e.body.error?.reason },
5948
});
6049
}
6150
// Case: default

x-pack/plugins/index_lifecycle_management/server/routes/api/nodes/register_list_route.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { LegacyAPICaller } from 'src/core/server';
8-
97
import { ListNodesRouteResponse, NodeDataRole } from '../../../../common/types';
108

119
import { RouteDependencies } from '../../../types';
@@ -47,15 +45,7 @@ function convertStatsIntoList(
4745
);
4846
}
4947

50-
async function fetchNodeStats(callAsCurrentUser: LegacyAPICaller): Promise<any> {
51-
const params = {
52-
format: 'json',
53-
};
54-
55-
return await callAsCurrentUser('nodes.stats', params);
56-
}
57-
58-
export function registerListRoute({ router, config, license, lib }: RouteDependencies) {
48+
export function registerListRoute({ router, config, license }: RouteDependencies) {
5949
const { filteredNodeAttributes } = config;
6050

6151
const NODE_ATTRS_KEYS_TO_IGNORE: string[] = [
@@ -74,16 +64,19 @@ export function registerListRoute({ router, config, license, lib }: RouteDepende
7464
{ path: addBasePath('/nodes/list'), validate: false },
7565
license.guardApiRoute(async (context, request, response) => {
7666
try {
77-
const stats = await fetchNodeStats(
78-
context.core.elasticsearch.legacy.client.callAsCurrentUser
67+
const statsResponse = await context.core.elasticsearch.client.asCurrentUser.nodes.stats<
68+
Stats
69+
>();
70+
const body: ListNodesRouteResponse = convertStatsIntoList(
71+
statsResponse.body,
72+
disallowedNodeAttributes
7973
);
80-
const body: ListNodesRouteResponse = convertStatsIntoList(stats, disallowedNodeAttributes);
8174
return response.ok({ body });
8275
} catch (e) {
83-
if (lib.isEsError(e)) {
76+
if (e.name === 'ResponseError') {
8477
return response.customError({
8578
statusCode: e.statusCode,
86-
body: e,
79+
body: { message: e.body.error?.reason },
8780
});
8881
}
8982
// Case: default

x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_create_route.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,22 @@
55
*/
66

77
import { schema } from '@kbn/config-schema';
8-
import { LegacyAPICaller } from 'src/core/server';
8+
import { ElasticsearchClient } from 'kibana/server';
99

1010
import { RouteDependencies } from '../../../types';
1111
import { addBasePath } from '../../../services';
1212

13-
async function createPolicy(
14-
callAsCurrentUser: LegacyAPICaller,
15-
name: string,
16-
phases: any
17-
): Promise<any> {
13+
async function createPolicy(client: ElasticsearchClient, name: string, phases: any): Promise<any> {
1814
const body = {
1915
policy: {
2016
phases,
2117
},
2218
};
23-
const params = {
24-
method: 'PUT',
25-
path: `/_ilm/policy/${encodeURIComponent(name)}`,
19+
const options = {
2620
ignore: [404],
27-
body,
2821
};
2922

30-
return await callAsCurrentUser('transport.request', params);
23+
return client.ilm.putLifecycle({ policy: name, body }, options);
3124
}
3225

3326
const minAgeSchema = schema.maybe(schema.string());
@@ -141,25 +134,21 @@ const bodySchema = schema.object({
141134
}),
142135
});
143136

144-
export function registerCreateRoute({ router, license, lib }: RouteDependencies) {
137+
export function registerCreateRoute({ router, license }: RouteDependencies) {
145138
router.post(
146139
{ path: addBasePath('/policies'), validate: { body: bodySchema } },
147140
license.guardApiRoute(async (context, request, response) => {
148141
const body = request.body as typeof bodySchema.type;
149142
const { name, phases } = body;
150143

151144
try {
152-
await createPolicy(
153-
context.core.elasticsearch.legacy.client.callAsCurrentUser,
154-
name,
155-
phases
156-
);
145+
await createPolicy(context.core.elasticsearch.client.asCurrentUser, name, phases);
157146
return response.ok();
158147
} catch (e) {
159-
if (lib.isEsError(e)) {
148+
if (e.name === 'ResponseError') {
160149
return response.customError({
161150
statusCode: e.statusCode,
162-
body: e,
151+
body: { message: e.body.error?.reason },
163152
});
164153
}
165154
// Case: default

0 commit comments

Comments
 (0)