From bc180b0e91124eda160ff10de46d427c49210080 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Sat, 9 Mar 2024 09:42:43 +0800 Subject: [PATCH] [Workspace] Consume workspace id in saved object client (#6014) * feat: consume current workspace in saved objects management and saved objects client Signed-off-by: SuZhou-Joe * feat: add unit test Signed-off-by: SuZhou-Joe * feat: add unit test for each change Signed-off-by: SuZhou-Joe * fix: update snapshot of unit test Signed-off-by: SuZhou-Joe * fix: unit test Signed-off-by: SuZhou-Joe * fix: unit test Signed-off-by: SuZhou-Joe * fix: unit test Signed-off-by: SuZhou-Joe * feat: update snapshot Signed-off-by: SuZhou-Joe * revert: saved object management changes Signed-off-by: SuZhou-Joe * feat: add CHANGELOG Signed-off-by: SuZhou-Joe * feat: address some comment Signed-off-by: SuZhou-Joe * feat: address comment Signed-off-by: SuZhou-Joe * feat: remove useless return Signed-off-by: SuZhou-Joe * fix: bootstrap error Signed-off-by: SuZhou-Joe * feat: update CHANGELOG Signed-off-by: SuZhou-Joe * feat: update CHANGELOG Signed-off-by: SuZhou-Joe * feat: optimize code Signed-off-by: SuZhou-Joe * feat: update comment Signed-off-by: SuZhou-Joe --------- Signed-off-by: SuZhou-Joe --- .../saved_objects/saved_objects_client.ts | 75 ++++++++----------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/src/core/public/saved_objects/saved_objects_client.ts b/src/core/public/saved_objects/saved_objects_client.ts index 329e9f316faf..f415e9c58596 100644 --- a/src/core/public/saved_objects/saved_objects_client.ts +++ b/src/core/public/saved_objects/saved_objects_client.ts @@ -190,11 +190,35 @@ export class SavedObjectsClient { private http: HttpSetup; private batchQueue: BatchQueueEntry[]; /** - * if currentWorkspaceId is undefined, it means - * we should not carry out workspace info when doing any operation. + * The currentWorkspaceId may be undefined when workspace plugin is not enabled. */ private currentWorkspaceId: string | undefined; + /** + * Check if workspaces field present in given options, if so, overwrite the current workspace id. + * @param options + * @returns + */ + private formatWorkspacesParams(options: { + workspaces?: SavedObjectsCreateOptions['workspaces']; + }): { workspaces: string[] } | {} { + const currentWorkspaceId = this.currentWorkspaceId; + let finalWorkspaces; + if (options.hasOwnProperty('workspaces')) { + finalWorkspaces = options.workspaces; + } else if (typeof currentWorkspaceId === 'string') { + finalWorkspaces = [currentWorkspaceId]; + } + + if (finalWorkspaces) { + return { + workspaces: finalWorkspaces, + }; + } + + return {}; + } + /** * Throttled processing of get requests into bulk requests at 100ms interval */ @@ -238,9 +262,8 @@ export class SavedObjectsClient { this.batchQueue = []; } - public setCurrentWorkspace(workspaceId: string): boolean { + public setCurrentWorkspace(workspaceId: string) { this.currentWorkspaceId = workspaceId; - return true; } /** @@ -265,14 +288,6 @@ export class SavedObjectsClient { overwrite: options.overwrite, }; - const currentWorkspaceId = this.currentWorkspaceId; - let finalWorkspaces; - if (options.hasOwnProperty('workspaces')) { - finalWorkspaces = options.workspaces; - } else if (typeof currentWorkspaceId === 'string') { - finalWorkspaces = [currentWorkspaceId]; - } - const createRequest: Promise> = this.savedObjectsFetch(path, { method: 'POST', query, @@ -280,11 +295,7 @@ export class SavedObjectsClient { attributes, migrationVersion: options.migrationVersion, references: options.references, - ...(finalWorkspaces - ? { - workspaces: finalWorkspaces, - } - : {}), + ...this.formatWorkspacesParams(options), }), }); @@ -305,21 +316,13 @@ export class SavedObjectsClient { ) => { const path = this.getPath(['_bulk_create']); const query: HttpFetchOptions['query'] = { overwrite: options.overwrite }; - const currentWorkspaceId = this.currentWorkspaceId; - let finalWorkspaces; - if (options.hasOwnProperty('workspaces')) { - finalWorkspaces = options.workspaces; - } else if (typeof currentWorkspaceId === 'string') { - finalWorkspaces = [currentWorkspaceId]; - } - - if (finalWorkspaces) { - query.workspaces = finalWorkspaces; - } const request: ReturnType = this.savedObjectsFetch(path, { method: 'POST', - query, + query: { + ...query, + ...this.formatWorkspacesParams(options), + }, body: JSON.stringify(objects), }); return request.then((resp) => { @@ -388,21 +391,9 @@ export class SavedObjectsClient { workspaces: 'workspaces', }; - const currentWorkspaceId = this.currentWorkspaceId; - let finalWorkspaces; - if (options.hasOwnProperty('workspaces')) { - finalWorkspaces = options.workspaces; - } else if (typeof currentWorkspaceId === 'string') { - finalWorkspaces = Array.from(new Set([currentWorkspaceId])); - } - const renamedQuery = renameKeys(renameMap, { ...options, - ...(finalWorkspaces - ? { - workspaces: finalWorkspaces, - } - : {}), + ...this.formatWorkspacesParams(options), }); const query = pick.apply(null, [renamedQuery, ...Object.values(renameMap)]) as Partial< Record