Skip to content

Commit

Permalink
refractor: change public objects logic
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 Aug 2, 2023
1 parent bc03d7c commit c61ebcd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
45 changes: 33 additions & 12 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ const getObjectsToFetch = (queue: BatchQueueEntry[]): ObjectTypeAndId[] => {
export class SavedObjectsClient {
private http: HttpSetup;
private batchQueue: BatchQueueEntry[];
private currentWorkspaceId?: string;
/**
* if currentWorkspaceId is undefined, it means
* we should not carry out workspace info when doing any operation.
*/
private currentWorkspaceId: string | undefined;

/**
* Throttled processing of get requests into bulk requests at 100ms interval
Expand Down Expand Up @@ -230,11 +234,11 @@ export class SavedObjectsClient {
this.batchQueue = [];
}

private async _getCurrentWorkspace(): Promise<string | null> {
return this.currentWorkspaceId || null;
private _getCurrentWorkspace(): string | undefined {
return this.currentWorkspaceId;
}

public async setCurrentWorkspace(workspaceId: string): Promise<boolean> {
public setCurrentWorkspace(workspaceId: string): boolean {
this.currentWorkspaceId = workspaceId;
return true;
}
Expand All @@ -260,7 +264,17 @@ export class SavedObjectsClient {
const query = {
overwrite: options.overwrite,
};
const currentWorkspaceId = await this._getCurrentWorkspace();
const currentWorkspaceId = this._getCurrentWorkspace();
let finalWorkspaces;
if (options.workspaces) {
finalWorkspaces = options.workspaces;
} else if (typeof currentWorkspaceId === 'string') {
/**
* When the currentWorkspaceId is an empty string instead of undefined
* It means user is creating object in public workspace.
*/
finalWorkspaces = currentWorkspaceId ? [currentWorkspaceId] : [PUBLIC_WORKSPACE];
}

const createRequest: Promise<SavedObject<T>> = this.savedObjectsFetch(path, {
method: 'POST',
Expand All @@ -269,9 +283,9 @@ export class SavedObjectsClient {
attributes,
migrationVersion: options.migrationVersion,
references: options.references,
...(options.workspaces || currentWorkspaceId
...(finalWorkspaces
? {
workspaces: options.workspaces || [currentWorkspaceId],
workspaces: finalWorkspaces,
}
: {}),
}),
Expand Down Expand Up @@ -366,14 +380,21 @@ export class SavedObjectsClient {
workspaces: 'workspaces',
};

const workspaces = [
...(options.workspaces || [await this._getCurrentWorkspace()]),
PUBLIC_WORKSPACE,
].filter((item) => item);
const currentWorkspaceId = this._getCurrentWorkspace();
let finalWorkspaces;
if (options.workspaces) {
finalWorkspaces = options.workspaces;
} else if (typeof currentWorkspaceId === 'string') {
finalWorkspaces = currentWorkspaceId ? [PUBLIC_WORKSPACE, currentWorkspaceId] : undefined;
}

const renamedQuery = renameKeys<SavedObjectsFindOptions, any>(renameMap, {
...options,
workspaces,
...(finalWorkspaces
? {
workspaces: finalWorkspaces,
}
: {}),
});
const query = pick.apply(null, [renamedQuery, ...Object.values<string>(renameMap)]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type KueryNode = any;

import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry';
import { ALL_NAMESPACES_STRING, DEFAULT_NAMESPACE_STRING } from '../utils';
import { PUBLIC_WORKSPACE } from '../../../../../utils';

/**
* Gets the types based on the type. Uses mappings to support
Expand Down Expand Up @@ -143,14 +142,6 @@ function getClauseForWorkspace(workspace: string) {
};
}

if (workspace === PUBLIC_WORKSPACE) {
return {
bool: {
must_not: [{ exists: { field: 'workspaces' } }],
},
};
}

return {
bool: {
must: [{ term: { workspaces: workspace } }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ export class WorkspaceSavedObjectsClientWrapper {
const findWithWorkspacePermissionControl = async <T = unknown>(
options: SavedObjectsFindOptions
) => {
if (options.type === WORKSPACE_TYPE) {
return await wrapperOptions.client.find<T>(options);
}
if (options.workspaces) {
options.workspaces = options.workspaces.filter(
async (workspaceId) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
}

private get workspaceIdQuery() {
return this.state.workspaceId ? [PUBLIC_WORKSPACE, this.state.workspaceId] : [PUBLIC_WORKSPACE];
return this.state.workspaceId
? Array.from(new Set([PUBLIC_WORKSPACE, this.state.workspaceId]))
: undefined;
}

componentDidMount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function WorkspaceColumn({ coreSetup, workspaces, record }: WorkspaceColumnProps
});
wsLookUp?.set('public', publicWsName);

if (!workspaces || workspaces?.includes(PUBLIC_WORKSPACE)) {
if (workspaces?.includes(PUBLIC_WORKSPACE)) {
return <EuiText>{publicWsName}</EuiText>;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep
if (this.coreStart) {
return this.coreStart.workspaces.client.currentWorkspaceId$.subscribe(
(currentWorkspaceId) => {
this.coreStart?.savedObjects.client.setCurrentWorkspace(currentWorkspaceId);
this.coreStart?.savedObjects.client.setCurrentWorkspace(currentWorkspaceId || '');
}
);
}
Expand Down

0 comments on commit c61ebcd

Please sign in to comment.