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

refactoring server routes in index management to use common code #30299

Merged
merged 3 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 9 additions & 6 deletions x-pack/plugins/index_management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { registerIndicesRoutes } from './server/routes/api/indices';
import { registerMappingRoute } from './server/routes/api/mapping';
import { registerSettingsRoutes } from './server/routes/api/settings';
import { registerStatsRoute } from './server/routes/api/stats';
import { registerLicenseChecker } from './server/lib/register_license_checker';
import { registerLicenseChecker } from '../../server/lib/register_license_checker';
import { PLUGIN } from './common/constants';
import { addIndexManagementDataEnricher } from "./index_management_data";
import { createRouter } from '../../server/lib/create_router';

export function indexManagement(kibana) {
return new kibana.Plugin({
id: PLUGIN.ID,
Expand All @@ -25,12 +27,13 @@ export function indexManagement(kibana) {
]
},
init: function (server) {
const router = createRouter(server, PLUGIN.ID, '/api/index_management/');
server.expose('addIndexManagementDataEnricher', addIndexManagementDataEnricher);
registerLicenseChecker(server);
registerIndicesRoutes(server);
registerSettingsRoutes(server);
registerStatsRoute(server);
registerMappingRoute(server);
registerLicenseChecker(server, PLUGIN.ID);
registerIndicesRoutes(router);
registerSettingsRoutes(router);
registerStatsRoute(router);
registerMappingRoute(router);
}
});
}
22 changes: 6 additions & 16 deletions x-pack/plugins/index_management/server/lib/fetch_indices.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { wrapEsError, wrapUnknownError } from './error_wrappers';
import { fetchAliases } from './fetch_aliases';
import { getIndexManagementDataEnrichers } from '../../index_management_data';

Expand Down Expand Up @@ -51,18 +49,10 @@ async function fetchIndicesCall(callWithRequest, indexNames) {
return await callWithRequest('cat.indices', params);
}

