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; 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) { async function importIndexPattern(doc, indexPatterns, overwriteAll) {
// TODO: consolidate this is the code in create_index_pattern_wizard.js // TODO: consolidate this is the code in create_index_pattern_wizard.js
const emptyPattern = await indexPatterns.get(); const emptyPattern = await indexPatterns.get();
Object.assign(emptyPattern, { const { title, timeFieldName, fields, fieldFormatMap, sourceFilters } = doc._source;
const importedIndexPattern = {
id: doc._id, id: doc._id,
title: doc._source.title, title,
timeFieldName: doc._source.timeFieldName, 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); const newId = await emptyPattern.create(true, !overwriteAll);
indexPatterns.cache.clear(newId); indexPatterns.cache.clear(newId);
return newId; return newId;
@ -148,13 +163,18 @@ export async function resolveSavedObjects(
// Keep track of how many we actually import because the user // Keep track of how many we actually import because the user
// can cancel an override // can cancel an override
let importedObjectCount = 0; let importedObjectCount = 0;
const failedImports = [];
// Start with the index patterns since everything is dependent on them // Start with the index patterns since everything is dependent on them
await awaitEachItemInParallel( await awaitEachItemInParallel(
docTypes.indexPatterns, docTypes.indexPatterns,
async indexPatternDoc => { async indexPatternDoc => {
if (await importIndexPattern(indexPatternDoc, indexPatterns, overwriteAll)) { try {
importedObjectCount++; const importedIndexPatternId = await importIndexPattern(indexPatternDoc, indexPatterns, overwriteAll);
if (importedIndexPatternId) {
importedObjectCount++;
}
} catch (error) {
failedImports.push({ indexPatternDoc, error });
} }
} }
); );