Skip to content

Commit

Permalink
feat: add basic workspace saved objects client wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Oct 7, 2023
1 parent c05b434 commit 9a1c9f2
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import {
PluginInitializerContext,
CoreSetup,
Plugin,
Logger,
CoreStart,
} from '../../../core/server';
import { PluginInitializerContext, CoreSetup, Plugin, Logger } from '../../../core/server';
import { WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID } from '../common/constants';
import { IWorkspaceDBImpl } from './types';
import { WorkspaceClientWithSavedObject } from './workspace_client';
import { registerRoutes } from './routes';
import { WorkspaceSavedObjectsClientWrapper } from './saved_objects';

export class WorkspacePlugin implements Plugin<{}, {}> {
private readonly logger: Logger;
Expand All @@ -28,6 +24,14 @@ export class WorkspacePlugin implements Plugin<{}, {}> {

await this.client.setup(core);

const workspaceSavedObjectsClientWrapper = new WorkspaceSavedObjectsClientWrapper();

core.savedObjects.addClientWrapper(
0,
WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID,
workspaceSavedObjectsClientWrapper.wrapperFactory
);

registerRoutes({
http: core.http,
logger: this.logger,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/workspace/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/

export { workspace } from './workspace';
export { WorkspaceSavedObjectsClientWrapper } from './workspace_saved_objects_client_wrapper';
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import {
SavedObject,
SavedObjectsBaseOptions,
SavedObjectsBulkCreateObject,
SavedObjectsBulkGetObject,
SavedObjectsBulkResponse,
SavedObjectsClientWrapperFactory,
SavedObjectsCreateOptions,
SavedObjectsDeleteOptions,
SavedObjectsFindOptions,
SavedObjectsUpdateOptions,
SavedObjectsUpdateResponse,
SavedObjectsBulkUpdateObject,
SavedObjectsBulkUpdateResponse,
SavedObjectsBulkUpdateOptions,
} from '../../../../core/server';

export class WorkspaceSavedObjectsClientWrapper {
public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
const deleteWithWorkspacePermissionControl = async (
type: string,
id: string,
options: SavedObjectsDeleteOptions = {}
) => {
return await wrapperOptions.client.delete(type, id, options);
};

const updateWithWorkspacePermissionControl = async <T = unknown>(
type: string,
id: string,
attributes: Partial<T>,
options: SavedObjectsUpdateOptions = {}
): Promise<SavedObjectsUpdateResponse<T>> => {
return await wrapperOptions.client.update(type, id, attributes, options);
};

const bulkUpdateWithWorkspacePermissionControl = async <T = unknown>(
objects: Array<SavedObjectsBulkUpdateObject<T>>,
options?: SavedObjectsBulkUpdateOptions
): Promise<SavedObjectsBulkUpdateResponse<T>> => {
return await wrapperOptions.client.bulkUpdate(objects, options);
};

const bulkCreateWithWorkspacePermissionControl = async <T = unknown>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
options: SavedObjectsCreateOptions = {}
): Promise<SavedObjectsBulkResponse<T>> => {
return await wrapperOptions.client.bulkCreate(objects, options);
};

const createWithWorkspacePermissionControl = async <T = unknown>(
type: string,
attributes: T,
options?: SavedObjectsCreateOptions
) => {
return await wrapperOptions.client.create(type, attributes, options);
};

const getWithWorkspacePermissionControl = async <T = unknown>(
type: string,
id: string,
options: SavedObjectsBaseOptions = {}
): Promise<SavedObject<T>> => {
const objectToGet = await wrapperOptions.client.get<T>(type, id, options);
return objectToGet;
};

const bulkGetWithWorkspacePermissionControl = async <T = unknown>(
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsBulkResponse<T>> => {
const objectToBulkGet = await wrapperOptions.client.bulkGet<T>(objects, options);

return objectToBulkGet;
};

const findWithWorkspacePermissionControl = async <T = unknown>(
options: SavedObjectsFindOptions
) => {
return await wrapperOptions.client.find<T>(options);
};

return {
...wrapperOptions.client,
get: getWithWorkspacePermissionControl,
checkConflicts: wrapperOptions.client.checkConflicts,
find: findWithWorkspacePermissionControl,
bulkGet: bulkGetWithWorkspacePermissionControl,
errors: wrapperOptions.client.errors,
addToNamespaces: wrapperOptions.client.addToNamespaces,
deleteFromNamespaces: wrapperOptions.client.deleteFromNamespaces,
create: createWithWorkspacePermissionControl,
bulkCreate: bulkCreateWithWorkspacePermissionControl,
delete: deleteWithWorkspacePermissionControl,
update: updateWithWorkspacePermissionControl,
bulkUpdate: bulkUpdateWithWorkspacePermissionControl,
};
};

constructor() {}
}

0 comments on commit 9a1c9f2

Please sign in to comment.