Skip to content

Commit

Permalink
feat: create public workspace when service start
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Aug 3, 2023
1 parent 4223a02 commit b5538a6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 36 additions & 3 deletions src/core/server/workspaces/workspaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +28,10 @@ export interface WorkspacesSetupDeps {
savedObject: InternalSavedObjectsServiceSetup;
}

export interface WorkpsaceStartDeps {
savedObjects: SavedObjectsServiceStart;
}

export type InternalWorkspacesServiceSetup = WorkspacesServiceSetup;
export type InternalWorkspacesServiceStart = WorkspacesServiceStart;

Expand Down Expand Up @@ -84,8 +90,35 @@ export class WorkspacesService
};
}

public async start(): Promise<InternalWorkspacesServiceStart> {
public async start(startDeps: WorkpsaceStartDeps): Promise<InternalWorkspacesServiceStart> {
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<WorkspaceAttribute, 'id'>,
{
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,
Expand Down

0 comments on commit b5538a6

Please sign in to comment.