Skip to content

Commit

Permalink
feat: change the implementation
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 15, 2024
1 parent 395f6b0 commit 0fe0d37
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ export {
StringValidation,
StringValidationRegex,
StringValidationRegexString,
UI_SETTINGS_SAVED_OBJECTS_TYPE,
} from './ui_settings';

export {
Expand Down
2 changes: 0 additions & 2 deletions src/core/server/ui_settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ export {
StringValidationRegex,
StringValidationRegexString,
} from './types';

export { UI_SETTINGS_SAVED_OBJECTS_TYPE } from './saved_objects/ui_settings';
4 changes: 1 addition & 3 deletions src/core/server/ui_settings/saved_objects/ui_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
import { SavedObjectsType } from '../../saved_objects';
import { migrations } from './migrations';

export const UI_SETTINGS_SAVED_OBJECTS_TYPE = 'config';

export const uiSettingsType: SavedObjectsType = {
name: UI_SETTINGS_SAVED_OBJECTS_TYPE,
name: 'config',
hidden: false,
namespaceType: 'single',
mappings: {
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ describe('WorkspaceIdConsumerWrapper', () => {
expect(() =>
wrapperClient.bulkCreate([
getSavedObject({
type: DATA_SOURCE_SAVED_OBJECT_TYPE,
type: 'config',
id: 'foo',
}),
])
).toThrow('type: data-source is not allowed to create within a workspace.');
).toThrow('type: config is not allowed to create within a workspace.');

expect(() =>
wrapperClient.bulkCreate([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
SavedObjectsCheckConflictsObject,
OpenSearchDashboardsRequest,
SavedObjectsFindOptions,
UI_SETTINGS_SAVED_OBJECTS_TYPE,
SavedObjectsErrorHelpers,
} from '../../../../core/server';
import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../../../plugins/data_source/common';

type WorkspaceOptions = Pick<SavedObjectsBaseOptions, 'workspaces'> | undefined;

const UI_SETTINGS_SAVED_OBJECTS_TYPE = 'config';

export class WorkspaceIdConsumerWrapper {
private formatWorkspaceIdParams<T extends WorkspaceOptions>(
request: OpenSearchDashboardsRequest,
Expand Down Expand Up @@ -72,30 +72,24 @@ export class WorkspaceIdConsumerWrapper {
options: SavedObjectsCreateOptions = {}
) => {
const { workspaces } = this.formatWorkspaceIdParams(wrapperOptions.request, options);
const disallowedSavedObjects = objects.filter((item) => {
// If create out of workspace, allow the operation
if (!workspaces?.length && !item.workspaces?.length) {
const allowedSavedObjects = objects.filter((item) => {
const isImportIntoWorkspace = workspaces?.length || item.workspaces?.length;
// config can not be created inside a workspace
if (this.isConfigType(item.type) && isImportIntoWorkspace) {
return false;
}

// config and data-sources can not be created inside a workspace
return this.isConfigType(item.type) || this.isDataSourceType(item.type);
});

if (!disallowedSavedObjects?.length) {
return wrapperOptions.client.bulkCreate(
objects,
this.formatWorkspaceIdParams(wrapperOptions.request, options)
);
}
// For 2.14, data source can only be created without workspace info
if (this.isDataSourceType(item.type) && isImportIntoWorkspace) {
return false;
}

const disallowedTypes = [...new Set(disallowedSavedObjects.map((item) => item.type))];
return true;
});

throw SavedObjectsErrorHelpers.decorateBadRequestError(
new Error(''),
`${disallowedTypes.map((item) => `type: ${item}`).join(', ')} ${
disallowedTypes.length > 1 ? 'are' : 'is'
} not allowed to create within a workspace.`
return wrapperOptions.client.bulkCreate(
allowedSavedObjects,
this.formatWorkspaceIdParams(wrapperOptions.request, options)
);
},
checkConflicts: (
Expand Down

0 comments on commit 0fe0d37

Please sign in to comment.