From 642d2279236d55206b595c835cde41e466f79ec8 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Thu, 11 Jul 2024 21:05:03 +0800 Subject: [PATCH] Add version decoupling meta for MDS (#338) (#340) * Add version decoupling meta for MDS Signed-off-by: Lin Wang * Filter out not compatible data sources Signed-off-by: Lin Wang * Fix in operator not work Signed-off-by: Lin Wang --------- Signed-off-by: Lin Wang (cherry picked from commit ab606786db893fe487bc3fe6a37e0ba70fa897d6) Co-authored-by: Lin Wang --- opensearch_dashboards.json | 4 +- .../components/data_source_top_nav_menu.tsx | 2 + public/utils/__tests__/data_source.test.ts | 68 ++++++++++++++++++- public/utils/data_source.ts | 27 ++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/opensearch_dashboards.json b/opensearch_dashboards.json index 4d2af008..c1e6f107 100644 --- a/opensearch_dashboards.json +++ b/opensearch_dashboards.json @@ -12,5 +12,7 @@ "dashboard", "opensearchUiShared" ], - "optionalPlugins": ["dataSource", "dataSourceManagement"] + "optionalPlugins": ["dataSource", "dataSourceManagement"], + "supportedOSDataSourceVersions": ">=2.9.0", + "requiredOSDataSourcePlugins": ["opensearch-ml"] } diff --git a/public/components/data_source_top_nav_menu.tsx b/public/components/data_source_top_nav_menu.tsx index 7194696a..d82b96a1 100644 --- a/public/components/data_source_top_nav_menu.tsx +++ b/public/components/data_source_top_nav_menu.tsx @@ -11,6 +11,7 @@ import type { DataSourceSelectableConfig, } from '../../../../src/plugins/data_source_management/public'; import { DataSourceContext } from '../contexts/data_source_context'; +import { isDataSourceCompatible } from '../utils/data_source'; export interface DataSourceTopNavMenuProps { notifications: CoreStart['notifications']; @@ -53,6 +54,7 @@ export const DataSourceTopNavMenu = ({ savedObjects: savedObjects.client, onSelectedDataSources: handleDataSourcesSelected, activeOption, + dataSourceFilter: isDataSourceCompatible, }} setMenuMountPoint={setActionMenu} /> diff --git a/public/utils/__tests__/data_source.test.ts b/public/utils/__tests__/data_source.test.ts index 51b6c3f2..74b92b6f 100644 --- a/public/utils/__tests__/data_source.test.ts +++ b/public/utils/__tests__/data_source.test.ts @@ -3,7 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { DATA_SOURCE_FETCHING_ID, DATA_SOURCE_INVALID_ID, getDataSourceId } from '../data_source'; +import { + DATA_SOURCE_FETCHING_ID, + DATA_SOURCE_INVALID_ID, + getDataSourceId, + isDataSourceCompatible, +} from '../data_source'; describe('getDataSourceId', () => { it('should return undefined when data source not enabled', () => { @@ -27,3 +32,64 @@ describe('getDataSourceId', () => { expect(getDataSourceId(true, { id: 'foo' })).toBe('foo'); }); }); + +describe('isDataSourceCompatible', () => { + it('should return true for compatible data sources', () => { + expect( + isDataSourceCompatible({ + attributes: { + installedPlugins: ['opensearch-ml'], + dataSourceVersion: '2.9.0', + }, + }) + ).toBe(true); + expect( + isDataSourceCompatible({ + attributes: { + installedPlugins: ['opensearch-ml'], + dataSourceVersion: '2.11.0', + }, + }) + ).toBe(true); + expect( + isDataSourceCompatible({ + attributes: { + installedPlugins: ['opensearch-ml'], + dataSourceVersion: '2.13.0', + }, + }) + ).toBe(true); + }); + + it('should return false for un-compatible data sources', () => { + expect( + isDataSourceCompatible({ + attributes: { + installedPlugins: [], + dataSourceVersion: '2.13.0', + }, + }) + ).toBe(false); + expect( + isDataSourceCompatible({ + attributes: { + installedPlugins: ['opensearch-jetty'], + dataSourceVersion: '2.13.0', + }, + }) + ).toBe(false); + expect( + isDataSourceCompatible({ + attributes: {}, + }) + ).toBe(false); + expect( + isDataSourceCompatible({ + attributes: { + installedPlugins: ['opensearch-ml'], + dataSourceVersion: '2.7.0', + }, + }) + ).toBe(false); + }); +}); diff --git a/public/utils/data_source.ts b/public/utils/data_source.ts index ba236bd7..754bedca 100644 --- a/public/utils/data_source.ts +++ b/public/utils/data_source.ts @@ -2,8 +2,12 @@ * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ +import semver from 'semver'; import { DataSourceOption } from '../contexts'; +import pluginManifest from '../../opensearch_dashboards.json'; +import type { SavedObject } from '../../../../src/core/public'; +import type { DataSourceAttributes } from '../../../../src/plugins/data_source/common/data_sources'; export const DATA_SOURCE_FETCHING_ID = Symbol('DATA_SOURCE_FETCHING_ID'); export const DATA_SOURCE_INVALID_ID = Symbol('DATA_SOURCE_INVALID_ID'); @@ -29,3 +33,26 @@ export const getDataSourceId = ( }; export type DataSourceId = ReturnType; + +export const isDataSourceCompatible = (dataSource: SavedObject) => { + if ( + 'requiredOSDataSourcePlugins' in pluginManifest && + !pluginManifest.requiredOSDataSourcePlugins.every((plugin) => + dataSource.attributes.installedPlugins?.includes(plugin) + ) + ) { + return false; + } + + // filter out data sources which is NOT in the support range of plugin + if ( + 'supportedOSDataSourceVersions' in pluginManifest && + !semver.satisfies( + dataSource.attributes.dataSourceVersion, + pluginManifest.supportedOSDataSourceVersions + ) + ) { + return false; + } + return true; +};