Skip to content

Commit d0d5882

Browse files
committed
feat: optimization according to PR comments
Signed-off-by: SuZhou-Joe <[email protected]>
1 parent bf53f12 commit d0d5882

File tree

9 files changed

+37
-41
lines changed

9 files changed

+37
-41
lines changed

src/plugins/workspace/opensearch_dashboards.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"id": "workspace",
33
"version": "opensearchDashboards",
44
"server": true,
5-
"ui": true,
6-
"requiredPlugins": [],
5+
"ui": false,
6+
"requiredPlugins": [
7+
"savedObjects"
8+
],
79
"optionalPlugins": [],
810
"requiredBundles": []
911
}

src/plugins/workspace/public/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright OpenSearch Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
56
import { Plugin } from '../../../core/public';
67

78
export class WorkspacePlugin implements Plugin<{}, {}, {}> {

src/plugins/workspace/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright OpenSearch Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
56
import { PluginConfigDescriptor, PluginInitializerContext } from '../../../core/server';
67
import { WorkspacePlugin } from './plugin';
78
import { configSchema } from '../config';

src/plugins/workspace/server/plugin.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
* Copyright OpenSearch Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
56
import {
67
PluginInitializerContext,
78
CoreSetup,
89
Plugin,
910
Logger,
1011
CoreStart,
1112
} from '../../../core/server';
12-
import { IWorkspaceDBImpl } from './types';
13-
import { WorkspaceClientWithSavedObject } from './workspace_client';
13+
import { IWorkspaceClientImpl } from './types';
14+
import { WorkspaceClient } from './workspace_client';
1415
import { registerRoutes } from './routes';
1516

1617
export class WorkspacePlugin implements Plugin<{}, {}> {
1718
private readonly logger: Logger;
18-
private client?: IWorkspaceDBImpl;
19+
private client?: IWorkspaceClientImpl;
1920

2021
constructor(initializerContext: PluginInitializerContext) {
2122
this.logger = initializerContext.logger.get('plugins', 'workspace');
@@ -24,14 +25,14 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
2425
public async setup(core: CoreSetup) {
2526
this.logger.debug('Setting up Workspaces service');
2627

27-
this.client = new WorkspaceClientWithSavedObject(core);
28+
this.client = new WorkspaceClient(core);
2829

2930
await this.client.setup(core);
3031

3132
registerRoutes({
3233
http: core.http,
3334
logger: this.logger,
34-
client: this.client as IWorkspaceDBImpl,
35+
client: this.client as IWorkspaceClientImpl,
3536
});
3637

3738
return {
@@ -44,7 +45,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
4445
this.client?.setSavedObjects(core.savedObjects);
4546

4647
return {
47-
client: this.client as IWorkspaceDBImpl,
48+
client: this.client as IWorkspaceClientImpl,
4849
};
4950
}
5051

src/plugins/workspace/server/routes/index.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* Copyright OpenSearch Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
import { schema } from '@osd/config-schema';
65

6+
import { schema } from '@osd/config-schema';
77
import { CoreSetup, Logger } from '../../../../core/server';
8-
import { IWorkspaceDBImpl } from '../types';
8+
import { IWorkspaceClientImpl } from '../types';
99

1010
const WORKSPACES_API_BASE_URL = '/api/workspaces';
1111

@@ -23,7 +23,7 @@ export function registerRoutes({
2323
logger,
2424
http,
2525
}: {
26-
client: IWorkspaceDBImpl;
26+
client: IWorkspaceClientImpl;
2727
logger: Logger;
2828
http: CoreSetup['http'];
2929
}) {
@@ -78,17 +78,9 @@ export function registerRoutes({
7878
},
7979
id
8080
);
81-
if (!result.success) {
82-
return res.ok({ body: result });
83-
}
8481

8582
return res.ok({
86-
body: {
87-
...result,
88-
result: {
89-
...result.result,
90-
},
91-
},
83+
body: result,
9284
});
9385
})
9486
);
@@ -110,9 +102,7 @@ export function registerRoutes({
110102
request: req,
111103
logger,
112104
},
113-
{
114-
...attributes,
115-
}
105+
attributes
116106
);
117107
return res.ok({ body: result });
118108
})
@@ -140,9 +130,7 @@ export function registerRoutes({
140130
logger,
141131
},
142132
id,
143-
{
144-
...attributes,
145-
}
133+
attributes
146134
);
147135
return res.ok({ body: result });
148136
})

