Skip to content

Commit

Permalink
feat: add error message
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Apr 16, 2024
1 parent 4097e94 commit e61910d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,19 @@ describe('workspace_id_consumer integration test', () => {
},
])
.expect(200);
expect(createResultFoo.body).toEqual({
saved_objects: [],
});
expect(createResultFoo.body.saved_objects[0].error).toEqual(
expect.objectContaining({
message: "Unsupport type in workspace: 'config' is not allowed to import in workspace.",
statusCode: 400,
})
);
expect(createResultFoo.body.saved_objects[1].error).toEqual(
expect.objectContaining({
message:
"Unsupport type in workspace: 'data-source' is not allowed to import in workspace.",
statusCode: 400,
})
);

// data source and advanced settings should not be found within the workspace
const findDataSourceResult = await osdTestServer.request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SavedObject } from '../../../../core/public';
import { httpServerMock, savedObjectsClientMock, coreMock } from '../../../../core/server/mocks';
import { WorkspaceIdConsumerWrapper } from './workspace_id_consumer_wrapper';
import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../../../plugins/data_source/common';
import { UI_SETTINGS_SAVED_OBJECTS_TYPE } from '../../../../core/server';

describe('WorkspaceIdConsumerWrapper', () => {
const requestHandlerContext = coreMock.createRequestHandlerContext();
Expand Down Expand Up @@ -89,7 +88,8 @@ describe('WorkspaceIdConsumerWrapper', () => {
});

it(`Should skip the objects when trying to create unallowed type within a workspace`, async () => {
await wrapperClient.bulkCreate([
mockedClient.bulkCreate.mockResolvedValueOnce({ saved_objects: [] });
const result = await wrapperClient.bulkCreate([
getSavedObject({
type: 'config',
id: 'foo',
Expand All @@ -103,6 +103,19 @@ describe('WorkspaceIdConsumerWrapper', () => {
expect(mockedClient.bulkCreate).toBeCalledWith([], {
workspaces: ['foo'],
});
expect(result.saved_objects[0].error).toEqual(
expect.objectContaining({
message: "Unsupport type in workspace: 'config' is not allowed to import in workspace.",
statusCode: 400,
})
);
expect(result.saved_objects[1].error).toEqual(
expect.objectContaining({
message:
"Unsupport type in workspace: 'data-source' is not allowed to import in workspace.",
statusCode: 400,
})
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SavedObjectsCheckConflictsObject,
OpenSearchDashboardsRequest,
SavedObjectsFindOptions,
SavedObjectsErrorHelpers,
} from '../../../../core/server';
import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../../../plugins/data_source/common';

Expand Down Expand Up @@ -67,30 +68,60 @@ export class WorkspaceIdConsumerWrapper {
attributes,
this.formatWorkspaceIdParams(wrapperOptions.request, options)
),
bulkCreate: <T = unknown>(
bulkCreate: async <T = unknown>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
options: SavedObjectsCreateOptions = {}
) => {
const { workspaces } = this.formatWorkspaceIdParams(wrapperOptions.request, options);
const allowedSavedObjects = objects.filter((item) => {
const disallowedSavedObjects: Array<SavedObjectsBulkCreateObject<T>> = [];
const allowedSavedObjects: Array<SavedObjectsBulkCreateObject<T>> = [];
objects.forEach((item) => {
const isImportIntoWorkspace = workspaces?.length || item.workspaces?.length;
// config can not be created inside a workspace
if (this.isConfigType(item.type) && isImportIntoWorkspace) {
return false;
disallowedSavedObjects.push(item);
return;
}

// For 2.14, data source can only be created without workspace info
if (this.isDataSourceType(item.type) && isImportIntoWorkspace) {
return false;
disallowedSavedObjects.push(item);
return;
}

return true;
allowedSavedObjects.push(item);
return;
});

return wrapperOptions.client.bulkCreate(
if (!disallowedSavedObjects.length) {
return await wrapperOptions.client.bulkCreate(
objects,
this.formatWorkspaceIdParams(wrapperOptions.request, options)
);
}

const allowedSavedObjectsBulkCreateResult = await wrapperOptions.client.bulkCreate(
allowedSavedObjects,
this.formatWorkspaceIdParams(wrapperOptions.request, options)
);

return {
saved_objects: [
...allowedSavedObjectsBulkCreateResult.saved_objects,
...disallowedSavedObjects.map((item) => ({
references: [],
id: '',
...item,
error: {
...SavedObjectsErrorHelpers.decorateBadRequestError(
new Error(`'${item.type}' is not allowed to import in workspace.`),
'Unsupport type in workspace'
).output.payload,
metadata: { isNotOverwritable: true },
},
})),
],
};
},
checkConflicts: (
objects: SavedObjectsCheckConflictsObject[] = [],
Expand Down

0 comments on commit e61910d

Please sign in to comment.