Skip to content

Commit 7e9d797

Browse files
authored
[Index management] Server-side NP ready (#56829)
1 parent bb7e152 commit 7e9d797

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1148
-498
lines changed

x-pack/legacy/plugins/cross_cluster_replication/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { PLUGIN } from './common/constants';
99
import { registerLicenseChecker } from './server/lib/register_license_checker';
1010
import { registerRoutes } from './server/routes/register_routes';
1111
import { ccrDataEnricher } from './cross_cluster_replication_data';
12-
import { addIndexManagementDataEnricher } from '../index_management/server/index_management_data';
12+
1313
export function crossClusterReplication(kibana) {
1414
return new kibana.Plugin({
1515
id: PLUGIN.ID,
@@ -49,8 +49,13 @@ export function crossClusterReplication(kibana) {
4949
init: function initCcrPlugin(server) {
5050
registerLicenseChecker(server);
5151
registerRoutes(server);
52-
if (server.config().get('xpack.ccr.ui.enabled')) {
53-
addIndexManagementDataEnricher(ccrDataEnricher);
52+
53+
if (
54+
server.config().get('xpack.ccr.ui.enabled') &&
55+
server.plugins.index_management &&
56+
server.plugins.index_management.addIndexManagementDataEnricher
57+
) {
58+
server.plugins.index_management.addIndexManagementDataEnricher(ccrDataEnricher);
5459
}
5560
},
5661
});

x-pack/legacy/plugins/index_management/common/constants/plugin.ts

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

7-
import { LICENSE_TYPE_BASIC } from '../../../../common/constants';
7+
import { LicenseType } from '../../../../../plugins/licensing/common/types';
8+
9+
const basicLicense: LicenseType = 'basic';
810

911
export const PLUGIN = {
10-
ID: 'index_management',
12+
id: 'index_management',
13+
minimumLicenseType: basicLicense,
1114
getI18nName: (i18n: any): string =>
1215
i18n.translate('xpack.idxMgmt.appTitle', {
1316
defaultMessage: 'Index Management',
1417
}),
15-
MINIMUM_LICENSE_REQUIRED: LICENSE_TYPE_BASIC,
1618
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export { PLUGIN, API_BASE_PATH } from './constants';

x-pack/legacy/plugins/index_management/index.ts

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55
*/
66

77
import { resolve } from 'path';
8-
import { i18n } from '@kbn/i18n';
98
import { Legacy } from 'kibana';
10-
import { createRouter } from '../../server/lib/create_router';
11-
import { registerLicenseChecker } from '../../server/lib/register_license_checker';
12-
import { PLUGIN, API_BASE_PATH } from './common/constants';
13-
import { LegacySetup } from './server/plugin';
14-
import { plugin as initServerPlugin } from './server';
9+
import { PLUGIN } from './common/constants';
10+
import { plugin as initServerPlugin, Dependencies } from './server';
1511

1612
export type ServerFacade = Legacy.Server;
1713

1814
export function indexManagement(kibana: any) {
1915
return new kibana.Plugin({
20-
id: PLUGIN.ID,
16+
id: PLUGIN.id,
2117
configPrefix: 'xpack.index_management',
2218
publicDir: resolve(__dirname, 'public'),
2319
require: ['kibana', 'elasticsearch', 'xpack_main'],
@@ -29,32 +25,15 @@ export function indexManagement(kibana: any) {
2925

3026
init(server: ServerFacade) {
3127
const coreSetup = server.newPlatform.setup.core;
32-
33-
const pluginsSetup = {};
34-
35-
const __LEGACY: LegacySetup = {
36-
router: createRouter(server, PLUGIN.ID, `${API_BASE_PATH}/`),
37-
plugins: {
38-
license: {
39-
registerLicenseChecker: registerLicenseChecker.bind(
40-
null,
41-
server,
42-
PLUGIN.ID,
43-
PLUGIN.getI18nName(i18n),
44-
PLUGIN.MINIMUM_LICENSE_REQUIRED as 'basic'
45-
),
46-
},
47-
elasticsearch: server.plugins.elasticsearch,
48-
},
28+
const coreInitializerContext = server.newPlatform.coreContext;
29+
const pluginsSetup: Dependencies = {
30+
licensing: server.newPlatform.setup.plugins.licensing as any,
4931
};
5032

51-
const serverPlugin = initServerPlugin();
52-
const indexMgmtSetup = serverPlugin.setup(coreSetup, pluginsSetup, __LEGACY);
33+
const serverPlugin = initServerPlugin(coreInitializerContext as any);
34+
const serverPublicApi = serverPlugin.setup(coreSetup, pluginsSetup);
5335

54-
server.expose(
55-
'addIndexManagementDataEnricher',
56-
indexMgmtSetup.addIndexManagementDataEnricher
57-
);
36+
server.expose('addIndexManagementDataEnricher', serverPublicApi.indexDataEnricher.add);
5837
},
5938
});
6039
}

x-pack/legacy/plugins/index_management/server/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import { IndexMgmtPlugin } from './plugin';
76

8-
export function plugin() {
9-
return new IndexMgmtPlugin();
10-
}
7+
import { PluginInitializerContext } from 'src/core/server';
8+
import { IndexMgmtServerPlugin } from './plugin';
9+
10+
export const plugin = (ctx: PluginInitializerContext) => new IndexMgmtServerPlugin(ctx);
11+
12+
export { Dependencies } from './types';

x-pack/legacy/plugins/index_management/server/index_management_data.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

x-pack/legacy/plugins/index_management/server/lib/fetch_indices.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
import { IndexDataEnricher } from '../services';
7+
import { Index, CallAsCurrentUser } from '../types';
68
import { fetchAliases } from './fetch_aliases';
7-
import { getIndexManagementDataEnrichers } from '../index_management_data';
9+
810
interface Hit {
911
health: string;
1012
status: string;
@@ -27,22 +29,7 @@ interface Params {
2729
index?: string[];
2830
}
2931

30-
const enrichResponse = async (response: any, callWithRequest: any) => {
31-
let enrichedResponse = response;
32-
const dataEnrichers = getIndexManagementDataEnrichers();
33-
for (let i = 0; i < dataEnrichers.length; i++) {
34-
const dataEnricher = dataEnrichers[i];
35-
try {
36-
const dataEnricherResponse = await dataEnricher(enrichedResponse, callWithRequest);
37-
enrichedResponse = dataEnricherResponse;
38-
} catch (e) {
39-
// silently swallow enricher response errors
40-
}
41-
}
42-
return enrichedResponse;
43-
};
44-
45-
function formatHits(hits: Hit[], aliases: Aliases) {
32+
function formatHits(hits: Hit[], aliases: Aliases): Index[] {
4633
return hits.map((hit: Hit) => {
4734
return {
4835
health: hit.health,
@@ -59,7 +46,7 @@ function formatHits(hits: Hit[], aliases: Aliases) {
5946
});
6047
}
6148

62-
async function fetchIndicesCall(callWithRequest: any, indexNames?: string[]) {
49+
async function fetchIndicesCall(callAsCurrentUser: CallAsCurrentUser, indexNames?: string[]) {
6350
const params: Params = {
6451
format: 'json',
6552
h: 'health,status,index,uuid,pri,rep,docs.count,sth,store.size',
@@ -69,13 +56,17 @@ async function fetchIndicesCall(callWithRequest: any, indexNames?: string[]) {
6956
params.index = indexNames;
7057
}
7158

72-
return await callWithRequest('cat.indices', params);
59+
return await callAsCurrentUser('cat.indices', params);
7360
}
7461

75-
export const fetchIndices = async (callWithRequest: any, indexNames?: string[]) => {
76-
const aliases = await fetchAliases(callWithRequest);
77-
const hits = await fetchIndicesCall(callWithRequest, indexNames);
78-
let response = formatHits(hits, aliases);
79-
response = await enrichResponse(response, callWithRequest);
80-
return response;
62+
export const fetchIndices = async (
63+
callAsCurrentUser: CallAsCurrentUser,
64+
indexDataEnricher: IndexDataEnricher,
65+
indexNames?: string[]
66+
) => {
67+
const aliases = await fetchAliases(callAsCurrentUser);
68+
const hits = await fetchIndicesCall(callAsCurrentUser, indexNames);
69+
const indices = formatHits(hits, aliases);
70+
71+
return await indexDataEnricher.enrichIndices(indices, callAsCurrentUser);
8172
};

x-pack/legacy/plugins/index_management/server/lib/get_managed_templates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
// Cloud has its own system for managing templates and we want to make
88
// this clear in the UI when a template is used in a Cloud deployment.
99
export const getManagedTemplatePrefix = async (
10-
callWithInternalUser: any
10+
callAsCurrentUser: any
1111
): Promise<string | undefined> => {
1212
try {
13-
const { persistent, transient, defaults } = await callWithInternalUser('cluster.getSettings', {
13+
const { persistent, transient, defaults } = await callAsCurrentUser('cluster.getSettings', {
1414
filterPath: '*.*managed_index_templates',
1515
flatSettings: true,
1616
includeDefaults: true,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import * as legacyElasticsearch from 'elasticsearch';
8+
9+
const esErrorsParent = legacyElasticsearch.errors._Abstract;
10+
11+
export function isEsError(err: Error) {
12+
return err instanceof esErrorsParent;
13+
}

x-pack/legacy/plugins/index_management/server/plugin.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,67 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import { CoreSetup } from 'src/core/server';
7-
import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch';
8-
import { Router } from '../../../server/lib/create_router';
9-
import { addIndexManagementDataEnricher } from './index_management_data';
10-
import { registerIndicesRoutes } from './routes/api/indices';
11-
import { registerTemplateRoutes } from './routes/api/templates';
12-
import { registerMappingRoute } from './routes/api/mapping';
13-
import { registerSettingsRoutes } from './routes/api/settings';
14-
import { registerStatsRoute } from './routes/api/stats';
15-
16-
export interface LegacySetup {
17-
router: Router;
18-
plugins: {
19-
elasticsearch: ElasticsearchPlugin;
20-
license: {
21-
registerLicenseChecker: () => void;
22-
};
23-
};
24-
}
6+
import { i18n } from '@kbn/i18n';
7+
import { CoreSetup, Plugin, Logger, PluginInitializerContext } from 'src/core/server';
8+
9+
import { PLUGIN } from '../common';
10+
import { Dependencies } from './types';
11+
import { ApiRoutes } from './routes';
12+
import { License, IndexDataEnricher } from './services';
13+
import { isEsError } from './lib/is_es_error';
2514

2615
export interface IndexMgmtSetup {
27-
addIndexManagementDataEnricher: (enricher: any) => void;
16+
indexDataEnricher: {
17+
add: IndexDataEnricher['add'];
18+
};
2819
}
2920

30-
export class IndexMgmtPlugin {
31-
public setup(core: CoreSetup, plugins: {}, __LEGACY: LegacySetup): IndexMgmtSetup {
32-
const serverFacade = {
33-
plugins: {
34-
elasticsearch: __LEGACY.plugins.elasticsearch,
35-
},
36-
};
21+
export class IndexMgmtServerPlugin implements Plugin<IndexMgmtSetup, void, any, any> {
22+
private readonly apiRoutes: ApiRoutes;
23+
private readonly license: License;
24+
private readonly logger: Logger;
25+
private readonly indexDataEnricher: IndexDataEnricher;
3726

38-
__LEGACY.plugins.license.registerLicenseChecker();
27+
constructor({ logger }: PluginInitializerContext) {
28+
this.logger = logger.get();
29+
this.apiRoutes = new ApiRoutes();
30+
this.license = new License();
31+
this.indexDataEnricher = new IndexDataEnricher();
32+
}
33+
34+
setup({ http }: CoreSetup, { licensing }: Dependencies): IndexMgmtSetup {
35+
const router = http.createRouter();
3936

40-
registerIndicesRoutes(__LEGACY.router);
41-
registerTemplateRoutes(__LEGACY.router, serverFacade);
42-
registerSettingsRoutes(__LEGACY.router);
43-
registerStatsRoute(__LEGACY.router);
44-
registerMappingRoute(__LEGACY.router);
37+
this.license.setup(
38+
{
39+
pluginId: PLUGIN.id,
40+
minimumLicenseType: PLUGIN.minimumLicenseType,
41+
defaultErrorMessage: i18n.translate('xpack.idxMgmt.licenseCheckErrorMessage', {
42+
defaultMessage: 'License check failed',
43+
}),
44+
},
45+
{
46+
licensing,
47+
logger: this.logger,
48+
}
49+
);
50+
51+
this.apiRoutes.setup({
52+
router,
53+
license: this.license,
54+
indexDataEnricher: this.indexDataEnricher,
55+
lib: {
56+
isEsError,
57+
},
58+
});
4559

4660
return {
47-
addIndexManagementDataEnricher,
61+
indexDataEnricher: {
62+
add: this.indexDataEnricher.add.bind(this.indexDataEnricher),
63+
},
4864
};
4965
}
66+
67+
start() {}
68+
stop() {}
5069
}

0 commit comments

Comments
 (0)