From a6ca40209eae12b6f6aba15947a38c463c09372f Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Mon, 11 Mar 2024 17:57:24 +0800 Subject: [PATCH] [Workspace] Consume workspace id in saved object client (#6014) (#290) * feat: consume current workspace in saved objects management and saved objects client * feat: add unit test * feat: add unit test for each change * fix: update snapshot of unit test * fix: unit test * fix: unit test * fix: unit test * feat: update snapshot * revert: saved object management changes * feat: add CHANGELOG * feat: address some comment * feat: address comment * feat: remove useless return * fix: bootstrap error * feat: update CHANGELOG * feat: update CHANGELOG * feat: optimize code * feat: update comment --------- 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