Skip to content

Commit

Permalink
server np migration phase i
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Oct 25, 2019
1 parent 5563ee9 commit 739edd8
Show file tree
Hide file tree
Showing 32 changed files with 341 additions and 146 deletions.
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/cross_cluster_replication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PLUGIN } from './common/constants';
import { registerLicenseChecker } from './server/lib/register_license_checker';
import { registerRoutes } from './server/routes/register_routes';
import { ccrDataEnricher } from './cross_cluster_replication_data';
import { addIndexManagementDataEnricher } from '../index_management/index_management_data';
import { addIndexManagementDataEnricher } from '../index_management/server/index_management_data';
export function crossClusterReplication(kibana) {
return new kibana.Plugin({
id: PLUGIN.ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { i18n } from '@kbn/i18n';
import { LICENSE_TYPE_BASIC } from '../../../../common/constants';

export const PLUGIN = {
ID: 'index_management',
NAME: i18n.translate('xpack.idxMgmt.appTitle', {
defaultMessage: 'Index Management',
}),
getI18nName: (i18n: any): string =>
i18n.translate('xpack.idxMgmt.appTitle', {
defaultMessage: 'Index Management',
}),
MINIMUM_LICENSE_REQUIRED: LICENSE_TYPE_BASIC,
};
41 changes: 0 additions & 41 deletions x-pack/legacy/plugins/index_management/index.js

This file was deleted.

60 changes: 60 additions & 0 deletions x-pack/legacy/plugins/index_management/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.
*/

import { resolve } from 'path';
import { i18n } from '@kbn/i18n';
import { Legacy } from 'kibana';
import { createRouter } from '../../server/lib/create_router';
import { registerLicenseChecker } from '../../server/lib/register_license_checker';
import { PLUGIN } from './common/constants';
import { LegacySetup } from './server/plugin';
import { plugin as initServerPlugin } from './server';

export type ServerFacade = Legacy.Server;

export function indexManagement(kibana: any) {
return new kibana.Plugin({
id: PLUGIN.ID,
configPrefix: 'xpack.index_management',
publicDir: resolve(__dirname, 'public'),
require: ['kibana', 'elasticsearch', 'xpack_main'],

uiExports: {
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
managementSections: ['plugins/index_management'],
},

init(server: ServerFacade) {
const coreSetup = server.newPlatform.setup.core;

const pluginsSetup = {};

const __LEGACY: LegacySetup = {
router: createRouter(server, PLUGIN.ID, '/api/index_management/'),
plugins: {
license: {
registerLicenseChecker: registerLicenseChecker.bind(
null,
server,
PLUGIN.ID,
PLUGIN.getI18nName(i18n),
PLUGIN.MINIMUM_LICENSE_REQUIRED as 'basic'
),
},
elasticsearch: server.plugins.elasticsearch,
},
};

const serverPlugin = initServerPlugin();
const indexMgmtSetup = serverPlugin.setup(coreSetup, pluginsSetup, __LEGACY);

server.expose(
'addIndexManagementDataEnricher',
indexMgmtSetup.addIndexManagementDataEnricher
);
},
});
}
10 changes: 10 additions & 0 deletions x-pack/legacy/plugins/index_management/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/
import { IndexMgmtPlugin } from './plugin';

export function plugin() {
return new IndexMgmtPlugin();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

const indexManagementDataEnrichers = [];
export const addIndexManagementDataEnricher = (enricher) => {
const indexManagementDataEnrichers: any[] = [];

export const addIndexManagementDataEnricher = (enricher: any) => {
indexManagementDataEnrichers.push(enricher);
};
export const getIndexManagementDataEnrichers = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('fetching aliases', () => {
{ index: 'test3Index', alias: 'test2Alias' },
{ index: 'test3Index', alias: 'test3Alias' },
];
const mockCallWithRequest = sinon.spy(() => {return retVal;});
const mockCallWithRequest = sinon.spy(() => {
return retVal;
});

const results = await fetchFn(mockCallWithRequest);

Expand All @@ -28,11 +30,12 @@ describe('fetching aliases', () => {
test2Index: ['test1Alias'],
test3Index: ['test2Alias', 'test3Alias'],
});

});

test('should return an empty object if no aliases exist', async () => {
const mockCallWithRequest = sinon.spy(() => {return [];});
const mockCallWithRequest = sinon.spy(() => {
return [];
});

const results = await fetchFn(mockCallWithRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

export async function fetchAliases(callWithRequest) {
export async function fetchAliases(callWithRequest: any) {
const results = await callWithRequest('cat.aliases', { format: 'json' });
return results.reduce((hash, { index, alias }) => {
(hash[index] = hash[index] || []).push(alias);
return hash;
}, {});
return results.reduce(
(hash: { [key: string]: any }, { index, alias }: { index: string; alias: string }) => {
(hash[index] = hash[index] || []).push(alias);
return hash;
},
{}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,46 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { fetchAliases } from './fetch_aliases';
import { getIndexManagementDataEnrichers } from '../../index_management_data';
import { getIndexManagementDataEnrichers } from '../index_management_data';
interface Hit {
health: string;
status: string;
index: string;
uuid: string;
pri: string;
rep: string;
'docs.count': any;
'store.size': any;
sth: 'true' | 'false';
}

interface Aliases {
[key: string]: string[];
}

const enrichResponse = async (response, callWithRequest) => {
interface Params {
format: string;
h: string;
index?: any; // todo
}

const enrichResponse = async (response: any, callWithRequest: any) => {
let enrichedResponse = response;
const dataEnrichers = getIndexManagementDataEnrichers();
for (let i = 0; i < dataEnrichers.length; i++) {
const dataEnricher = dataEnrichers[i];
try {
const dataEnricherResponse = await dataEnricher(enrichedResponse, callWithRequest);
enrichedResponse = dataEnricherResponse;
} catch(e) {
} catch (e) {
// silently swallow enricher response errors
}
}
return enrichedResponse;
};
function formatHits(hits, aliases) {
return hits.map(hit => {

function formatHits(hits: Hit[], aliases: Aliases) {
return hits.map((hit: Hit) => {
return {
health: hit.health,
status: hit.status,
Expand All @@ -37,19 +59,20 @@ function formatHits(hits, aliases) {
});
}

async function fetchIndicesCall(callWithRequest, indexNames) {
const params = {
async function fetchIndicesCall(callWithRequest: any, indexNames?: string[]) {
const params: Params = {
format: 'json',
h: 'health,status,index,uuid,pri,rep,docs.count,sth,store.size'
h: 'health,status,index,uuid,pri,rep,docs.count,sth,store.size',
};

if (indexNames) {
params.index = indexNames;
}

return await callWithRequest('cat.indices', params);
}

export const fetchIndices = async (callWithRequest, indexNames) => {
export const fetchIndices = async (callWithRequest: any, indexNames?: string[]) => {
const aliases = await fetchAliases(callWithRequest);
const hits = await fetchIndicesCall(callWithRequest, indexNames);
let response = formatHits(hits, aliases);
Expand Down
50 changes: 50 additions & 0 deletions x-pack/legacy/plugins/index_management/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.
*/
import { CoreSetup } from 'src/core/server';
import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch';
import { Router } from '../../../server/lib/create_router';
import { addIndexManagementDataEnricher } from './index_management_data';
import { registerIndicesRoutes } from './routes/api/indices';
import { registerTemplateRoutes } from './routes/api/templates';
import { registerMappingRoute } from './routes/api/mapping';
import { registerSettingsRoutes } from './routes/api/settings';
import { registerStatsRoute } from './routes/api/stats';

export interface LegacySetup {
router: Router;
plugins: {
elasticsearch: ElasticsearchPlugin;
license: {
registerLicenseChecker: () => void;
};
};
}

export interface IndexMgmtSetup {
addIndexManagementDataEnricher: (enricher: any) => void;
}

export class IndexMgmtPlugin {
public setup(core: CoreSetup, plugins: {}, __LEGACY: LegacySetup): IndexMgmtSetup {
const serverFacade = {
plugins: {
elasticsearch: __LEGACY.plugins.elasticsearch,
},
};

__LEGACY.plugins.license.registerLicenseChecker();

registerIndicesRoutes(__LEGACY.router);
registerTemplateRoutes(__LEGACY.router, serverFacade);
registerSettingsRoutes(__LEGACY.router);
registerStatsRoute(__LEGACY.router);
registerMappingRoute(__LEGACY.router);

return {
addIndexManagementDataEnricher,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router';

interface ReqPayload {
indices: string[];
}

const handler: RouterRouteHandler = async (request, callWithRequest, h) => {
const payload = request.payload as ReqPayload;
const { indices = [] } = payload;

const handler = async (request, callWithRequest, h) => {
const indices = request.payload.indices || [];
const params = {
expandWildcards: 'none',
format: 'json',
index: indices
index: indices,
};

await callWithRequest('indices.clearCache', params);
return h.response();
};
export function registerClearCacheRoute(router) {

export function registerClearCacheRoute(router: Router) {
router.post('indices/clear_cache', handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Router, RouterRouteHandler } from '../../../../../../server/lib/create_router';

interface ReqPayload {
indices: string[];
}

const handler: RouterRouteHandler = async (request, callWithRequest, h) => {
const payload = request.payload as ReqPayload;
const { indices = [] } = payload;

const handler = async (request, callWithRequest, h) => {
const indices = request.payload.indices || [];
const params = {
expandWildcards: 'none',
format: 'json',
index: indices
index: indices,
};

await callWithRequest('indices.close', params);
return h.response();
};
export function registerCloseRoute(router) {

export function registerCloseRoute(router: Router) {
router.post('indices/close', handler);
}
Loading

0 comments on commit 739edd8

Please sign in to comment.