Skip to content

Commit

Permalink
[Workspace] Consume workspace id in saved object client (opensearch-p…
Browse files Browse the repository at this point in the history
…roject#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 <[email protected]>
  • Loading branch information
SuZhou-Joe authored Mar 11, 2024
1 parent 58f8ea4 commit a6ca402
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -238,9 +262,8 @@ export class SavedObjectsClient {
this.batchQueue = [];
}

public setCurrentWorkspace(workspaceId: string): boolean {
public setCurrentWorkspace(workspaceId: string) {
this.currentWorkspaceId = workspaceId;
return true;
}

/**
Expand All @@ -265,26 +288,14 @@ 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<SavedObject<T>> = this.savedObjectsFetch(path, {
method: 'POST',
query,
body: JSON.stringify({
attributes,
migrationVersion: options.migrationVersion,
references: options.references,
...(finalWorkspaces
? {
workspaces: finalWorkspaces,
}
: {}),
...this.formatWorkspacesParams(options),
}),
});

Expand All @@ -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<SavedObjectsApi['bulkCreate']> = this.savedObjectsFetch(path, {
method: 'POST',
query,
query: {
...query,
...this.formatWorkspacesParams(options),
},
body: JSON.stringify(objects),
});
return request.then((resp) => {
Expand Down Expand Up @@ -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<SavedObjectsFindOptions, any>(renameMap, {
...options,
...(finalWorkspaces
? {
workspaces: finalWorkspaces,
}
: {}),
...this.formatWorkspacesParams(options),
});
const query = pick.apply(null, [renamedQuery, ...Object.values<string>(renameMap)]) as Partial<
Record<string, any>
Expand Down

0 comments on commit a6ca402

Please sign in to comment.