-
Notifications
You must be signed in to change notification settings - Fork 894
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][Bug] Check if workspaces exists when creating saved objects #8739
base: main
Are you sure you want to change the base?
Changes from all commits
d1c32ce
6df8a76
c11a3fb
f9dad70
5bcfd2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fix: | ||
- [Workspace] [Bug] Check if workspaces exists when creating saved objects. ([#8739](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8739)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import { | |
OpenSearchDashboardsRequest, | ||
SavedObjectsFindOptions, | ||
SavedObjectsErrorHelpers, | ||
SavedObjectsClientWrapperOptions, | ||
} from '../../../../core/server'; | ||
import { IWorkspaceClientImpl } from '../types'; | ||
|
||
|
@@ -48,25 +49,71 @@ export class WorkspaceIdConsumerWrapper { | |
return type === UI_SETTINGS_SAVED_OBJECTS_TYPE; | ||
} | ||
|
||
private async checkWorkspacesExist( | ||
finalOptions: SavedObjectsCreateOptions, | ||
wrapperOptions: SavedObjectsClientWrapperOptions | ||
) { | ||
if (finalOptions.workspaces?.length) { | ||
let invalidWorkspaces: string[] = []; | ||
// If only has one workspace, we should use get to optimize performance | ||
if (finalOptions.workspaces.length === 1) { | ||
const workspaceGet = await this.workspaceClient.get( | ||
{ request: wrapperOptions.request }, | ||
finalOptions.workspaces[0] | ||
); | ||
if (!workspaceGet.success) { | ||
invalidWorkspaces = [finalOptions.workspaces[0]]; | ||
} | ||
} else { | ||
const workspaceList = await this.workspaceClient.list( | ||
{ | ||
request: wrapperOptions.request, | ||
}, | ||
{ | ||
perPage: 9999, | ||
} | ||
); | ||
if (workspaceList.success) { | ||
const workspaceIdsSet = new Set( | ||
workspaceList.result.workspaces.map((workspace) => workspace.id) | ||
); | ||
invalidWorkspaces = finalOptions.workspaces.filter( | ||
(targetWorkspace) => !workspaceIdsSet.has(targetWorkspace) | ||
); | ||
} | ||
} | ||
|
||
if (invalidWorkspaces.length > 0) { | ||
throw SavedObjectsErrorHelpers.decorateBadRequestError( | ||
new Error( | ||
i18n.translate('workspace.id_consumer.invalid', { | ||
yubonluo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defaultMessage: 'Invalid workspaces: {invalidWorkspaces}', | ||
values: { invalidWorkspaces: invalidWorkspaces.join(', ') }, | ||
}) | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => { | ||
return { | ||
...wrapperOptions.client, | ||
create: <T>(type: string, attributes: T, options: SavedObjectsCreateOptions = {}) => | ||
wrapperOptions.client.create( | ||
type, | ||
attributes, | ||
this.isConfigType(type) | ||
? options | ||
: this.formatWorkspaceIdParams(wrapperOptions.request, options) | ||
), | ||
bulkCreate: <T = unknown>( | ||
create: async <T>(type: string, attributes: T, options: SavedObjectsCreateOptions = {}) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we do the check for addToWorkspace? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we also need to check the update and bulk update api? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be fine for update and bulkUpdate because these 2 APIs won't touch |
||
const finalOptions = this.isConfigType(type) | ||
? options | ||
: this.formatWorkspaceIdParams(wrapperOptions.request, options); | ||
await this.checkWorkspacesExist(finalOptions, wrapperOptions); | ||
return wrapperOptions.client.create(type, attributes, finalOptions); | ||
}, | ||
bulkCreate: async <T = unknown>( | ||
objects: Array<SavedObjectsBulkCreateObject<T>>, | ||
options: SavedObjectsCreateOptions = {} | ||
) => | ||
wrapperOptions.client.bulkCreate( | ||
objects, | ||
this.formatWorkspaceIdParams(wrapperOptions.request, options) | ||
), | ||
) => { | ||
const finalOptions = this.formatWorkspaceIdParams(wrapperOptions.request, options); | ||
await this.checkWorkspacesExist(finalOptions, wrapperOptions); | ||
return wrapperOptions.client.bulkCreate(objects, finalOptions); | ||
}, | ||
checkConflicts: ( | ||
objects: SavedObjectsCheckConflictsObject[] = [], | ||
options: SavedObjectsBaseOptions = {} | ||
|
@@ -84,46 +131,7 @@ export class WorkspaceIdConsumerWrapper { | |
this.isConfigType(options.type as string) && options.sortField === 'buildNum' | ||
? options | ||
: this.formatWorkspaceIdParams(wrapperOptions.request, options); | ||
if (finalOptions.workspaces?.length) { | ||
let isAllTargetWorkspaceExisting = false; | ||
// If only has one workspace, we should use get to optimize performance | ||
if (finalOptions.workspaces.length === 1) { | ||
const workspaceGet = await this.workspaceClient.get( | ||
{ request: wrapperOptions.request }, | ||
finalOptions.workspaces[0] | ||
); | ||
if (workspaceGet.success) { | ||
isAllTargetWorkspaceExisting = true; | ||
} | ||
} else { | ||
const workspaceList = await this.workspaceClient.list( | ||
{ | ||
request: wrapperOptions.request, | ||
}, | ||
{ | ||
perPage: 9999, | ||
} | ||
); | ||
if (workspaceList.success) { | ||
const workspaceIdsSet = new Set( | ||
workspaceList.result.workspaces.map((workspace) => workspace.id) | ||
); | ||
isAllTargetWorkspaceExisting = finalOptions.workspaces.every((targetWorkspace) => | ||
workspaceIdsSet.has(targetWorkspace) | ||
); | ||
} | ||
} | ||
|
||
if (!isAllTargetWorkspaceExisting) { | ||
throw SavedObjectsErrorHelpers.decorateBadRequestError( | ||
new Error( | ||
i18n.translate('workspace.id_consumer.invalid', { | ||
defaultMessage: 'Invalid workspaces', | ||
}) | ||
) | ||
); | ||
} | ||
} | ||
await this.checkWorkspacesExist(finalOptions, wrapperOptions); | ||
return wrapperOptions.client.find(finalOptions); | ||
}, | ||
bulkGet: wrapperOptions.client.bulkGet, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice refactor.