Skip to content

Commit

Permalink
[Fleet] Fix saved object import missing reference (#127760) (#127957)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 4f3022e)

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
  • Loading branch information
kibanamachine and nchaulet authored Mar 17, 2022
1 parent ee09dbd commit 0a94c82
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,32 +213,40 @@ 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 ${
otherErrors.length
} 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
Expand All @@ -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,
});
Expand All @@ -279,7 +289,7 @@ export async function installKibanaSavedObjects({
);
}

allSuccessResults = [...allSuccessResults, ...resolveSuccessResults];
allSuccessResults = allSuccessResults.concat(resolveSuccessResults);
}

return allSuccessResults;
Expand Down

0 comments on commit 0a94c82

Please sign in to comment.