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

[Workspace] Isolate objects based on workspace when calling get/bulkGet #8888

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
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,128 @@ 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();
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 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');

await osdTestServer.request
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
.get(root, `/w/${createdFooWorkspace.id}/api/saved_objects/${dashboard.type}/bar`)
.expect(404);

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

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": "Not Found",
"message": "Saved object [dashboard/bar] not found",
"statusCode": 404,
}
`);

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,
})
)
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SavedObject } from '../../../../core/public';
import { httpServerMock, savedObjectsClientMock, coreMock } from '../../../../core/server/mocks';
import { WorkspaceIdConsumerWrapper } from './workspace_id_consumer_wrapper';
import { workspaceClientMock } from '../workspace_client.mock';
import { SavedObjectsErrorHelpers } from '../../../../core/server';

describe('WorkspaceIdConsumerWrapper', () => {
const requestHandlerContext = coreMock.createRequestHandlerContext();
Expand Down Expand Up @@ -196,4 +197,230 @@ describe('WorkspaceIdConsumerWrapper', () => {
});
});
});

describe('get', () => {
beforeEach(() => {
mockedClient.get.mockClear();
});

it(`Should get object belonging to options.workspaces`, async () => {
const savedObject = {
type: 'dashboard',
id: 'dashboard_id',
attributes: {},
references: [],
workspaces: ['foo'],
};
mockedClient.get.mockResolvedValueOnce(savedObject);
const result = await wrapperClient.get(savedObject.type, savedObject.id, {
workspaces: savedObject.workspaces,
});
expect(mockedClient.get).toBeCalledWith(savedObject.type, savedObject.id, {
workspaces: savedObject.workspaces,
});
expect(result).toEqual(savedObject);
});

it(`Should get object belonging to the workspace in request`, async () => {
const savedObject = {
type: 'dashboard',
id: 'dashboard_id',
attributes: {},
references: [],
workspaces: ['foo'],
};
mockedClient.get.mockResolvedValueOnce(savedObject);
const result = await wrapperClient.get(savedObject.type, savedObject.id);
expect(mockedClient.get).toBeCalledWith(savedObject.type, savedObject.id, {});
expect(result).toEqual(savedObject);
});

it(`Should get object if the object type is workspace`, async () => {
const savedObject = {
type: 'workspace',
id: 'workspace_id',
attributes: {},
references: [],
};
mockedClient.get.mockResolvedValueOnce(savedObject);
const result = await wrapperClient.get(savedObject.type, savedObject.id);
expect(mockedClient.get).toBeCalledWith(savedObject.type, savedObject.id, {});
expect(result).toEqual(savedObject);
});

it(`Should get object if the object type is config`, async () => {
const savedObject = {
type: 'config',
id: 'config_id',
attributes: {},
references: [],
};
mockedClient.get.mockResolvedValueOnce(savedObject);
const result = await wrapperClient.get(savedObject.type, savedObject.id);
expect(mockedClient.get).toBeCalledWith(savedObject.type, savedObject.id, {});
expect(result).toEqual(savedObject);
});

it(`Should get object when there is no workspace in options/request`, async () => {
const workspaceIdConsumerWrapper = new WorkspaceIdConsumerWrapper(mockedWorkspaceClient);
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest();
updateWorkspaceState(mockRequest, {});
const mockedWrapperClient = workspaceIdConsumerWrapper.wrapperFactory({
client: mockedClient,
typeRegistry: requestHandlerContext.savedObjects.typeRegistry,
request: mockRequest,
});
const savedObject = {
type: 'dashboard',
id: 'dashboard_id',
attributes: {},
references: [],
};
mockedClient.get.mockResolvedValueOnce(savedObject);
const result = await mockedWrapperClient.get(savedObject.type, savedObject.id);
expect(mockedClient.get).toBeCalledWith(savedObject.type, savedObject.id, {});
expect(result).toEqual(savedObject);
});

it(`Should throw error when the object is not belong to the workspace`, async () => {
const savedObject = {
type: 'dashboard',
id: 'dashboard_id',
attributes: {},
references: [],
workspaces: ['bar'],
};
mockedClient.get.mockResolvedValueOnce(savedObject);
expect(wrapperClient.get(savedObject.type, savedObject.id)).rejects.toMatchInlineSnapshot(
`[Error: Saved object [dashboard/dashboard_id] not found]`
);
expect(mockedClient.get).toBeCalledWith(savedObject.type, savedObject.id, {});
});

it(`Should throw error when the object does not exist`, async () => {
mockedClient.get.mockRejectedValueOnce(SavedObjectsErrorHelpers.createGenericNotFoundError());
expect(wrapperClient.get('type', 'id')).rejects.toMatchInlineSnapshot(`[Error: Not Found]`);
expect(mockedClient.get).toHaveBeenCalledTimes(1);
});
});

describe('bulkGet', () => {
const payload = [
{ id: 'dashboard_id', type: 'dashboard' },
{ id: 'visualization_id', type: 'visualization' },
];
const savedObjects = [
{
type: 'dashboard',
id: 'dashboard_id',
attributes: {},
references: [],
workspaces: ['foo'],
},
{
type: 'visualization',
id: 'visualization_id',
attributes: {},
references: [],
workspaces: ['bar'],
},
];
const options = { workspaces: ['foo'] };
beforeEach(() => {
mockedClient.bulkGet.mockClear();
});

it(`Should bulkGet objects belonging to options.workspaces`, async () => {
mockedClient.bulkGet.mockResolvedValueOnce({ saved_objects: savedObjects });
const result = await wrapperClient.bulkGet(payload, options);
expect(mockedClient.bulkGet).toBeCalledWith(payload, options);
expect(result).toMatchInlineSnapshot(`
Object {
"saved_objects": Array [
Object {
"attributes": Object {},
"id": "dashboard_id",
"references": Array [],
"type": "dashboard",
"workspaces": Array [
"foo",
],
},
Object {
"attributes": Object {},
"error": Object {
"error": "Not Found",
"message": "Saved object [visualization/visualization_id] not found",
"statusCode": 404,
},
"id": "visualization_id",
"references": Array [],
"type": "visualization",
"workspaces": Array [
"bar",
],
},
],
}
`);
});

it(`Should bulkGet objects belonging to the workspace in request`, async () => {
mockedClient.bulkGet.mockResolvedValueOnce({ saved_objects: savedObjects });
const result = await wrapperClient.bulkGet(payload);
expect(mockedClient.bulkGet).toBeCalledWith(payload, {});
expect(result).toMatchInlineSnapshot(`
Object {
"saved_objects": Array [
Object {
"attributes": Object {},
"id": "dashboard_id",
"references": Array [],
"type": "dashboard",
"workspaces": Array [
"foo",
],
},
Object {
"attributes": Object {},
"error": Object {
"error": "Not Found",
"message": "Saved object [visualization/visualization_id] not found",
"statusCode": 404,
},
"id": "visualization_id",
"references": Array [],
"type": "visualization",
"workspaces": Array [
"bar",
],
},
],
}
`);
});

it(`Should bulkGet objects when there is no workspace in options/request`, async () => {
const workspaceIdConsumerWrapper = new WorkspaceIdConsumerWrapper(mockedWorkspaceClient);
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest();
updateWorkspaceState(mockRequest, {});
const mockedWrapperClient = workspaceIdConsumerWrapper.wrapperFactory({
client: mockedClient,
typeRegistry: requestHandlerContext.savedObjects.typeRegistry,
request: mockRequest,
});
mockedClient.bulkGet.mockResolvedValueOnce({ saved_objects: savedObjects });
const result = await mockedWrapperClient.bulkGet(payload);
expect(mockedClient.bulkGet).toBeCalledWith(payload, {});
expect(result).toEqual({ saved_objects: savedObjects });
});

it(`Should throw error when the objects do not exist`, async () => {
mockedClient.bulkGet.mockRejectedValueOnce(
SavedObjectsErrorHelpers.createGenericNotFoundError()
);
expect(wrapperClient.bulkGet(payload)).rejects.toMatchInlineSnapshot(`[Error: Not Found]`);
expect(mockedClient.bulkGet).toBeCalledWith(payload, {});
});
});
});
Loading
Loading