diff --git a/CHANGELOG.md b/CHANGELOG.md index 82d1175a15c0..f28a7c2522ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [BUG][Multiple Datasource] Fix missing customApiRegistryPromise param for test connection ([#5944](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5944)) - [BUG][Multiple Datasource] Add a migration function for datasource to add migrationVersion field ([#6025](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6025)) - [BUG][MD]Expose picker using function in data source management plugin setup([#6030](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6030)) +- [BUG][Multiple Datasource] Fix data source filter bug and add tests ([#6152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6152)) ### 🚞 Infrastructure diff --git a/src/plugins/data_source_management/public/components/data_source_selector/__snapshots__/data_source_selector.test.tsx.snap b/src/plugins/data_source_management/public/components/data_source_selector/__snapshots__/data_source_selector.test.tsx.snap index 07359ef9ac05..2b8e3eba860d 100644 --- a/src/plugins/data_source_management/public/components/data_source_selector/__snapshots__/data_source_selector.test.tsx.snap +++ b/src/plugins/data_source_management/public/components/data_source_selector/__snapshots__/data_source_selector.test.tsx.snap @@ -202,6 +202,29 @@ exports[`DataSourceSelector: check dataSource options should hide prepend if rem /> `; +exports[`DataSourceSelector: check dataSource options should return empty options if filter out all options and hide local cluster 1`] = ` + +`; + exports[`DataSourceSelector: check dataSource options should show custom placeholder text if configured 1`] = ` { disabled={false} hideLocalCluster={false} fullWidth={false} - filterFn={(ds) => ds.attributes.auth.type !== AuthType.NoAuth} + dataSourceFilter={(ds) => ds.attributes.auth.type !== AuthType.NoAuth} /> ); @@ -152,4 +152,22 @@ describe('DataSourceSelector: check dataSource options', () => { expect(component).toMatchSnapshot(); expect(toasts.addWarning).toBeCalledTimes(0); }); + + it('should return empty options if filter out all options and hide local cluster', async () => { + component = shallow( + ds.attributes.auth.type === 'random'} + /> + ); + component.instance().componentDidMount!(); + await nextTick(); + expect(component).toMatchSnapshot(); + expect(toasts.addWarning).toBeCalledTimes(0); + }); }); diff --git a/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx b/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx index d2f92e89388a..e7503cba645a 100644 --- a/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx +++ b/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx @@ -6,8 +6,9 @@ import React from 'react'; import { i18n } from '@osd/i18n'; import { EuiComboBox } from '@elastic/eui'; -import { SavedObjectsClientContract, ToastsStart } from 'opensearch-dashboards/public'; +import { SavedObjectsClientContract, ToastsStart, SavedObject } from 'opensearch-dashboards/public'; import { getDataSourcesWithFields } from '../utils'; +import { DataSourceAttributes } from '../../types'; export const LocalCluster: DataSourceOption = { label: i18n.translate('dataSource.localCluster', { @@ -26,7 +27,7 @@ export interface DataSourceSelectorProps { defaultOption?: DataSourceOption[]; placeholderText?: string; removePrepend?: boolean; - filterFn?: (dataSource: any) => boolean; + dataSourceFilter?: (dataSource: SavedObject) => boolean; compressed?: boolean; } @@ -73,14 +74,13 @@ export class DataSourceSelector extends React.Component< getDataSourcesWithFields(this.props.savedObjectsClient, ['id', 'title', 'auth.type']) .then((fetchedDataSources) => { if (fetchedDataSources?.length) { - let filteredDataSources = []; - if (this.props.filterFn) { - filteredDataSources = fetchedDataSources.filter((ds) => this.props.filterFn!(ds)); + let filteredDataSources = fetchedDataSources; + if (this.props.dataSourceFilter) { + filteredDataSources = fetchedDataSources.filter((ds) => + this.props.dataSourceFilter!(ds) + ); } - if (filteredDataSources.length === 0) { - filteredDataSources = fetchedDataSources; - } const dataSourceOptions = filteredDataSources.map((dataSource) => ({ id: dataSource.id, label: dataSource.attributes?.title || '', diff --git a/src/plugins/data_source_management/public/components/utils.ts b/src/plugins/data_source_management/public/components/utils.ts index 6f55b71b2503..5773363efd61 100644 --- a/src/plugins/data_source_management/public/components/utils.ts +++ b/src/plugins/data_source_management/public/components/utils.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { HttpStart, SavedObjectsClientContract } from 'src/core/public'; +import { HttpStart, SavedObjectsClientContract, SavedObject } from 'src/core/public'; import { DataSourceAttributes, DataSourceTableItem, @@ -39,8 +39,8 @@ export async function getDataSources(savedObjectsClient: SavedObjectsClientContr export async function getDataSourcesWithFields( savedObjectsClient: SavedObjectsClientContract, fields: string[] -) { - const response = await savedObjectsClient.find({ +): Promise>> { + const response = await savedObjectsClient.find({ type: 'data-source', fields, perPage: 10000,