fixing index pattern import (#21743) (#21790)

This commit is contained in:
Bill McConaghy 2018-08-08 12:12:21 -04:00 committed by GitHub
parent e69a2e1a36
commit ccff96340f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,14 +30,29 @@ async function getSavedObject(doc, services) {
return obj;
}
function addJsonFieldToIndexPattern(target, sourceString, fieldName, indexName) {
if (sourceString) {
try {
target[fieldName] = JSON.parse(sourceString);
} catch (error) {
throw new Error(`Error encountered parsing ${fieldName} for index pattern ${indexName}: ${error.message}`);
}
}
}
async function importIndexPattern(doc, indexPatterns, overwriteAll) {
// TODO: consolidate this is the code in create_index_pattern_wizard.js
const emptyPattern = await indexPatterns.get();
Object.assign(emptyPattern, {
const { title, timeFieldName, fields, fieldFormatMap, sourceFilters } = doc._source;
const importedIndexPattern = {
id: doc._id,
title: doc._source.title,
timeFieldName: doc._source.timeFieldName,
});
title,
timeFieldName
};
addJsonFieldToIndexPattern(importedIndexPattern, fields, 'fields', title);
addJsonFieldToIndexPattern(importedIndexPattern, fieldFormatMap, 'fieldFormatMap', title);
addJsonFieldToIndexPattern(importedIndexPattern, sourceFilters, 'sourceFilters', title);
Object.assign(emptyPattern, importedIndexPattern);
const newId = await emptyPattern.create(true, !overwriteAll);
indexPatterns.cache.clear(newId);
return newId;
@ -148,13 +163,18 @@ export async function resolveSavedObjects(
// Keep track of how many we actually import because the user
// can cancel an override
let importedObjectCount = 0;
const failedImports = [];
// Start with the index patterns since everything is dependent on them
await awaitEachItemInParallel(
docTypes.indexPatterns,
async indexPatternDoc => {
if (await importIndexPattern(indexPatternDoc, indexPatterns, overwriteAll)) {
importedObjectCount++;
try {
const importedIndexPatternId = await importIndexPattern(indexPatternDoc, indexPatterns, overwriteAll);
if (importedIndexPatternId) {
importedObjectCount++;
}
} catch (error) {
failedImports.push({ indexPatternDoc, error });
}
}
);