diff --git a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts index d654fab427f19..e76e44476df03 100644 --- a/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts @@ -213,25 +213,32 @@ export async function installKibanaSavedObjects({ kibanaAssets.map((asset) => createSavedObjectKibanaAsset(asset)) ); - let allSuccessResults = []; + let allSuccessResults: SavedObjectsImportSuccess[] = []; if (toBeSavedObjects.length === 0) { return []; } else { - const { successResults: importSuccessResults = [], errors: importErrors = [] } = - await retryImportOnConflictError(() => - savedObjectsImporter.import({ - overwrite: true, - readStream: createListStream(toBeSavedObjects), - createNewCopies: false, - }) - ); + const { + successResults: importSuccessResults = [], + errors: importErrors = [], + success, + } = await retryImportOnConflictError(() => + savedObjectsImporter.import({ + overwrite: true, + readStream: createListStream(toBeSavedObjects), + createNewCopies: false, + }) + ); + + if (success) { + allSuccessResults = importSuccessResults; + } - allSuccessResults = importSuccessResults; const [referenceErrors, otherErrors] = partition( importErrors, (e) => e?.error?.type === 'missing_references' ); + if (otherErrors?.length) { throw new Error( `Encountered ${ @@ -239,6 +246,7 @@ export async function installKibanaSavedObjects({ } errors creating saved objects: ${formatImportErrorsForLog(otherErrors)}` ); } + /* A reference error here means that a saved object reference in the references array cannot be found. This is an error in the package its-self but not a fatal @@ -253,20 +261,22 @@ export async function installKibanaSavedObjects({ } reference errors creating saved objects: ${formatImportErrorsForLog(referenceErrors)}` ); - const idsToResolve = new Set(referenceErrors.map(({ id }) => id)); - - const resolveSavedObjects = toBeSavedObjects.filter(({ id }) => idsToResolve.has(id)); - const retries = referenceErrors.map(({ id, type }) => ({ - id, - type, - ignoreMissingReferences: true, - replaceReferences: [], - overwrite: true, - })); + const retries = toBeSavedObjects.map(({ id, type }) => { + if (referenceErrors.find(({ id: idToSearch }) => idToSearch === id)) { + return { + id, + type, + ignoreMissingReferences: true, + replaceReferences: [], + overwrite: true, + }; + } + return { id, type, overwrite: true, replaceReferences: [] }; + }); const { successResults: resolveSuccessResults = [], errors: resolveErrors = [] } = await savedObjectsImporter.resolveImportErrors({ - readStream: createListStream(resolveSavedObjects), + readStream: createListStream(toBeSavedObjects), createNewCopies: false, retries, }); @@ -279,7 +289,7 @@ export async function installKibanaSavedObjects({ ); } - allSuccessResults = [...allSuccessResults, ...resolveSuccessResults]; + allSuccessResults = allSuccessResults.concat(resolveSuccessResults); } return allSuccessResults;