From 023fa9befb84e588001bed54840b8fc43930962d Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Thu, 7 Mar 2024 17:19:05 +0800 Subject: [PATCH] default workspace Signed-off-by: Hailong Cui fix test case Signed-off-by: Hailong Cui add test case Signed-off-by: Hailong Cui remove hide import Signed-off-by: Hailong Cui --- src/core/public/index.ts | 2 +- src/core/server/index.ts | 7 +- src/core/utils/constants.ts | 2 + src/core/utils/index.ts | 7 +- .../saved_objects_table.test.tsx.snap | 1 - .../objects_table/components/header.test.tsx | 19 ----- .../objects_table/components/header.tsx | 32 ++++---- .../saved_objects_table.test.tsx | 73 ++++++++++++++++--- .../objects_table/saved_objects_table.tsx | 30 ++++++-- .../server/routes/find.ts | 1 + .../server/routes/scroll_count.ts | 7 +- src/plugins/workspace/public/plugin.test.ts | 4 +- 12 files changed, 121 insertions(+), 64 deletions(-) diff --git a/src/core/public/index.ts b/src/core/public/index.ts index 271d68f4cf1..bcc04c31371 100644 --- a/src/core/public/index.ts +++ b/src/core/public/index.ts @@ -356,4 +356,4 @@ export { __osdBootstrap__ } from './osd_bootstrap'; export { WorkspacesStart, WorkspacesSetup, WorkspacesService } from './workspace'; -export { WORKSPACE_TYPE, cleanWorkspaceId } from '../utils'; +export { WORKSPACE_TYPE, cleanWorkspaceId, DEFAULT_WORKSPACE_ID } from '../utils'; diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 1af8818ab0f..84ee65dcb19 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -354,7 +354,12 @@ export { } from './metrics'; export { AppCategory, WorkspaceAttribute } from '../types'; -export { DEFAULT_APP_CATEGORIES, PUBLIC_WORKSPACE_ID, WORKSPACE_TYPE } from '../utils'; +export { + DEFAULT_APP_CATEGORIES, + PUBLIC_WORKSPACE_ID, + WORKSPACE_TYPE, + DEFAULT_WORKSPACE_ID, +} from '../utils'; export { SavedObject, diff --git a/src/core/utils/constants.ts b/src/core/utils/constants.ts index 2ea3b423a7c..5fe1eeec5de 100644 --- a/src/core/utils/constants.ts +++ b/src/core/utils/constants.ts @@ -8,3 +8,5 @@ export const WORKSPACE_TYPE = 'workspace'; export const WORKSPACE_PATH_PREFIX = '/w'; export const PUBLIC_WORKSPACE_ID = 'public'; + +export const DEFAULT_WORKSPACE_ID = 'default'; diff --git a/src/core/utils/index.ts b/src/core/utils/index.ts index ec797186556..e2f5fd90460 100644 --- a/src/core/utils/index.ts +++ b/src/core/utils/index.ts @@ -38,4 +38,9 @@ export { } from './context'; export { DEFAULT_APP_CATEGORIES } from './default_app_categories'; export { getWorkspaceIdFromUrl, formatUrlWithWorkspaceId, cleanWorkspaceId } from './workspace'; -export { WORKSPACE_PATH_PREFIX, PUBLIC_WORKSPACE_ID, WORKSPACE_TYPE } from './constants'; +export { + WORKSPACE_PATH_PREFIX, + PUBLIC_WORKSPACE_ID, + WORKSPACE_TYPE, + DEFAULT_WORKSPACE_ID, +} from './constants'; diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/__snapshots__/saved_objects_table.test.tsx.snap b/src/plugins/saved_objects_management/public/management_section/objects_table/__snapshots__/saved_objects_table.test.tsx.snap index 914660040f2..c501b2c7457 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/__snapshots__/saved_objects_table.test.tsx.snap +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/__snapshots__/saved_objects_table.test.tsx.snap @@ -203,7 +203,6 @@ exports[`SavedObjectsTable should render normally 1`] = ` >
{ onRefresh: () => {}, totalCount: 4, filteredCount: 2, - title: '', }; const component = shallow(
); @@ -48,21 +47,3 @@ describe('Header', () => { expect(component).toMatchSnapshot(); }); }); - -describe('Header - workspace enabled', () => { - it('should hide `Import` button for application home state', () => { - const props = { - onExportAll: () => {}, - onImport: () => {}, - onRefresh: () => {}, - totalCount: 4, - filteredCount: 2, - hideImport: true, - title: 'Saved Objectes', - }; - - const component = shallow(
); - - expect(component.find('EuiButtonEmpty[data-test-subj="importObjects"]').exists()).toBe(false); - }); -}); diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/components/header.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/components/header.tsx index 2e19f897342..a22e349d524 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/components/header.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/components/header.tsx @@ -45,15 +45,11 @@ export const Header = ({ onImport, onRefresh, filteredCount, - hideImport, - title, }: { onExportAll: () => void; onImport: () => void; onRefresh: () => void; filteredCount: number; - hideImport?: boolean; - title: string; }) => ( @@ -86,21 +82,19 @@ export const Header = ({ /> - {!hideImport && ( - - - - - - )} + + + + + { ); expect(component.state('selectedSavedObjects').length).toBe(0); }); + }); + describe('workspace filter', () => { it('show workspace filter when workspace turn on and not in any workspace', async () => { const applications = applicationServiceMock.createStartContract(); applications.capabilities = { @@ -690,9 +693,10 @@ describe('SavedObjectsTable', () => { expect(filters.length).toBe(2); expect(filters[0].field).toBe('type'); expect(filters[1].field).toBe('workspaces'); - expect(filters[1].options.length).toBe(2); + expect(filters[1].options.length).toBe(3); expect(filters[1].options[0].value).toBe('foo'); expect(filters[1].options[1].value).toBe('bar'); + expect(filters[1].options[2].value).toBe(DEFAULT_WORKSPACE_ID); }); it('show workspace filter when workspace turn on and enter a workspace', async () => { @@ -740,7 +744,8 @@ describe('SavedObjectsTable', () => { expect(wsFilter[0].options[0].value).toBe('foo'); }); - it('workspace exists in query options when workspace on', async () => { + it('workspace exists in find options when workspace on', async () => { + findObjectsMock.mockClear(); const applications = applicationServiceMock.createStartContract(); applications.capabilities = { navLinks: {}, @@ -777,17 +782,61 @@ describe('SavedObjectsTable', () => { // Ensure the state changes are reflected component.update(); - waitFor( - () => { - expect(http.get).toHaveBeenCalledWith( - expect.stringMatching('/api/opensearch-dashboards/management/saved_objects/_find'), - expect.objectContaining({ - workspaces: expect.arrayContaining(['workspace1']), - }) - ); + await waitFor(() => { + expect(findObjectsMock).toBeCalledWith( + http, + expect.objectContaining({ + workspaces: expect.arrayContaining(['workspace1']), + }) + ); + }); + }); + + it('workspace exists in find options when workspace on and not in any workspace', async () => { + findObjectsMock.mockClear(); + const applications = applicationServiceMock.createStartContract(); + applications.capabilities = { + navLinks: {}, + management: {}, + catalogue: {}, + savedObjectsManagement: { + read: true, + edit: false, + delete: false, }, - { timeout: 1000 } - ); + workspaces: { + enabled: true, + }, + }; + + const workspaceList: WorkspaceObject[] = [ + { + id: 'workspace1', + name: 'foo', + }, + { + id: 'workspace2', + name: 'bar', + }, + ]; + workspaces.workspaceList$.next(workspaceList); + + const component = shallowRender({ applications, workspaces }); + + // Ensure all promises resolve + await new Promise((resolve) => process.nextTick(resolve)); + // Ensure the state changes are reflected + component.update(); + + await waitFor(() => { + expect(findObjectsMock).toBeCalledWith( + http, + expect.objectContaining({ + workspaces: expect.arrayContaining(['workspace1', 'default']), + workspacesSearchOperator: expect.stringMatching('OR'), + }) + ); + }); }); }); }); diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx index ac992397fe2..1b1de928be0 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx @@ -69,6 +69,7 @@ import { WorkspaceAttribute, } from 'src/core/public'; import { Subscription } from 'rxjs'; +import { DEFAULT_WORKSPACE_ID } from '../../../../../core/public'; import { RedirectAppLinks } from '../../../../opensearch_dashboards_react/public'; import { IndexPatternsContract } from '../../../../data/public'; import { @@ -192,7 +193,7 @@ export class SavedObjectsTable extends Component ws.id); + return availableWorkspaces?.map((ws) => ws.id).concat(DEFAULT_WORKSPACE_ID); } else { return [currentWorkspaceId]; } @@ -201,10 +202,13 @@ export class SavedObjectsTable extends Component(); + workspaceNameIdMap.set(DEFAULT_WORKSPACE_ID, DEFAULT_WORKSPACE_ID); // Assumption: workspace name is unique across the system - return availableWorkspaces?.reduce((map, ws) => { + availableWorkspaces?.reduce((map, ws) => { return map.set(ws.name, ws.id); - }, new Map()); + }, workspaceNameIdMap); + return workspaceNameIdMap; } private formatWorkspaceIdParams( @@ -352,6 +356,13 @@ export class SavedObjectsTable extends Component 1) { findOptions.sortField = 'type'; } @@ -962,6 +973,14 @@ export class SavedObjectsTable extends Component {this.renderFlyout()} @@ -987,8 +1003,6 @@ export class SavedObjectsTable extends Component diff --git a/src/plugins/saved_objects_management/server/routes/find.ts b/src/plugins/saved_objects_management/server/routes/find.ts index 61211532e96..8d94c0d935c 100644 --- a/src/plugins/saved_objects_management/server/routes/find.ts +++ b/src/plugins/saved_objects_management/server/routes/find.ts @@ -67,6 +67,7 @@ export const registerFindRoute = ( workspaces: schema.maybe( schema.oneOf([schema.string(), schema.arrayOf(schema.string())]) ), + workspacesSearchOperator: schema.maybe(schema.string()), }), }, }, diff --git a/src/plugins/saved_objects_management/server/routes/scroll_count.ts b/src/plugins/saved_objects_management/server/routes/scroll_count.ts index fc523780537..ec8af548285 100644 --- a/src/plugins/saved_objects_management/server/routes/scroll_count.ts +++ b/src/plugins/saved_objects_management/server/routes/scroll_count.ts @@ -30,6 +30,7 @@ import { schema } from '@osd/config-schema'; import { IRouter, SavedObjectsFindOptions } from 'src/core/server'; +import { DEFAULT_WORKSPACE_ID } from '../../../../core/server'; import { findAll } from '../lib'; export const registerScrollForCountRoute = (router: IRouter) => { @@ -69,6 +70,10 @@ export const registerScrollForCountRoute = (router: IRouter) => { if (requestHasWorkspaces) { counts.workspaces = {}; findOptions.workspaces = req.body.workspaces; + if (findOptions.workspaces.indexOf(DEFAULT_WORKSPACE_ID) !== -1) { + // search both saved objects with workspace and without workspace + findOptions.workspacesSearchOperator = 'OR'; + } } if (req.body.searchString) { @@ -91,7 +96,7 @@ export const registerScrollForCountRoute = (router: IRouter) => { }); } if (requestHasWorkspaces) { - const resultWorkspaces = result.workspaces || []; + const resultWorkspaces = result.workspaces || [DEFAULT_WORKSPACE_ID]; resultWorkspaces.forEach((ws) => { counts.workspaces[ws] = counts.workspaces[ws] || 0; counts.workspaces[ws]++; diff --git a/src/plugins/workspace/public/plugin.test.ts b/src/plugins/workspace/public/plugin.test.ts index e6c21712418..21ac95e6733 100644 --- a/src/plugins/workspace/public/plugin.test.ts +++ b/src/plugins/workspace/public/plugin.test.ts @@ -133,7 +133,9 @@ describe('Workspace plugin', () => { it('#setup register workspace dropdown menu when setup', async () => { const setupMock = coreMock.createSetup(); const workspacePlugin = new WorkspacePlugin(); - await workspacePlugin.setup(setupMock); + await workspacePlugin.setup(setupMock, { + savedObjectsManagement: savedObjectsManagementPluginMock.createSetupContract(), + }); expect(setupMock.chrome.registerCollapsibleNavHeader).toBeCalledTimes(1); }); });