diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e432e83d93..36c35fcf3a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Table Visualization] Move format table, consolidate types and add unit tests ([#3397](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3397)) - [Multiple Datasource] Support Amazon OpenSearch Serverless ([#3957](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3957)) - Add support for Node.js >=14.20.1 <19 ([#4071](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4071)) +- [Saved Object Service] Add Repository Factory Provider ([#4149](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4149)) ### 🐛 Bug Fixes diff --git a/src/core/server/legacy/legacy_service.ts b/src/core/server/legacy/legacy_service.ts index ae7e85b98d6..4baa0c65f43 100644 --- a/src/core/server/legacy/legacy_service.ts +++ b/src/core/server/legacy/legacy_service.ts @@ -275,6 +275,8 @@ export class LegacyService implements CoreService { addClientWrapper: setupDeps.core.savedObjects.addClientWrapper, registerType: setupDeps.core.savedObjects.registerType, getImportExportObjectLimit: setupDeps.core.savedObjects.getImportExportObjectLimit, + registerRepositoryFactoryProvider: + setupDeps.core.savedObjects.registerRepositoryFactoryProvider, }, status: { isStatusPageAnonymous: setupDeps.core.status.isStatusPageAnonymous, diff --git a/src/core/server/plugins/plugin_context.ts b/src/core/server/plugins/plugin_context.ts index 521a8dd2f7b..4c166ffedbd 100644 --- a/src/core/server/plugins/plugin_context.ts +++ b/src/core/server/plugins/plugin_context.ts @@ -204,6 +204,7 @@ export function createPluginSetupContext( addClientWrapper: deps.savedObjects.addClientWrapper, registerType: deps.savedObjects.registerType, getImportExportObjectLimit: deps.savedObjects.getImportExportObjectLimit, + registerRepositoryFactoryProvider: deps.savedObjects.registerRepositoryFactoryProvider, }, status: { core$: deps.status.core$, diff --git a/src/core/server/saved_objects/saved_objects_service.mock.ts b/src/core/server/saved_objects/saved_objects_service.mock.ts index ae36b83c0cd..21e96877e8d 100644 --- a/src/core/server/saved_objects/saved_objects_service.mock.ts +++ b/src/core/server/saved_objects/saved_objects_service.mock.ts @@ -79,6 +79,7 @@ const createSetupContractMock = () => { addClientWrapper: jest.fn(), registerType: jest.fn(), getImportExportObjectLimit: jest.fn(), + registerRepositoryFactoryProvider: jest.fn(), }; setupContract.getImportExportObjectLimit.mockReturnValue(100); diff --git a/src/core/server/saved_objects/saved_objects_service.test.ts b/src/core/server/saved_objects/saved_objects_service.test.ts index 42ee52567e5..acd91a23587 100644 --- a/src/core/server/saved_objects/saved_objects_service.test.ts +++ b/src/core/server/saved_objects/saved_objects_service.test.ts @@ -41,7 +41,7 @@ import { errors as opensearchErrors } from '@opensearch-project/opensearch'; import { SavedObjectsService } from './saved_objects_service'; import { mockCoreContext } from '../core_context.mock'; import { Env } from '../config'; -import { configServiceMock } from '../mocks'; +import { configServiceMock, savedObjectsRepositoryMock } from '../mocks'; import { opensearchServiceMock } from '../opensearch/opensearch_service.mock'; import { opensearchClientMock } from '../opensearch/client/mocks'; import { httpServiceMock } from '../http/http_service.mock'; @@ -49,6 +49,7 @@ import { httpServerMock } from '../http/http_server.mocks'; import { SavedObjectsClientFactoryProvider } from './service/lib'; import { NodesVersionCompatibility } from '../opensearch/version_check/ensure_opensearch_version'; import { SavedObjectsRepository } from './service/lib/repository'; +import { SavedObjectRepositoryFactoryProvider } from './service/lib/scoped_client_provider'; jest.mock('./service/lib/repository'); @@ -169,6 +170,27 @@ describe('SavedObjectsService', () => { expect(typeRegistryInstanceMock.registerType).toHaveBeenCalledWith(type); }); }); + + describe('#registerRepositoryFactoryProvider', () => { + it('throws error if a repository is already registered', async () => { + const coreContext = createCoreContext(); + const soService = new SavedObjectsService(coreContext); + const setup = await soService.setup(createSetupDeps()); + + const firstRepository: SavedObjectRepositoryFactoryProvider = () => + savedObjectsRepositoryMock.create(); + const secondRepository: SavedObjectRepositoryFactoryProvider = () => + savedObjectsRepositoryMock.create(); + + setup.registerRepositoryFactoryProvider(firstRepository); + + expect(() => { + setup.registerRepositoryFactoryProvider(secondRepository); + }).toThrowErrorMatchingInlineSnapshot( + `"custom repository factory is already set, and can only be set once"` + ); + }); + }); }); describe('#start()', () => { @@ -281,6 +303,15 @@ describe('SavedObjectsService', () => { }).toThrowErrorMatchingInlineSnapshot( `"cannot call \`registerType\` after service startup."` ); + + const customRpository: SavedObjectRepositoryFactoryProvider = () => + savedObjectsRepositoryMock.create(); + + expect(() => { + setup.registerRepositoryFactoryProvider(customRpository); + }).toThrowErrorMatchingInlineSnapshot( + '"cannot call `registerRepositoryFactoryProvider` after service startup."' + ); }); describe('#getTypeRegistry', () => { @@ -368,6 +399,36 @@ describe('SavedObjectsService', () => { expect(includedHiddenTypes).toEqual(['someHiddenType']); }); + + it('Should not create SavedObjectsRepository when custom repository is registered ', async () => { + const coreContext = createCoreContext({ skipMigration: false }); + const soService = new SavedObjectsService(coreContext); + const coreSetup = createSetupDeps(); + const setup = await soService.setup(coreSetup); + + const customRpository: SavedObjectRepositoryFactoryProvider = () => + savedObjectsRepositoryMock.create(); + setup.registerRepositoryFactoryProvider(customRpository); + + const coreStart = createStartDeps(); + const { createInternalRepository } = await soService.start(coreStart); + createInternalRepository(); + + expect(SavedObjectsRepository.createRepository as jest.Mocked).not.toHaveBeenCalled(); + }); + + it('Should create SavedObjectsRepository when no custom repository is registered ', async () => { + const coreContext = createCoreContext({ skipMigration: false }); + const soService = new SavedObjectsService(coreContext); + const coreSetup = createSetupDeps(); + await soService.setup(coreSetup); + + const coreStart = createStartDeps(); + const { createInternalRepository } = await soService.start(coreStart); + createInternalRepository(); + + expect(SavedObjectsRepository.createRepository as jest.Mocked).toHaveBeenCalled(); + }); }); }); }); diff --git a/src/core/server/saved_objects/saved_objects_service.ts b/src/core/server/saved_objects/saved_objects_service.ts index 92649d36beb..1860e1d3498 100644 --- a/src/core/server/saved_objects/saved_objects_service.ts +++ b/src/core/server/saved_objects/saved_objects_service.ts @@ -56,6 +56,7 @@ import { ISavedObjectsRepository, SavedObjectsRepository } from './service/lib/r import { SavedObjectsClientFactoryProvider, SavedObjectsClientWrapperFactory, + SavedObjectRepositoryFactoryProvider, } from './service/lib/scoped_client_provider'; import { Logger } from '../logging'; import { SavedObjectTypeRegistry, ISavedObjectTypeRegistry } from './saved_objects_type_registry'; @@ -166,6 +167,14 @@ export interface SavedObjectsServiceSetup { * Returns the maximum number of objects allowed for import or export operations. */ getImportExportObjectLimit: () => number; + + /** + * Set the default {@link SavedObjectRepositoryFactoryProvider | factory provider} for creating Saved Objects repository. + * Only one repository can be set, subsequent calls to this method will fail. + */ + registerRepositoryFactoryProvider: ( + respositoryFactoryProvider: SavedObjectRepositoryFactoryProvider + ) => void; } /** @@ -291,6 +300,8 @@ export class SavedObjectsService private typeRegistry = new SavedObjectTypeRegistry(); private started = false; + private respositoryFactoryProvider?: SavedObjectRepositoryFactoryProvider; + constructor(private readonly coreContext: CoreContext) { this.logger = coreContext.logger.get('savedobjects-service'); } @@ -348,6 +359,15 @@ export class SavedObjectsService this.typeRegistry.registerType(type); }, getImportExportObjectLimit: () => this.config!.maxImportExportSize, + registerRepositoryFactoryProvider: (repositoryProvider) => { + if (this.started) { + throw new Error('cannot call `registerRepositoryFactoryProvider` after service startup.'); + } + if (this.respositoryFactoryProvider) { + throw new Error('custom repository factory is already set, and can only be set once'); + } + this.respositoryFactoryProvider = repositoryProvider; + }, }; } @@ -422,13 +442,21 @@ export class SavedObjectsService opensearchClient: OpenSearchClient, includedHiddenTypes: string[] = [] ) => { - return SavedObjectsRepository.createRepository( - migrator, - this.typeRegistry, - opensearchDashboardsConfig.index, - opensearchClient, - includedHiddenTypes - ); + if (this.respositoryFactoryProvider) { + return this.respositoryFactoryProvider({ + migrator, + typeRegistry: this.typeRegistry, + includedHiddenTypes, + }); + } else { + return SavedObjectsRepository.createRepository( + migrator, + this.typeRegistry, + opensearchDashboardsConfig.index, + opensearchClient, + includedHiddenTypes + ); + } }; const repositoryFactory: SavedObjectsRepositoryFactory = { diff --git a/src/core/server/saved_objects/service/lib/scoped_client_provider.ts b/src/core/server/saved_objects/service/lib/scoped_client_provider.ts index 439cd31afa9..e7d423f39cf 100644 --- a/src/core/server/saved_objects/service/lib/scoped_client_provider.ts +++ b/src/core/server/saved_objects/service/lib/scoped_client_provider.ts @@ -31,8 +31,33 @@ import { PriorityCollection } from './priority_collection'; import { SavedObjectsClientContract } from '../../types'; import { SavedObjectsRepositoryFactory } from '../../saved_objects_service'; -import { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry'; +import { + ISavedObjectTypeRegistry, + SavedObjectTypeRegistry, +} from '../../saved_objects_type_registry'; import { OpenSearchDashboardsRequest } from '../../../http'; +import { ISavedObjectsRepository } from './repository'; +import { OpenSearchClient } from '../../../opensearch'; +import { IOpenSearchDashboardsMigrator } from '../../migrations'; + +/** + * Options passed to each SavedObjectRepositoryFactoryProvider to aid in creating the repository instance. + * @public + */ +export interface SavedObjectsRepositoryOptions { + migrator: IOpenSearchDashboardsMigrator; + typeRegistry: SavedObjectTypeRegistry; + includedHiddenTypes: string[]; + client?: OpenSearchClient; +} + +/** + * Provider to invoke to a factory function for creating ISavedObjectRepository {@link ISavedObjectRepository} instances. + * @public + */ +export type SavedObjectRepositoryFactoryProvider = ( + options: SavedObjectsRepositoryOptions +) => ISavedObjectsRepository; /** * Options passed to each SavedObjectsClientWrapperFactory to aid in creating the wrapper instance.