Skip to content

Commit

Permalink
[Backport workspace-pr-integr][Workspace] Support workspace in saved …
Browse files Browse the repository at this point in the history
…objects client in server side (opensearch-project#6365) (#327)

* [Workspace] Support workspace in saved objects client in server side.  (opensearch-project#6365)

* Support workspace in saved objects client in server side. (#293)

* feat: POC implementation

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add some comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: revert dependency

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: address one TODO

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: address TODO

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: some special logic on specific operation

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add integration test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: declare workspaces as empty array for advanced settings

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: unified workspaces parameters when parsing from router

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: improve code coverage

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: declare workspaces as null

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: use unified types

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: remove null

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: address comments

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: use request app to store request workspace id

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: use app state to store request workspace id

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* refact: update types declaration

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* fix: unit test error

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

---------

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* fix: import error

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add integration test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update CHANGELOG

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: use consts and add comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: change the priority value

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

---------

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: remove useless code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

---------

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe authored Apr 15, 2024
1 parent 2058998 commit d6999ac
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export const createSavedObjects = async <T>({
const bulkCreateResponse = await savedObjectsClient.bulkCreate(objectsToCreate, {
namespace,
overwrite,
workspaces,
...(workspaces ? { workspaces } : {}),
});
expectedResults = bulkCreateResponse.saved_objects;
}
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/workspace/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ export enum WorkspacePermissionMode {
}

export const WORKSPACE_ID_CONSUMER_WRAPPER_ID = 'workspace_id_consumer';

/**
* The priority for these wrappers matters:
* 1. WORKSPACE_ID_CONSUMER should be placed before the other two wrappers(smaller than the other two wrappers) as it cost little
* and will append the essential workspaces field into the options, which will be honored by permission control wrapper and conflict wrapper.
* 2. The order of permission wrapper and conflict wrapper does not matter as no dependency between these two wrappers.
*/
export const PRIORITY_FOR_WORKSPACE_ID_CONSUMER_WRAPPER = -2;
export const PRIORITY_FOR_PERMISSION_CONTROL_WRAPPER = 0;
export const PRIORITY_FOR_WORKSPACE_CONFLICT_CONTROL_WRAPPER = -1;
9 changes: 6 additions & 3 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID,
WORKSPACE_CONFLICT_CONTROL_SAVED_OBJECTS_CLIENT_WRAPPER_ID,
WORKSPACE_ID_CONSUMER_WRAPPER_ID,
PRIORITY_FOR_WORKSPACE_CONFLICT_CONTROL_WRAPPER,
PRIORITY_FOR_WORKSPACE_ID_CONSUMER_WRAPPER,
PRIORITY_FOR_PERMISSION_CONTROL_WRAPPER,
} from '../common/constants';
import {
IWorkspaceClientImpl,
Expand Down Expand Up @@ -107,7 +110,7 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
);

core.savedObjects.addClientWrapper(
0,
PRIORITY_FOR_PERMISSION_CONTROL_WRAPPER,
WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID,
this.workspaceSavedObjectsClientWrapper.wrapperFactory
);
Expand All @@ -131,13 +134,13 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
this.workspaceConflictControl = new WorkspaceConflictSavedObjectsClientWrapper();

core.savedObjects.addClientWrapper(
-1,
PRIORITY_FOR_WORKSPACE_CONFLICT_CONTROL_WRAPPER,
WORKSPACE_CONFLICT_CONTROL_SAVED_OBJECTS_CLIENT_WRAPPER_ID,
this.workspaceConflictControl.wrapperFactory
);

core.savedObjects.addClientWrapper(
-2,
PRIORITY_FOR_WORKSPACE_ID_CONSUMER_WRAPPER,
WORKSPACE_ID_CONSUMER_WRAPPER_ID,
new WorkspaceIdConsumerWrapper().wrapperFactory
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,33 @@ describe('workspace_id_consumer integration test', () => {
)
);
});

it('import within workspace', async () => {
await clearFooAndBar();

const importWithWorkspacesResult = await osdTestServer.request
.post(root, `/w/${createdFooWorkspace.id}/api/saved_objects/_import?overwrite=false`)
.attach(
'file',
Buffer.from(
[
JSON.stringify({
...dashboard,
id: 'bar',
}),
].join('\n'),
'utf-8'
),
'tmp.ndjson'
)
.expect(200);

const findResult = await osdTestServer.request
.get(root, `/w/${createdFooWorkspace.id}/api/saved_objects/_find?type=${dashboard.type}`)
.expect(200);

expect(importWithWorkspacesResult.body.success).toEqual(true);
expect(findResult.body.saved_objects[0].workspaces).toEqual([createdFooWorkspace.id]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
const errorContent = (error: Boom.Boom) => error.output.payload;

const filterWorkspacesAccordingToSourceWorkspaces = (
targetWorkspaces?: string[],
baseWorkspaces?: string[]
targetWorkspaces?: SavedObjectsBaseOptions['workspaces'],
baseWorkspaces?: SavedObjectsBaseOptions['workspaces']
): string[] => targetWorkspaces?.filter((item) => !baseWorkspaces?.includes(item)) || [];

export class WorkspaceConflictSavedObjectsClientWrapper {
Expand Down Expand Up @@ -110,7 +110,7 @@ export class WorkspaceConflictSavedObjectsClientWrapper {
})
: [];
const objectsConflictWithWorkspace: SavedObject[] = [];
const objectsMapWorkspaces: Record<string, string[] | undefined> = {};
const objectsMapWorkspaces: Record<string, SavedObjectsBaseOptions['workspaces']> = {};
if (bulkGetDocs.length) {
/**
* Get latest status of objects
Expand Down

0 comments on commit d6999ac

Please sign in to comment.