src/plugins/workspace/server/saved_objects/workspace.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export const workspace: SavedObjectsType = {
2121
description: {
2222
type: 'text',
2323
},
24-
/**
25-
* In opensearch, string[] is also mapped to text
26-
*/
2724
features: {
2825
type: 'keyword',
2926
},

src/plugins/workspace/server/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright OpenSearch Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
56
import {
67
Logger,
78
OpenSearchDashboardsRequest,
@@ -27,7 +28,7 @@ export interface IRequestDetail {
2728
logger: Logger;
2829
}
2930

30-
export interface IWorkspaceDBImpl {
31+
export interface IWorkspaceClientImpl {
3132
setup(dep: CoreSetup): Promise<IResponse<boolean>>;
3233
setSavedObjects(savedObjects: SavedObjectsServiceStart): void;
3334
create(

src/plugins/workspace/server/workspace_client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright OpenSearch Contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
56
import { i18n } from '@osd/i18n';
67
import type {
78
SavedObject,
@@ -11,7 +12,7 @@ import type {
1112
SavedObjectsServiceStart,
1213
} from '../../../core/server';
1314
import { WORKSPACE_TYPE } from '../../../core/server';
14-
import { IWorkspaceDBImpl, WorkspaceFindOptions, IResponse, IRequestDetail } from './types';
15+
import { IWorkspaceClientImpl, WorkspaceFindOptions, IResponse, IRequestDetail } from './types';
1516
import { workspace } from './saved_objects';
1617
import { generateRandomId } from './utils';
1718
import { WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID } from '../common/constants';
@@ -22,7 +23,7 @@ const DUPLICATE_WORKSPACE_NAME_ERROR = i18n.translate('workspace.duplicate.name.
2223
defaultMessage: 'workspace name has already been used, try with a different name',
2324
});
2425

25-
export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl {
26+
export class WorkspaceClient implements IWorkspaceClientImpl {
2627
private setupDep: CoreSetup;
2728
private savedObjects?: SavedObjectsServiceStart;
2829

@@ -64,7 +65,7 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl {
6465
public async create(
6566
requestDetail: IRequestDetail,
6667
payload: Omit<WorkspaceAttribute, 'id'>
67-
): ReturnType<IWorkspaceDBImpl['create']> {
68+
): ReturnType<IWorkspaceClientImpl['create']> {
6869
try {
6970
const attributes = payload;
7071
const id = generateRandomId(WORKSPACE_ID_SIZE);
@@ -102,7 +103,7 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl {
102103
public async list(
103104
requestDetail: IRequestDetail,
104105
options: WorkspaceFindOptions
105-
): ReturnType<IWorkspaceDBImpl['list']> {
106+
): ReturnType<IWorkspaceClientImpl['list']> {
106107
try {
107108
const {
108109
saved_objects: savedObjects,

test/api_integration/apis/workspace/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
import expect from '@osd/expect';
77
import { WorkspaceAttribute } from 'opensearch-dashboards/server';
8-
import { omit } from 'lodash';
98
import { FtrProviderContext } from '../../ftr_provider_context';
109

10+
const omitId = <T extends { id?: string }>(object: T): Omit<T, 'id'> => {
11+
const { id, ...others } = object;
12+
return others;
13+
};
14+
1115
const testWorkspace: WorkspaceAttribute = {
1216
id: 'fake_id',
1317
name: 'test_workspace',
@@ -47,7 +51,7 @@ export default function ({ getService }: FtrProviderContext) {
4751
const result: any = await supertest
4852
.post(`/api/workspaces`)
4953
.send({
50-
attributes: omit(testWorkspace, 'id'),
54+
attributes: omitId(testWorkspace),
5155
})
5256
.set('osd-xsrf', 'opensearch-dashboards')
5357
.expect(200);
@@ -59,7 +63,7 @@ export default function ({ getService }: FtrProviderContext) {
5963
const result = await supertest
6064
.post(`/api/workspaces`)
6165
.send({
62-
attributes: omit(testWorkspace, 'id'),
66+
attributes: omitId(testWorkspace),
6367
})
6468
.set('osd-xsrf', 'opensearch-dashboards')
6569
.expect(200);
@@ -71,7 +75,7 @@ export default function ({ getService }: FtrProviderContext) {
7175
const result: any = await supertest
7276
.post(`/api/workspaces`)
7377
.send({
74-
attributes: omit(testWorkspace, 'id'),
78+
attributes: omitId(testWorkspace),
7579
})
7680
.set('osd-xsrf', 'opensearch-dashboards')
7781
.expect(200);
@@ -80,7 +84,7 @@ export default function ({ getService }: FtrProviderContext) {
8084
.put(`/api/workspaces/${result.body.result.id}`)
8185
.send({
8286
attributes: {
83-
...omit(testWorkspace, 'id'),
87+
...omitId(testWorkspace),
8488
name: 'updated',
8589
},
8690
})
@@ -96,7 +100,7 @@ export default function ({ getService }: FtrProviderContext) {
96100
const result: any = await supertest
97101
.post(`/api/workspaces`)
98102
.send({
99-
attributes: omit(testWorkspace, 'id'),
103+
attributes: omitId(testWorkspace),
100104
})
101105
.set('osd-xsrf', 'opensearch-dashboards')
102106
.expect(200);
@@ -114,7 +118,7 @@ export default function ({ getService }: FtrProviderContext) {
114118
await supertest
115119
.post(`/api/workspaces`)
116120
.send({
117-
attributes: omit(testWorkspace, 'id'),
121+
attributes: omitId(testWorkspace),
118122
})
119123
.set('osd-xsrf', 'opensearch-dashboards')
120124
.expect(200);

0 commit comments

Comments
 (0)