Skip to content

Commit

Permalink
Move plugin init back into routes folder. Syntax updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Caldwell committed Dec 23, 2019
1 parent 24b7c90 commit ac97af5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
12 changes: 4 additions & 8 deletions x-pack/legacy/plugins/file_upload/server/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getImportRouteHandler, importRouteConfig } from './routes/file_upload';
import { initRoutes } from './routes/file_upload';
import { registerFileUploadUsageCollector } from './telemetry';

export const IMPORT_ROUTE = '/api/fileupload/import';

export class FileUploadPlugin {
setup(core, plugins, __LEGACY) {
const elasticsearchPlugin = __LEGACY.plugins.elasticsearch;
const getSavedObjectsRepository = __LEGACY.savedObjects.getSavedObjectsRepository;

// Set up route
__LEGACY.route({
method: 'POST',
path: '/api/fileupload/import',
handler: getImportRouteHandler(elasticsearchPlugin, getSavedObjectsRepository),
config: importRouteConfig
});
initRoutes(__LEGACY.route, elasticsearchPlugin, getSavedObjectsRepository);

registerFileUploadUsageCollector(plugins.usageCollection, {
elasticsearchPlugin,
Expand Down
49 changes: 32 additions & 17 deletions x-pack/legacy/plugins/file_upload/server/routes/file_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { importDataProvider } from '../models/import_data';
import { updateTelemetry } from '../telemetry/telemetry';
import { MAX_BYTES } from '../../common/constants/file_import';
import Joi from 'joi';
import { IMPORT_ROUTE } from '../plugin';

function importData({ callWithRequest, id, index, settings, mappings, ingestPipeline, data }) {
const { importData: importDataFunc } = importDataProvider(callWithRequest);
Expand Down Expand Up @@ -37,40 +38,54 @@ export function getImportRouteHandler(elasticsearchPlugin, getSavedObjectsReposi
const requestContentWithDefaults = {
id,
callWithRequest: callWithRequestFactory(elasticsearchPlugin, requestObj),
...requestObj.payload
...requestObj.payload,
};
return importData(requestContentWithDefaults).catch(wrapError);
};
}

export const importRouteConfig = {
payload: {
maxBytes: MAX_BYTES
maxBytes: MAX_BYTES,
},
validate: {
query: Joi.object().keys({
id: Joi.string(),
}),
payload: Joi.object({
app: Joi.string(),
index: Joi.string().min(1).required(),
data: Joi.array().when(
Joi.ref('$query.id'), {
index: Joi.string()
.min(1)
.required(),
data: Joi.array()
.when(Joi.ref('$query.id'), {
is: Joi.exist(),
then: Joi.array().min(1).required()
}).default([]),
then: Joi.array()
.min(1)
.required(),
})
.default([]),
fileType: Joi.string().required(),
ingestPipeline: Joi.object().default({}),
settings: Joi.object().when(
Joi.ref('$query.id'), {
settings: Joi.object()
.when(Joi.ref('$query.id'), {
is: null,
then: Joi.required()
}).default({}),
mappings: Joi.object().when(
Joi.ref('$query.id'), {
is: null,
then: Joi.required()
}),
then: Joi.required(),
})
.default({}),
mappings: Joi.object().when(Joi.ref('$query.id'), {
is: null,
then: Joi.required(),
}),
}).required(),
}
},
};

export const initRoutes = (route, esPlugin, getSavedObjectsRepository) => {
route({
method: 'POST',
path: IMPORT_ROUTE,
handler: getImportRouteHandler(esPlugin, getSavedObjectsRepository),
config: importRouteConfig,
});
};

0 comments on commit ac97af5

Please sign in to comment.