Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport workspace-pr-integr][Workspace] Support workspace in saved objects client in server side (#6365) #327

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading