Skip to content

Commit

Permalink
feat: remove workspaces when listing data sources
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Apr 3, 2024
1 parent 7fd5160 commit 9d72524
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { updateWorkspaceState } from '../../../../core/server/utils';
import { SavedObject } from '../../../../core/public';
import { httpServerMock, savedObjectsClientMock, coreMock } from '../../../../core/server/mocks';
import { WorkspaceIdConsumerWrapper } from './workspace_id_consumer_wrapper';
import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../../../plugins/data_source/common';

describe('WorkspaceIdConsumerWrapper', () => {
const requestHandlerContext = coreMock.createRequestHandlerContext();
const wrapperInstance = new WorkspaceIdConsumerWrapper();
const mockedClient = savedObjectsClientMock.create();
const workspaceEnabledMockRequest = httpServerMock.createOpenSearchDashboardsRequest();
updateWorkspaceState(workspaceEnabledMockRequest, {
id: 'foo',
requestWorkspaceId: 'foo',
});
const wrapperClient = wrapperInstance.wrapperFactory({
client: mockedClient,
Expand Down Expand Up @@ -114,5 +115,14 @@ describe('WorkspaceIdConsumerWrapper', () => {
workspaces: ['foo'],
});
});

it(`workspaces parameters should be removed when finding data sources`, async () => {
await wrapperClient.find({
type: DATA_SOURCE_SAVED_OBJECT_TYPE,
});
expect(mockedClient.find).toBeCalledWith({
type: DATA_SOURCE_SAVED_OBJECT_TYPE,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
OpenSearchDashboardsRequest,
SavedObjectsFindOptions,
} from '../../../../core/server';
import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../../../plugins/data_source/common';

type WorkspaceOptions = Pick<SavedObjectsBaseOptions, 'workspaces'> | undefined;

Expand All @@ -37,6 +38,17 @@ export class WorkspaceIdConsumerWrapper {
...(finalWorkspaces.length ? { workspaces: finalWorkspaces } : {}),
};
}
private isDataSourceType(type: SavedObjectsFindOptions['type']): boolean {
if (Array.isArray(type)) {
return type.every((item) => item === DATA_SOURCE_SAVED_OBJECT_TYPE);
}

return type === DATA_SOURCE_SAVED_OBJECT_TYPE;
}
private formatFindParams(options: SavedObjectsFindOptions): SavedObjectsFindOptions {
const isListingDataSource = this.isDataSourceType(options.type);
return isListingDataSource ? { ...options, workspaces: null } : options;
}
public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
return {
...wrapperOptions.client,
Expand Down Expand Up @@ -64,7 +76,14 @@ export class WorkspaceIdConsumerWrapper {
),
delete: wrapperOptions.client.delete,
find: (options: SavedObjectsFindOptions) =>
wrapperOptions.client.find(this.formatWorkspaceIdParams(wrapperOptions.request, options)),
wrapperOptions.client.find(
this.formatWorkspaceIdParams(
wrapperOptions.request,
// The `formatFindParams` is a workaroud for 2.14 to always list global data sources,
// should remove this workaround in 2.15 once readonly share is available
this.formatFindParams(options)
)
),
bulkGet: wrapperOptions.client.bulkGet,
get: wrapperOptions.client.get,
update: wrapperOptions.client.update,
Expand Down

0 comments on commit 9d72524

Please sign in to comment.