export const fetchIndices = async (callWithRequest, isEsError, indexNames) => {
try {
const aliases = await fetchAliases(callWithRequest);
const hits = await fetchIndicesCall(callWithRequest, indexNames);
let response = formatHits(hits, aliases);
response = await enrichResponse(response, callWithRequest);
return response;
} catch (err) {
if (isEsError(err)) {
throw wrapEsError(err);
}

throw wrapUnknownError(err);
}
export const fetchIndices = async (callWithRequest, indexNames) => {
const aliases = await fetchAliases(callWithRequest);
const hits = await fetchIndicesCall(callWithRequest, indexNames);
let response = formatHits(hits, aliases);
response = await enrichResponse(response, callWithRequest);
return response;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,18 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { callWithRequestFactory } from '../../../lib/call_with_request_factory';
import { isEsErrorFactory } from '../../../lib/is_es_error_factory';
import { wrapEsError, wrapUnknownError } from '../../../lib/error_wrappers';
import { licensePreRoutingFactory } from'../../../lib/license_pre_routing_factory';

function getIndexArrayFromPayload(payload) {
return payload.indices || [];
}

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

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

export function registerClearCacheRoute(server) {
const isEsError = isEsErrorFactory(server);
const licensePreRouting = licensePreRoutingFactory(server);

server.route({
path: '/api/index_management/indices/clear_cache',
method: 'POST',
handler: async (request, h) => {
const callWithRequest = callWithRequestFactory(server, request);
const indices = getIndexArrayFromPayload(request.payload);

try {
await clearCache(callWithRequest, indices);

//TODO: Should we check acknowledged = true?
return h.response();
} catch (err) {
if (isEsError(err)) {
throw wrapEsError(err);
}

throw wrapUnknownError(err);
}
},
config: {
pre: [ licensePreRouting ]
}
});
await callWithRequest('indices.clearCache', params);
return h.response();
};
export function registerClearCacheRoute(router) {
router.post('indices/clear_caches', handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,18 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { callWithRequestFactory } from '../../../lib/call_with_request_factory';
import { isEsErrorFactory } from '../../../lib/is_es_error_factory';
import { wrapEsError, wrapUnknownError } from '../../../lib/error_wrappers';
import { licensePreRoutingFactory } from'../../../lib/license_pre_routing_factory';

function getIndexArrayFromPayload(payload) {
return payload.indices || [];
}

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

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

export function registerCloseRoute(server) {
const isEsError = isEsErrorFactory(server);
const licensePreRouting = licensePreRoutingFactory(server);

server.route({
path: '/api/index_management/indices/close',
method: 'POST',
handler: async (request, h) => {
const callWithRequest = callWithRequestFactory(server, request);
const indices = getIndexArrayFromPayload(request.payload);

try {
await closeIndices(callWithRequest, indices);

//TODO: Should we check acknowledged = true?
return h.response();
} catch (err) {
if (isEsError(err)) {
throw wrapEsError(err);
}

throw wrapUnknownError(err);
}
},
config: {
pre: [ licensePreRouting ]
}
});
await callWithRequest('indices.close', params);
return h.response();
};
export function registerCloseRoute(router) {
router.post('indices/close', handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { callWithRequestFactory } from '../../../lib/call_with_request_factory';
import { isEsErrorFactory } from '../../../lib/is_es_error_factory';
import { wrapEsError, wrapUnknownError } from '../../../lib/error_wrappers';
import { licensePreRoutingFactory } from'../../../lib/license_pre_routing_factory';

function getIndexArrayFromPayload(payload) {
return payload.indices || [];
}

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

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

export function registerDeleteRoute(server) {
const isEsError = isEsErrorFactory(server);
const licensePreRouting = licensePreRoutingFactory(server);

server.route({
path: '/api/index_management/indices/delete',
method: 'POST',
handler: async (request, h) => {
const callWithRequest = callWithRequestFactory(server, request);
const indices = getIndexArrayFromPayload(request.payload);

try {
await deleteIndices(callWithRequest, indices);
return h.response();
} catch (err) {
if (isEsError(err)) {
throw wrapEsError(err);
}

throw wrapUnknownError(err);
}
},
config: {
pre: [ licensePreRouting ]
}
});
await callWithRequest('indices.delete', params);
return h.response();
};
export function registerDeleteRoute(router) {
router.post('indices/delete', handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { callWithRequestFactory } from '../../../lib/call_with_request_factory';
import { isEsErrorFactory } from '../../../lib/is_es_error_factory';
import { wrapEsError, wrapUnknownError } from '../../../lib/error_wrappers';
import { licensePreRoutingFactory } from'../../../lib/license_pre_routing_factory';

function getIndexArrayFromPayload(payload) {
return payload.indices || [];
}

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

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

export function registerFlushRoute(server) {
const isEsError = isEsErrorFactory(server);
const licensePreRouting = licensePreRoutingFactory(server);

server.route({
path: '/api/index_management/indices/flush',
method: 'POST',
handler: async (request, h) => {
const callWithRequest = callWithRequestFactory(server, request);
const indices = getIndexArrayFromPayload(request.payload);

try {
await flushIndices(callWithRequest, indices);

//TODO: Should we check acknowledged = true?
return h.response();
} catch (err) {
if (isEsError(err)) {
throw wrapEsError(err);
}

throw wrapUnknownError(err);
}
},
config: {
pre: [ licensePreRouting ]
}
});
}
await callWithRequest('indices.flush', params);
return h.response();
};
export function registerFlushRoute(router) {
router.post('indices/flush', handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { callWithRequestFactory } from '../../../lib/call_with_request_factory';
import { isEsErrorFactory } from '../../../lib/is_es_error_factory';
import { wrapEsError, wrapUnknownError } from '../../../lib/error_wrappers';
import { licensePreRoutingFactory } from'../../../lib/license_pre_routing_factory';

async function forcemergeIndices(callWithRequest, indices, maxNumSegments) {
const handler = async (request, callWithRequest, h) => {
const { maxNumSegments, indices = [] } = request.payload;
const params = {
expandWildcards: 'none',
index: indices,
Expand All @@ -18,35 +15,9 @@ async function forcemergeIndices(callWithRequest, indices, maxNumSegments) {
params.max_num_segments = maxNumSegments;
}

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

export function registerForcemergeRoute(server) {
const isEsError = isEsErrorFactory(server);
const licensePreRouting = licensePreRoutingFactory(server);

server.route({
path: '/api/index_management/indices/forcemerge',
method: 'POST',
handler: async (request, h) => {
const callWithRequest = callWithRequestFactory(server, request);
const { payload } = request;
const { indices = [], maxNumSegments } = payload;
try {
await forcemergeIndices(callWithRequest, indices, maxNumSegments);

//TODO: Should we check acknowledged = true?
return h.response();
} catch (err) {
if (isEsError(err)) {
throw wrapEsError(err);
}

throw wrapUnknownError(err);
}
},
config: {
pre: [ licensePreRouting ]
}
});
}
await callWithRequest('indices.forcemerge', params);
return h.response();
};
export function registerForcemergeRoute(router) {
router.post('indices/forcemerge', handler);
}
Loading