Skip to content

Commit

Permalink
Refactor types
Browse files Browse the repository at this point in the history
Renamed `importIdMap` to `importStateMap`, changed `id` value to
`destinationId`, and added TSdocs.
  • Loading branch information
Joe Portner committed Jan 6, 2022
1 parent 76f17de commit 78f2428
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 208 deletions.
61 changes: 31 additions & 30 deletions src/core/server/saved_objects/import/import_saved_objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ISavedObjectTypeRegistry } from '..';
import { typeRegistryMock } from '../saved_objects_type_registry.mock';
import { importSavedObjectsFromStream, ImportSavedObjectsOptions } from './import_saved_objects';
import { SavedObjectsImportHook, SavedObjectsImportWarning } from './types';
import type { ImportStateMap } from './lib';

describe('#importSavedObjectsFromStream', () => {
beforeEach(() => {
Expand All @@ -37,19 +38,19 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [],
collectedObjects: [],
importIdMap: new Map(),
importStateMap: new Map(),
});
mockRegenerateIds.mockReturnValue(new Map());
mockValidateReferences.mockResolvedValue([]);
mockCheckConflicts.mockResolvedValue({
errors: [],
filteredObjects: [],
importIdMap: new Map(),
importStateMap: new Map(),
pendingOverwrites: new Set(),
});
mockCheckOriginConflicts.mockResolvedValue({
errors: [],
importIdMap: new Map(),
importStateMap: new Map(),
pendingOverwrites: new Set(),
});
mockCreateSavedObjects.mockResolvedValue({ errors: [], createdObjects: [] });
Expand Down Expand Up @@ -141,7 +142,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [],
collectedObjects,
importIdMap: new Map(),
importStateMap: new Map(),
});

await importSavedObjectsFromStream(options);
Expand All @@ -162,7 +163,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [],
collectedObjects,
importIdMap: new Map(),
importStateMap: new Map(),
});
mockCreateSavedObjects.mockResolvedValue({
errors: [],
Expand All @@ -184,7 +185,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [],
collectedObjects,
importIdMap: new Map(),
importStateMap: new Map(),
});

await importSavedObjectsFromStream(options);
Expand All @@ -197,7 +198,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [],
collectedObjects,
importIdMap: new Map(),
importStateMap: new Map(),
});

await importSavedObjectsFromStream(options);
Expand All @@ -213,11 +214,11 @@ describe('#importSavedObjectsFromStream', () => {
test('checks origin conflicts', async () => {
const options = setupOptions();
const filteredObjects = [createObject()];
const importIdMap = new Map();
const importStateMap = new Map();
mockCheckConflicts.mockResolvedValue({
errors: [],
filteredObjects,
importIdMap,
importStateMap,
pendingOverwrites: new Set(),
});

Expand All @@ -228,7 +229,7 @@ describe('#importSavedObjectsFromStream', () => {
typeRegistry,
namespace,
ignoreRegularConflicts: overwrite,
importIdMap,
importStateMap,
};
expect(mockCheckOriginConflicts).toHaveBeenCalledWith(checkOriginConflictsParams);
});
Expand All @@ -241,7 +242,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [errors[0]],
collectedObjects,
importIdMap: new Map([
importStateMap: new Map([
['foo', {}],
['bar', {}],
['baz', {}],
Expand All @@ -251,26 +252,26 @@ describe('#importSavedObjectsFromStream', () => {
mockCheckConflicts.mockResolvedValue({
errors: [errors[2]],
filteredObjects,
importIdMap: new Map([['bar', { id: 'newId1' }]]),
importStateMap: new Map([['bar', { destinationId: 'newId1' }]]),
pendingOverwrites: new Set(),
});
mockCheckOriginConflicts.mockResolvedValue({
errors: [errors[3]],
importIdMap: new Map([['baz', { id: 'newId2' }]]),
importStateMap: new Map([['baz', { destinationId: 'newId2' }]]),
pendingOverwrites: new Set(),
});

await importSavedObjectsFromStream(options);
const importIdMap = new Map([
const importStateMap = new Map([
['foo', {}],
['bar', { id: 'newId1' }],
['baz', { id: 'newId2' }],
['bar', { destinationId: 'newId1' }],
['baz', { destinationId: 'newId2' }],
]);
const createSavedObjectsParams = {
objects: collectedObjects,
accumulatedErrors: errors,
savedObjectsClient,
importIdMap,
importStateMap,
overwrite,
namespace,
};
Expand All @@ -285,7 +286,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [],
collectedObjects,
importIdMap: new Map(), // doesn't matter
importStateMap: new Map(), // doesn't matter
});

await importSavedObjectsFromStream(options);
Expand All @@ -308,22 +309,22 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [errors[0]],
collectedObjects,
importIdMap: new Map([
importStateMap: new Map([
['foo', {}],
['bar', {}],
]),
});
mockValidateReferences.mockResolvedValue([errors[1]]);
// this importIdMap is not composed with the one obtained from `collectSavedObjects`
const importIdMap = new Map().set(`id1`, { id: `newId1` });
mockRegenerateIds.mockReturnValue(importIdMap);
// this importStateMap is not composed with the one obtained from `collectSavedObjects`
const importStateMap: ImportStateMap = new Map().set(`id1`, { destinationId: `newId1` });
mockRegenerateIds.mockReturnValue(importStateMap);

await importSavedObjectsFromStream(options);
const createSavedObjectsParams = {
objects: collectedObjects,
accumulatedErrors: errors,
savedObjectsClient,
importIdMap,
importStateMap,
overwrite,
namespace,
};
Expand All @@ -345,7 +346,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [createError()],
collectedObjects: [],
importIdMap: new Map(), // doesn't matter
importStateMap: new Map(), // doesn't matter
});

const result = await importSavedObjectsFromStream(options);
Expand All @@ -363,7 +364,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [],
collectedObjects,
importIdMap: new Map(),
importStateMap: new Map(),
});
mockCreateSavedObjects.mockResolvedValue({
errors: [],
Expand Down Expand Up @@ -411,7 +412,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCheckConflicts.mockResolvedValue({
errors: [],
filteredObjects: [],
importIdMap: new Map(),
importStateMap: new Map(),
pendingOverwrites: new Set([
`${success2.type}:${success2.id}`, // the success2 object was overwritten
`${error2.type}:${error2.id}`, // an attempt was made to overwrite the error2 object
Expand Down Expand Up @@ -487,7 +488,7 @@ describe('#importSavedObjectsFromStream', () => {
mockCheckConflicts.mockResolvedValue({
errors: [],
filteredObjects: [],
importIdMap: new Map(),
importStateMap: new Map(),
pendingOverwrites: new Set(),
});
mockCreateSavedObjects.mockResolvedValue({ errors: [], createdObjects: [obj1, obj2] });
Expand Down Expand Up @@ -521,18 +522,18 @@ describe('#importSavedObjectsFromStream', () => {
mockCollectSavedObjects.mockResolvedValue({
errors: [errors[0]],
collectedObjects: [],
importIdMap: new Map(), // doesn't matter
importStateMap: new Map(), // doesn't matter
});
mockValidateReferences.mockResolvedValue([errors[1]]);
mockCheckConflicts.mockResolvedValue({
errors: [errors[2]],
filteredObjects: [],
importIdMap: new Map(), // doesn't matter
importStateMap: new Map(), // doesn't matter
pendingOverwrites: new Set(),
});
mockCheckOriginConflicts.mockResolvedValue({
errors: [errors[3]],
importIdMap: new Map(), // doesn't matter
importStateMap: new Map(), // doesn't matter
pendingOverwrites: new Set(),
});
mockCreateSavedObjects.mockResolvedValue({ errors: [errors[4]], createdObjects: [] });
Expand Down
12 changes: 6 additions & 6 deletions src/core/server/saved_objects/import/import_saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function importSavedObjectsFromStream({
});
errorAccumulator = [...errorAccumulator, ...collectSavedObjectsResult.errors];
/** Map of all IDs for objects that we are attempting to import; each value is empty by default */
let importIdMap = collectSavedObjectsResult.importIdMap;
let importStateMap = collectSavedObjectsResult.importStateMap;
let pendingOverwrites = new Set<string>();

// Validate references
Expand All @@ -85,7 +85,7 @@ export async function importSavedObjectsFromStream({
errorAccumulator = [...errorAccumulator, ...validateReferencesResult];

if (createNewCopies) {
importIdMap = regenerateIds(collectSavedObjectsResult.collectedObjects);
importStateMap = regenerateIds(collectSavedObjectsResult.collectedObjects);
} else {
// Check single-namespace objects for conflicts in this namespace, and check multi-namespace objects for conflicts across all namespaces
const checkConflictsParams = {
Expand All @@ -96,7 +96,7 @@ export async function importSavedObjectsFromStream({
};
const checkConflictsResult = await checkConflicts(checkConflictsParams);
errorAccumulator = [...errorAccumulator, ...checkConflictsResult.errors];
importIdMap = new Map([...importIdMap, ...checkConflictsResult.importIdMap]);
importStateMap = new Map([...importStateMap, ...checkConflictsResult.importStateMap]);
pendingOverwrites = checkConflictsResult.pendingOverwrites;

// Check multi-namespace object types for origin conflicts in this namespace
Expand All @@ -106,11 +106,11 @@ export async function importSavedObjectsFromStream({
typeRegistry,
namespace,
ignoreRegularConflicts: overwrite,
importIdMap,
importStateMap,
};
const checkOriginConflictsResult = await checkOriginConflicts(checkOriginConflictsParams);
errorAccumulator = [...errorAccumulator, ...checkOriginConflictsResult.errors];
importIdMap = new Map([...importIdMap, ...checkOriginConflictsResult.importIdMap]);
importStateMap = new Map([...importStateMap, ...checkOriginConflictsResult.importStateMap]);
pendingOverwrites = new Set([
...pendingOverwrites,
...checkOriginConflictsResult.pendingOverwrites,
Expand All @@ -122,7 +122,7 @@ export async function importSavedObjectsFromStream({
objects: collectSavedObjectsResult.collectedObjects,
accumulatedErrors: errorAccumulator,
savedObjectsClient,
importIdMap,
importStateMap,
overwrite,
namespace,
};
Expand Down
14 changes: 9 additions & 5 deletions src/core/server/saved_objects/import/lib/check_conflicts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('#checkConflicts', () => {
expect(checkConflictsResult).toEqual({
filteredObjects: [],
errors: [],
importIdMap: new Map(),
importStateMap: new Map(),
pendingOverwrites: new Set(),
});
});
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('#checkConflicts', () => {
error: { ...obj4Error.error, type: 'unknown' },
},
],
importIdMap: new Map([[`${obj3.type}:${obj3.id}`, { id: 'uuidv4' }]]),
importStateMap: new Map([[`${obj3.type}:${obj3.id}`, { destinationId: 'uuidv4' }]]),
pendingOverwrites: new Set(),
});
});
Expand Down Expand Up @@ -185,20 +185,24 @@ describe('#checkConflicts', () => {
error: { ...obj4Error.error, type: 'unknown' },
},
],
importIdMap: new Map([[`${obj3.type}:${obj3.id}`, { id: 'uuidv4', omitOriginId: true }]]),
importStateMap: new Map([
[`${obj3.type}:${obj3.id}`, { destinationId: 'uuidv4', omitOriginId: true }],
]),
pendingOverwrites: new Set([`${obj5.type}:${obj5.id}`]),
});
});

it('adds `omitOriginId` field to `importIdMap` entries when createNewCopies=true', async () => {
it('adds `omitOriginId` field to `importStateMap` entries when createNewCopies=true', async () => {
const namespace = 'foo-namespace';
const params = setupParams({ objects, namespace, createNewCopies: true });
socCheckConflicts.mockResolvedValue({ errors: [obj2Error, obj3Error, obj4Error] });

const checkConflictsResult = await checkConflicts(params);
expect(checkConflictsResult).toEqual(
expect.objectContaining({
importIdMap: new Map([[`${obj3.type}:${obj3.id}`, { id: 'uuidv4', omitOriginId: true }]]),
importStateMap: new Map([
[`${obj3.type}:${obj3.id}`, { destinationId: 'uuidv4', omitOriginId: true }],
]),
})
);
});
Expand Down
9 changes: 5 additions & 4 deletions src/core/server/saved_objects/import/lib/check_conflicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SavedObjectError,
SavedObjectsImportRetry,
} from '../../types';
import type { ImportStateMap } from './types';

interface CheckConflictsParams {
objects: Array<SavedObject<{ title?: string }>>;
Expand All @@ -37,12 +38,12 @@ export async function checkConflicts({
}: CheckConflictsParams) {
const filteredObjects: Array<SavedObject<{ title?: string }>> = [];
const errors: SavedObjectsImportFailure[] = [];
const importIdMap = new Map<string, { id?: string; omitOriginId?: boolean }>();
const importStateMap: ImportStateMap = new Map();
const pendingOverwrites = new Set<string>();

// exit early if there are no objects to check
if (objects.length === 0) {
return { filteredObjects, errors, importIdMap, pendingOverwrites };
return { filteredObjects, errors, importStateMap, pendingOverwrites };
}

const retryMap = retries.reduce(
Expand Down Expand Up @@ -76,7 +77,7 @@ export async function checkConflicts({
// This code path should not be triggered for a retry, but in case the consumer is using the import APIs incorrectly and attempting to
// retry an object with a destinationId that would result in an unresolvable conflict, we regenerate the ID here as a fail-safe.
const omitOriginId = createNewCopies || createNewCopy;
importIdMap.set(`${type}:${id}`, { id: uuidv4(), omitOriginId });
importStateMap.set(`${type}:${id}`, { destinationId: uuidv4(), omitOriginId });
filteredObjects.push(object);
} else if (errorObj && errorObj.statusCode !== 409) {
errors.push({ type, id, title, meta: { title }, error: { ...errorObj, type: 'unknown' } });
Expand All @@ -90,5 +91,5 @@ export async function checkConflicts({
}
}
});
return { filteredObjects, errors, importIdMap, pendingOverwrites };
return { filteredObjects, errors, importStateMap, pendingOverwrites };
}
Loading

0 comments on commit 78f2428

Please sign in to comment.