Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Jun 25, 2024
1 parent 09c6abe commit b7fcb9b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@ import type {
SavedObjectsImportSuccess,
SavedObjectsImportResponse,
} from '@kbn/core/server';
import {
loggingSystemMock,
savedObjectsClientMock,
savedObjectsServiceMock,
} from '@kbn/core/server/mocks';
import { loggingSystemMock } from '@kbn/core/server/mocks';

import { appContextService } from '../../../app_context';

import { type ArchiveAsset, getSpaceAwareSaveobjectsClients } from './install';
import { type ArchiveAsset } from './install';

jest.mock('timers/promises', () => ({
async setTimeout() {},
}));
jest.mock('../../../app_context');

import { createSavedObjectKibanaAsset, installKibanaSavedObjects } from './install';

Expand Down Expand Up @@ -152,30 +145,3 @@ describe('createSavedObjectKibanaAsset', () => {
expect(result.coreMigrationVersion).toEqual('8.7.0');
});
});

describe('getSpaceAwareSaveobjectsClients', () => {
it('return space scopped clients', () => {
const soStartMock = savedObjectsServiceMock.createStartContract();
const mockedSavedObjectTagging = {
createInternalAssignmentService: jest.fn(),
createTagClient: jest.fn(),
};

const scoppedSoClient = savedObjectsClientMock.create();
jest
.mocked(appContextService.getInternalUserSOClientForSpaceId)
.mockReturnValue(scoppedSoClient);

jest.mocked(appContextService.getSavedObjects).mockReturnValue(soStartMock);
jest.mocked(appContextService.getSavedObjectsTagging).mockReturnValue(mockedSavedObjectTagging);

getSpaceAwareSaveobjectsClients('test1');

expect(appContextService.getInternalUserSOClientForSpaceId).toBeCalledWith('test1');
expect(soStartMock.createImporter).toBeCalledWith(scoppedSoClient, expect.anything());
expect(mockedSavedObjectTagging.createInternalAssignmentService).toBeCalledWith({
client: scoppedSoClient,
});
expect(mockedSavedObjectTagging.createTagClient).toBeCalledWith({ client: scoppedSoClient });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import {
import { kibanaAssetsToAssetsRef, saveKibanaAssetsRefs } from '../../packages/install';
import { deleteKibanaSavedObjectsAssets } from '../../packages/remove';
import { FleetError, KibanaSOReferenceError } from '../../../../errors';
import { appContextService } from '../../../app_context';
import { withPackageSpan } from '../../packages/utils';

import { tagKibanaAssets } from './tag_assets';
import { getSpaceAwareSaveobjectsClients } from './saved_objects';

type SavedObjectsImporterContract = Pick<ISavedObjectsImporter, 'import' | 'resolveImportErrors'>;
const formatImportErrorsForLog = (errors: SavedObjectsImportFailure[]) =>
Expand Down Expand Up @@ -219,30 +219,6 @@ export async function installKibanaAssetsAndReferencesMultispace({
});
}

export function getSpaceAwareSaveobjectsClients(spaceId?: string) {
// Saved object client need to be scopped with the package space for saved object tagging
const savedObjectClientWithSpace = appContextService.getInternalUserSOClientForSpaceId(spaceId);

const savedObjectsImporter = appContextService
.getSavedObjects()
.createImporter(savedObjectClientWithSpace, { importSizeLimit: 15_000 });

const savedObjectTagAssignmentService = appContextService
.getSavedObjectsTagging()
.createInternalAssignmentService({ client: savedObjectClientWithSpace });

const savedObjectTagClient = appContextService
.getSavedObjectsTagging()
.createTagClient({ client: savedObjectClientWithSpace });

return {
savedObjectClientWithSpace,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
};
}

export async function installKibanaAssetsAndReferences({
savedObjectsClient,
logger,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { savedObjectsClientMock, savedObjectsServiceMock } from '@kbn/core/server/mocks';

import { appContextService } from '../../../app_context';

import { getSpaceAwareSaveobjectsClients } from './saved_objects';

jest.mock('../../../app_context');

describe('getSpaceAwareSaveobjectsClients', () => {
it('return space scopped clients', () => {
const soStartMock = savedObjectsServiceMock.createStartContract();
const mockedSavedObjectTagging = {
createInternalAssignmentService: jest.fn(),
createTagClient: jest.fn(),
};

const scoppedSoClient = savedObjectsClientMock.create();
jest
.mocked(appContextService.getInternalUserSOClientForSpaceId)
.mockReturnValue(scoppedSoClient);

jest.mocked(appContextService.getSavedObjects).mockReturnValue(soStartMock);
jest.mocked(appContextService.getSavedObjectsTagging).mockReturnValue(mockedSavedObjectTagging);

getSpaceAwareSaveobjectsClients('test1');

expect(appContextService.getInternalUserSOClientForSpaceId).toBeCalledWith('test1');
expect(soStartMock.createImporter).toBeCalledWith(scoppedSoClient, expect.anything());
expect(mockedSavedObjectTagging.createInternalAssignmentService).toBeCalledWith({
client: scoppedSoClient,
});
expect(mockedSavedObjectTagging.createTagClient).toBeCalledWith({ client: scoppedSoClient });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { appContextService } from '../../../app_context';

export function getSpaceAwareSaveobjectsClients(spaceId?: string) {
// Saved object client need to be scopped with the package space for saved object tagging
const savedObjectClientWithSpace = appContextService.getInternalUserSOClientForSpaceId(spaceId);

const savedObjectsImporter = appContextService
.getSavedObjects()
.createImporter(savedObjectClientWithSpace, { importSizeLimit: 15_000 });

const savedObjectTagAssignmentService = appContextService
.getSavedObjectsTagging()
.createInternalAssignmentService({ client: savedObjectClientWithSpace });

const savedObjectTagClient = appContextService
.getSavedObjectsTagging()
.createTagClient({ client: savedObjectClientWithSpace });

return {
savedObjectClientWithSpace,
savedObjectsImporter,
savedObjectTagAssignmentService,
savedObjectTagClient,
};
}

0 comments on commit b7fcb9b

Please sign in to comment.