From ccff96340feb8b1e656a0b04de6fcbedf6387bc7 Mon Sep 17 00:00:00 2001 From: Bill McConaghy Date: Wed, 8 Aug 2018 12:12:21 -0400 Subject: [PATCH] fixing index pattern import (#21743) (#21790) --- .../objects/lib/resolve_saved_objects.js | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.js b/src/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.js index f3976ec37e53..d5cc485f51ea 100644 --- a/src/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.js +++ b/src/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.js @@ -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 }); } } );