Skip to content

Commit

Permalink
[Workspace] Isolate objects based on workspace when calling get/bulkG…
Browse files Browse the repository at this point in the history
…et (#8888) (#8990)

* Isolate objects based on workspace when calling get/bulkGet



* Changeset file for PR #8888 created/updated

* add integration tests



* optimize the code



* optimize the code



* optimize the code



* optimize the function name



* add data source validate



* optimize the code



---------



(cherry picked from commit b31206a)

Signed-off-by: yubonluo <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 3, 2024
1 parent 573ca63 commit 719d272
Show file tree
Hide file tree
Showing 6 changed files with 744 additions and 341 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8888.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
refactor:
- [Workspace] Isolate objects based on workspace when calling get/bulkGet ([#8888](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8888))
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('workspace_id_consumer integration test', () => {
let createdBarWorkspace: WorkspaceAttributes = {
id: '',
};
const deleteWorkspace = (workspaceId: string) =>
osdTestServer.request.delete(root, `/api/workspaces/${workspaceId}`);
beforeAll(async () => {
const { startOpenSearch, startOpenSearchDashboards } = osdTestServer.createTestServers({
adjustTimeout: (t: number) => jest.setTimeout(t),
Expand Down Expand Up @@ -75,6 +77,10 @@ describe('workspace_id_consumer integration test', () => {
}).then((resp) => resp.body.result);
}, 30000);
afterAll(async () => {
await Promise.all([
deleteWorkspace(createdFooWorkspace.id),
deleteWorkspace(createdBarWorkspace.id),
]);
await root.shutdown();
await opensearchServer.stop();
});
Expand Down Expand Up @@ -312,5 +318,153 @@ describe('workspace_id_consumer integration test', () => {
expect(importWithWorkspacesResult.body.success).toEqual(true);
expect(findResult.body.saved_objects[0].workspaces).toEqual([createdFooWorkspace.id]);
});

it('get', async () => {
await clearFooAndBar();
await osdTestServer.request.delete(
root,
`/api/saved_objects/${config.type}/${packageInfo.version}`
);
const createResultFoo = await osdTestServer.request
.post(root, `/w/${createdFooWorkspace.id}/api/saved_objects/_bulk_create`)
.send([
{
...dashboard,
id: 'foo',
},
])
.expect(200);

const createResultBar = await osdTestServer.request
.post(root, `/w/${createdBarWorkspace.id}/api/saved_objects/_bulk_create`)
.send([
{
...dashboard,
id: 'bar',
},
])
.expect(200);

await osdTestServer.request
.post(root, `/api/saved_objects/${config.type}/${packageInfo.version}`)
.send({
attributes: {
legacyConfig: 'foo',
},
})
.expect(200);

const getResultWithRequestWorkspace = await osdTestServer.request
.get(root, `/w/${createdFooWorkspace.id}/api/saved_objects/${dashboard.type}/foo`)
.expect(200);
expect(getResultWithRequestWorkspace.body.id).toEqual('foo');
expect(getResultWithRequestWorkspace.body.workspaces).toEqual([createdFooWorkspace.id]);

const getResultWithoutRequestWorkspace = await osdTestServer.request
.get(root, `/api/saved_objects/${dashboard.type}/bar`)
.expect(200);
expect(getResultWithoutRequestWorkspace.body.id).toEqual('bar');

const getGlobalResultWithinWorkspace = await osdTestServer.request
.get(
root,
`/w/${createdFooWorkspace.id}/api/saved_objects/${config.type}/${packageInfo.version}`
)
.expect(200);
expect(getGlobalResultWithinWorkspace.body.id).toEqual(packageInfo.version);

await osdTestServer.request
.get(root, `/w/${createdFooWorkspace.id}/api/saved_objects/${dashboard.type}/bar`)
.expect(403);

await Promise.all(
[...createResultFoo.body.saved_objects, ...createResultBar.body.saved_objects].map((item) =>
deleteItem({
type: item.type,
id: item.id,
})
)
);
await osdTestServer.request.delete(
root,
`/api/saved_objects/${config.type}/${packageInfo.version}`
);
});

it('bulk get', async () => {
await clearFooAndBar();
const createResultFoo = await osdTestServer.request
.post(root, `/w/${createdFooWorkspace.id}/api/saved_objects/_bulk_create`)
.send([
{
...dashboard,
id: 'foo',
},
])
.expect(200);

const createResultBar = await osdTestServer.request
.post(root, `/w/${createdBarWorkspace.id}/api/saved_objects/_bulk_create`)
.send([
{
...dashboard,
id: 'bar',
},
])
.expect(200);

const payload = [
{ id: 'foo', type: 'dashboard' },
{ id: 'bar', type: 'dashboard' },
];
const bulkGetResultWithWorkspace = await osdTestServer.request
.post(root, `/w/${createdFooWorkspace.id}/api/saved_objects/_bulk_get`)
.send(payload)
.expect(200);

expect(bulkGetResultWithWorkspace.body.saved_objects.length).toEqual(2);
expect(bulkGetResultWithWorkspace.body.saved_objects[0].id).toEqual('foo');
expect(bulkGetResultWithWorkspace.body.saved_objects[0].workspaces).toEqual([
createdFooWorkspace.id,
]);
expect(bulkGetResultWithWorkspace.body.saved_objects[0]?.error).toBeUndefined();
expect(bulkGetResultWithWorkspace.body.saved_objects[1].id).toEqual('bar');
expect(bulkGetResultWithWorkspace.body.saved_objects[1].workspaces).toEqual([
createdBarWorkspace.id,
]);
expect(bulkGetResultWithWorkspace.body.saved_objects[1]?.error).toMatchInlineSnapshot(`
Object {
"error": "Forbidden",
"message": "Saved object does not belong to the workspace",
"statusCode": 403,
}
`);

const bulkGetResultWithoutWorkspace = await osdTestServer.request
.post(root, `/api/saved_objects/_bulk_get`)
.send(payload)
.expect(200);

expect(bulkGetResultWithoutWorkspace.body.saved_objects.length).toEqual(2);
expect(bulkGetResultWithoutWorkspace.body.saved_objects[0].id).toEqual('foo');
expect(bulkGetResultWithoutWorkspace.body.saved_objects[0].workspaces).toEqual([
createdFooWorkspace.id,
]);
expect(bulkGetResultWithoutWorkspace.body.saved_objects[0]?.error).toBeUndefined();
expect(bulkGetResultWithoutWorkspace.body.saved_objects[1].id).toEqual('bar');
expect(bulkGetResultWithoutWorkspace.body.saved_objects[1].workspaces).toEqual([
createdBarWorkspace.id,
]);
expect(bulkGetResultWithoutWorkspace.body.saved_objects[1]?.error).toBeUndefined();

await Promise.all(
[...createResultFoo.body.saved_objects, ...createResultBar.body.saved_objects].map((item) =>
deleteItem({
type: item.type,
id: item.id,
})
)
);
});
});
});
Loading

0 comments on commit 719d272

Please sign in to comment.