From b5538a6eede80d2dceb10a55600a08c6274af29e Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Thu, 3 Aug 2023 13:21:48 +0800 Subject: [PATCH] feat: create public workspace when service start Signed-off-by: SuZhou-Joe --- src/core/server/server.ts | 4 +- .../server/workspaces/workspaces_service.ts | 39 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/core/server/server.ts b/src/core/server/server.ts index 47a72cfe6cbb..f80b90ba6baa 100644 --- a/src/core/server/server.ts +++ b/src/core/server/server.ts @@ -261,7 +261,9 @@ export class Server { opensearch: opensearchStart, savedObjects: savedObjectsStart, }); - await this.workspaces.start(); + await this.workspaces.start({ + savedObjects: savedObjectsStart, + }); this.coreStart = { capabilities: capabilitiesStart, diff --git a/src/core/server/workspaces/workspaces_service.ts b/src/core/server/workspaces/workspaces_service.ts index f86c0e7ca4d3..c9708d87a91a 100644 --- a/src/core/server/workspaces/workspaces_service.ts +++ b/src/core/server/workspaces/workspaces_service.ts @@ -8,10 +8,12 @@ import { CoreContext } from '../core_context'; import { InternalHttpServiceSetup } from '../http'; import { Logger } from '../logging'; import { registerRoutes } from './routes'; -import { InternalSavedObjectsServiceSetup } from '../saved_objects'; -import { IWorkspaceDBImpl } from './types'; +import { InternalSavedObjectsServiceSetup, SavedObjectsServiceStart } from '../saved_objects'; +import { IWorkspaceDBImpl, WorkspaceAttribute } from './types'; import { WorkspacesClientWithSavedObject } from './workspaces_client'; import { WorkspaceSavedObjectsClientWrapper } from './saved_objects'; +import { WORKSPACE_TYPE } from './constants'; +import { PUBLIC_WORKSPACE } from '../../utils'; export interface WorkspacesServiceSetup { client: IWorkspaceDBImpl; @@ -26,6 +28,10 @@ export interface WorkspacesSetupDeps { savedObject: InternalSavedObjectsServiceSetup; } +export interface WorkpsaceStartDeps { + savedObjects: SavedObjectsServiceStart; +} + export type InternalWorkspacesServiceSetup = WorkspacesServiceSetup; export type InternalWorkspacesServiceStart = WorkspacesServiceStart; @@ -84,8 +90,35 @@ export class WorkspacesService }; } - public async start(): Promise { + public async start(startDeps: WorkpsaceStartDeps): Promise { this.logger.debug('Starting SavedObjects service'); + /** + * Internal repository is attached to global tenant. + */ + const internalRepository = startDeps.savedObjects.createInternalRepository(); + + try { + await internalRepository.get(WORKSPACE_TYPE, PUBLIC_WORKSPACE); + } catch (error) { + this.logger.debug(error?.toString() || ''); + this.logger.info('No public workspace found, create it by using internal user'); + try { + const createResult = await internalRepository.create( + WORKSPACE_TYPE, + { + name: 'public', + } as Omit, + { + id: PUBLIC_WORKSPACE, + } + ); + if (createResult.id) { + this.logger.info(`Created workspace ${createResult.id} in global tenant.`); + } + } catch (e) { + this.logger.error(`Create public workspace error: ${e?.toString() || ''}`); + } + } return { client: this.client as IWorkspaceDBImpl,