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 (opensearch-project#8888)

* Isolate objects based on workspace when calling get/bulkGet

Signed-off-by: yubonluo <[email protected]>

* Changeset file for PR opensearch-project#8888 created/updated

* add integration tests

Signed-off-by: yubonluo <[email protected]>

* optimize the code

Signed-off-by: yubonluo <[email protected]>

* optimize the code

Signed-off-by: yubonluo <[email protected]>

* optimize the code

Signed-off-by: yubonluo <[email protected]>

* optimize the function name

Signed-off-by: yubonluo <[email protected]>

* add data source validate

Signed-off-by: yubonluo <[email protected]>

* optimize the code

Signed-off-by: yubonluo <[email protected]>

---------

Signed-off-by: yubonluo <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
yubonluo and opensearch-changeset-bot[bot] authored Dec 2, 2024
1 parent 1c744d6 commit b31206a
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 b31206a

Please sign in to comment.