import { Readable } from 'stream' ; import { SavedObject , SavedObjectsClientContract , SavedObjectsImportRetry } from '../types' ; import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry' ; import { SavedObjectsImportFailure , SavedObjectsImportHook , SavedObjectsImportResponse , SavedObjectsImportSuccess , } from './types' ; import { collectSavedObjects , createObjectsFilter , splitOverwrites , regenerateIds , checkReferenceOrigins , validateReferences , validateRetries , createSavedObjects , getImportStateMapForRetries , checkConflicts , executeImportHooks , } from './lib' ; /**
 * Options to control the "resolve import" operation.
 */ export interface ResolveSavedObjectsImportErrorsOptions { /** The stream of  SavedObject to resolve errors from  readStream : Readable ; /** The maximum number of object to import */ objectLimit : number ; /** client to use to perform the import operation */ savedObjectsClient : SavedObjectsClientContract ; /** The registry of all known saved object types */ typeRegistry : ISavedObjectTypeRegistry ; /** List of registered import hooks */ importHooks : Record < string , SavedObjectsImportHook [ ] > ; /** saved object import references to retry */ retries : SavedObjectsImportRetry [ ] ; /** if specified, will import in given namespace */ namespace ? : string ; /** If true, will create new copies of import objects, each with a random `id` and undefined `originId`. */ createNewCopies : boolean ; } /**
 * Resolve and return saved object import errors.
 * See the  SavedObjectsResolveImportErrorsOptions for more detailed information.
 *
 *  public export async function resolveSavedObjectsImportErrors ( { readStream , objectLimit , retries , savedObjectsClient , typeRegistry , importHooks , namespace , createNewCopies , } ) { validateRetries ( retries ) ; let successCount = 0 ; let errorAccumulator = [ ] ; const supportedTypes = typeRegistry . getImportableAndExportableTypes ( ) . map ( ( type ) => type . name ) ; const filter = createObjectsFilter ( retries ) ; const collectSavedObjectsResult = await collectSavedObjects ( { readStream , objectLimit , filter , supportedTypes , } ) ; let importStateMap = collectSavedObjectsResult . importStateMap ; errorAccumulator = [ ... errorAccumulator , ... collectSavedObjectsResult . errors ] ; const retriesReferencesMap = new Map < string , { [ key ] : string ; } > ( ) ; for ( const retry of retries ) { const map = { } ; for ( const { type , from , to } of retry . replaceReferences ) { map [ `${ type }:${ from }` ] = to ; } retriesReferencesMap . set ( `${ retry . type }:${ retry . id }` , map ) ; } for ( const savedObject of collectSavedObjectsResult . collectedObjects ) { const refMap = retriesReferencesMap . get ( `${ savedObject . type }:${ savedObject . id }` ) ; if ( ! refMap ) { continue ; } for ( const reference of savedObject . references || [ ] ) { if ( refMap [ `${ reference . type }:${ reference . id }` ] ) { reference . id = refMap [ `${ reference . type }:${ reference . id }` ] ; } } } const checkReferenceOriginsResult = await checkReferenceOrigins ( { savedObjectsClient , typeRegistry , namespace , importStateMap , } ) ; importStateMap = new Map ( [ ... importStateMap , ... checkReferenceOriginsResult . importStateMap ] ) ; const validateReferencesResult = await validateReferences ( { objects : collectSavedObjectsResult . collectedObjects , savedObjectsClient , namespace , importStateMap , retries , } ) ; errorAccumulator = [ ... errorAccumulator , ... validateReferencesResult ] ; if ( createNewCopies ) { importStateMap = new Map ( [ ... importStateMap , ... regenerateIds ( collectSavedObjectsResult . collectedObjects ) , ] ) ; } const checkConflictsParams = { objects : collectSavedObjectsResult . collectedObjects , savedObjectsClient , namespace , retries , createNewCopies , } ; const checkConflictsResult = await checkConflicts ( checkConflictsParams ) ; errorAccumulator = [ ... errorAccumulator , ... checkConflictsResult . errors ] ; const getImportStateMapForRetriesParams = { objects : checkConflictsResult . filteredObjects , retries , createNewCopies , } ; const importStateMapForRetries = getImportStateMapForRetries ( getImportStateMapForRetriesParams ) ; importStateMap = new Map ( [ ... importStateMap , ... importStateMapForRetries , ... checkConflictsResult . importStateMap , ] ) ; let successResults = [ ] ; let successObjects = [ ] ; const accumulatedErrors = [ ... errorAccumulator ] ; const bulkCreateObjects = async ( objects , overwrite ? ) => { const createSavedObjectsParams = { objects , accumulatedErrors , savedObjectsClient , importStateMap , namespace , overwrite , } ; const { createdObjects , errors : bulkCreateErrors } = await createSavedObjects ( createSavedObjectsParams ) ; successObjects = [ ... successObjects , ... createdObjects ] ; errorAccumulator = [ ... errorAccumulator , ... bulkCreateErrors ] ; successCount += createdObjects . length ; successResults = [ ... successResults , ... createdObjects . map ( ( createdObject ) => { const { type , id , destinationId , originId } = createdObject ; const getTitle = typeRegistry . getType ( type ) ?. management ?. getTitle ; const meta = { title : getTitle ? getTitle ( createdObject ) : createdObject . attributes . title , icon : typeRegistry . getType ( type ) ?. management ?. icon , } ; return { type , id , meta , ... ( overwrite && { overwrite } ) , ... ( destinationId && { destinationId } ) , ... ( destinationId && ! originId && ! createNewCopies && { createNewCopy : true } ) , } ; } ) , ] ; } ; const { objectsToOverwrite , objectsToNotOverwrite } = splitOverwrites ( collectSavedObjectsResult . collectedObjects , retries ) ; await bulkCreateObjects ( objectsToOverwrite , true ) ; await bulkCreateObjects ( objectsToNotOverwrite ) ; const errorResults = errorAccumulator . map ( ( error ) => { const icon = typeRegistry . getType ( error . type ) ?. management ?. icon ; const attemptedOverwrite = retries . some ( ( { type , id , overwrite } ) => type === error . type && id === error . id && overwrite ) ; return { ... error , meta : { ... error . meta , icon } , ... ( attemptedOverwrite && { overwrite : true } ) , } ; } ) ; const warnings = await executeImportHooks ( { objects : successObjects , importHooks , } ) ; return { successCount , success : errorAccumulator . length === 0 , warnings , ... ( successResults . length && { successResults } ) , ... ( errorResults . length && { errors : errorResults } ) , } ; }