ML] Fixes bug in module setup endpoint caused by PR 30935 (#34414)

* [ML] Fixes module setup endpoint

* tweaks endpoint to allow trailing slash
This commit is contained in:
James Gowdy 2019-04-03 12:42:59 +01:00 committed by GitHub
parent 5c457972d4
commit 3930c36898
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View file

@ -267,15 +267,12 @@ export class DataRecognizer {
// load the config from disk
const moduleConfig = await this.getModule(moduleId, jobPrefix);
const manifestFile = await this.getManifestFile(moduleId);
if (indexPatternName === undefined &&
manifestFile && manifestFile.json &&
manifestFile.json.defaultIndexPattern === undefined) {
if (indexPatternName === undefined && moduleConfig.defaultIndexPattern === undefined) {
throw Boom.badRequest(`No index pattern configured in "${moduleId}" configuration file and no index pattern passed to the endpoint`);
}
this.indexPatternName = (indexPatternName === undefined) ? manifestFile.json.defaultIndexPattern : indexPatternName;
this.indexPatternName = (indexPatternName === undefined) ? moduleConfig.defaultIndexPattern : indexPatternName;
this.indexPatternId = this.getIndexPatternId(this.indexPatternName);
// create an empty results object
@ -573,6 +570,12 @@ export class DataRecognizer {
// listing each job/datafeed/savedObject with a save success boolean
createResultsTemplate(moduleConfig) {
const results = {};
const reducedConfig = {
jobs: moduleConfig.jobs,
datafeeds: moduleConfig.datafeeds,
kibana: moduleConfig.kibana,
};
function createResultsItems(configItems, resultItems, index) {
resultItems[index] = [];
configItems.forEach((j) => {
@ -583,13 +586,13 @@ export class DataRecognizer {
});
}
Object.keys(moduleConfig).forEach((i) => {
if (Array.isArray(moduleConfig[i])) {
createResultsItems(moduleConfig[i], results, i);
Object.keys(reducedConfig).forEach((i) => {
if (Array.isArray(reducedConfig[i])) {
createResultsItems(reducedConfig[i], results, i);
} else {
results[i] = {};
Object.keys(moduleConfig[i]).forEach((k) => {
createResultsItems(moduleConfig[i][k], results[i], k);
Object.keys(reducedConfig[i]).forEach((k) => {
createResultsItems(reducedConfig[i][k], results[i], k);
});
}
});

View file

@ -73,7 +73,12 @@ export function dataRecognizer(server, commonRouteConfig) {
path: '/api/ml/modules/get_module/{moduleId?}',
handler(request) {
const callWithRequest = callWithRequestFactory(server, request);
const moduleId = request.params.moduleId;
let moduleId = request.params.moduleId;
if (moduleId === '') {
// if the endpoint is called with a trailing /
// the moduleId will be an empty string.
moduleId = undefined;
}
return getModule(callWithRequest, moduleId)
.catch(resp => wrapError(resp));
},