Skip to content

Commit

Permalink
feat: unified workspaces parameters when parsing from router
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Mar 25, 2024
1 parent f4da902 commit 5d9073d
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/import/check_conflicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function checkConflicts({
});
const checkConflictsResult = await savedObjectsClient.checkConflicts(objectsToCheck, {
namespace,
workspaces,
...(workspaces ? { workspaces } : {}),
});
const errorMap = checkConflictsResult.errors.reduce(
(acc, { type, id, error }) => acc.set(`${type}:${id}`, error),
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/routes/bulk_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const registerBulkCreateRoute = (router: IRouter) => {
: undefined;
const result = await context.core.savedObjects.client.bulkCreate(req.body, {
overwrite,
workspaces,
...(workspaces ? { workspaces } : {}),
});
return res.ok({ body: result });
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const registerCreateRoute = (router: IRouter) => {
migrationVersion,
references,
initialNamespaces,
workspaces,
...(workspaces ? { workspaces } : {}),
};
const result = await context.core.savedObjects.client.create(type, attributes, options);
return res.ok({ body: result });
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/routes/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const registerExportRoute = (router: IRouter, config: SavedObjectConfig)
exportSizeLimit: maxImportExportSize,
includeReferencesDeep,
excludeExportDetails,
workspaces,
...(workspaces ? { workspaces } : {}),
});

const docsToExport: string[] = await createPromiseFromStreams([
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/routes/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const registerFindRoute = (router: IRouter) => {
fields: typeof query.fields === 'string' ? [query.fields] : query.fields,
filter: query.filter,
namespaces,
workspaces,
...(workspaces ? { workspaces } : {}),
});

return res.ok({ body: result });
Expand Down
4 changes: 0 additions & 4 deletions src/core/server/saved_objects/service/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ export interface SavedObjectsCreateOptions extends SavedObjectsBaseOptions {
* Note: this can only be used for multi-namespace object types.
*/
initialNamespaces?: string[];
/**
* workspaces the new created objects belong to
*/
workspaces?: string[];
/** permission control describe by ACL object */
permissions?: Permissions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export interface SavedObjectsBaseOptions {
/** Specify the namespace for this operation */
namespace?: string;
/** Specify the workspaces for this operation */
workspaces?: string[];
workspaces?: string[] | null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function () {
},
{
id: version,
workspaces: null,
}
);
});
Expand Down Expand Up @@ -133,6 +134,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function () {
},
{
id: version,
workspaces: null,
}
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export async function createOrUpgradeSavedConfig(
await savedObjectsClient.create('config', attributes, {
id: version,
// config is a global object that should not belong to any of the workspaces
// declare it as empty array to prevent it being created as a workspace object
workspaces: [],
// declare it as null to prevent it being created as a workspace object
workspaces: null,
});
} catch (error) {
if (handleWriteErrors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('WorkspaceIdConsumerWrapper', () => {
);
});

it(`Should use options.workspaces there is workspaces inside options`, async () => {
it(`Should use options.workspaces when there is no workspaces inside options`, async () => {
await wrapperClient.create(
'dashboard',
{
Expand All @@ -59,7 +59,7 @@ describe('WorkspaceIdConsumerWrapper', () => {
{
id: 'dashboard:foo',
overwrite: true,
workspaces: undefined,
workspaces: null,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import {
SavedObjectsFindOptions,
} from '../../../../core/server';

type WorkspaceOptions =
| {
workspaces?: string[];
}
| undefined;
type WorkspaceOptions = Pick<SavedObjectsBaseOptions, 'workspaces'> | undefined;

export class WorkspaceIdConsumerWrapper {
private formatWorkspaceIdParams<T extends WorkspaceOptions>(
Expand All @@ -30,8 +26,8 @@ export class WorkspaceIdConsumerWrapper {
const workspaceIdParsedFromRequest = workspaceState?.id;
const workspaceIdsInUserOptions = options?.workspaces;
let finalWorkspaces: string[] = [];
if (workspaceIdsInUserOptions) {
finalWorkspaces = workspaceIdsInUserOptions;
if (options?.hasOwnProperty('workspaces')) {
finalWorkspaces = workspaceIdsInUserOptions || [];
} else if (workspaceIdParsedFromRequest) {
finalWorkspaces = [workspaceIdParsedFromRequest];
}
Expand Down

0 comments on commit 5d9073d

Please sign in to comment.