From b92b333cfb2bcd1df3e400f9c53f6e00c080cf94 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Mon, 11 Nov 2024 15:55:54 +0800 Subject: [PATCH 1/2] Fix error toasts in sample data page Signed-off-by: Lin Wang --- .../components/sample_data_set_card.js | 2 + .../components/sample_data_set_cards.js | 18 ++- .../opensearch_dashboards_services.ts | 2 + .../public/application/sample_data_client.js | 17 ++- .../home/public/application/utils.test.ts | 106 ++++++++++++++++++ src/plugins/home/public/application/utils.ts | 35 ++++++ src/plugins/home/public/plugin.ts | 1 + 7 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 src/plugins/home/public/application/utils.test.ts create mode 100644 src/plugins/home/public/application/utils.ts diff --git a/src/plugins/home/public/application/components/sample_data_set_card.js b/src/plugins/home/public/application/components/sample_data_set_card.js index 5671478fa0de..8b0bb9117257 100644 --- a/src/plugins/home/public/application/components/sample_data_set_card.js +++ b/src/plugins/home/public/application/components/sample_data_set_card.js @@ -80,6 +80,7 @@ export class SampleDataSetCard extends React.Component { ({ @@ -162,7 +168,12 @@ export class SampleDataSetCards extends React.Component { })); try { - await uninstallSampleDataSet(id, targetSampleDataSet.defaultIndex, dataSourceId); + await uninstallSampleDataSet( + id, + targetSampleDataSet.defaultIndex, + dataSourceId, + isAbleToUpdateUiSettings(getServices()) + ); } catch (fetchError) { if (this._isMounted) { this.setState((prevState) => ({ @@ -216,6 +227,8 @@ export class SampleDataSetCards extends React.Component { }; render() { + const ableToUpdateUiSettings = isAbleToUpdateUiSettings(getServices()); + return ( ); diff --git a/src/plugins/home/public/application/opensearch_dashboards_services.ts b/src/plugins/home/public/application/opensearch_dashboards_services.ts index 1107e46ecf2e..7de131121815 100644 --- a/src/plugins/home/public/application/opensearch_dashboards_services.ts +++ b/src/plugins/home/public/application/opensearch_dashboards_services.ts @@ -37,6 +37,7 @@ import { SavedObjectsClientContract, IUiSettingsClient, ApplicationStart, + WorkspacesStart, } from 'opensearch-dashboards/public'; import { UiStatsMetricType } from '@osd/analytics'; import { TelemetryPluginStart } from '../../../telemetry/public'; @@ -77,6 +78,7 @@ export interface HomeOpenSearchDashboardsServices { }; dataSource?: DataSourcePluginStart; sectionTypes: SectionTypeService; + workspaces: WorkspacesStart; } let services: HomeOpenSearchDashboardsServices | null = null; diff --git a/src/plugins/home/public/application/sample_data_client.js b/src/plugins/home/public/application/sample_data_client.js index 045736c428f6..8681714c1c22 100644 --- a/src/plugins/home/public/application/sample_data_client.js +++ b/src/plugins/home/public/application/sample_data_client.js @@ -41,24 +41,35 @@ export async function listSampleDataSets(dataSourceId) { return await getServices().http.get(sampleDataUrl, { query }); } -export async function installSampleDataSet(id, sampleDataDefaultIndex, dataSourceId) { +export async function installSampleDataSet( + id, + sampleDataDefaultIndex, + dataSourceId, + ableToUpdateUiSettings +) { const query = buildQuery(dataSourceId); await getServices().http.post(`${sampleDataUrl}/${id}`, { query }); - if (getServices().uiSettings.isDefault('defaultIndex')) { + if (ableToUpdateUiSettings && getServices().uiSettings.isDefault('defaultIndex')) { getServices().uiSettings.set('defaultIndex', sampleDataDefaultIndex); } clearIndexPatternsCache(); } -export async function uninstallSampleDataSet(id, sampleDataDefaultIndex, dataSourceId) { +export async function uninstallSampleDataSet( + id, + sampleDataDefaultIndex, + dataSourceId, + ableToUpdateUiSettings +) { const query = buildQuery(dataSourceId); await getServices().http.delete(`${sampleDataUrl}/${id}`, { query }); const uiSettings = getServices().uiSettings; if ( + ableToUpdateUiSettings && !uiSettings.isDefault('defaultIndex') && uiSettings.get('defaultIndex') === sampleDataDefaultIndex ) { diff --git a/src/plugins/home/public/application/utils.test.ts b/src/plugins/home/public/application/utils.test.ts new file mode 100644 index 000000000000..e8fac191b935 --- /dev/null +++ b/src/plugins/home/public/application/utils.test.ts @@ -0,0 +1,106 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { isAbleToUpdateUiSettings } from './utils'; + +describe('isAbleToUpdateUiSettings', () => { + it('should return true when workspace is not enabled and permission is not enabled', () => { + const application = { + capabilities: { + workspaces: { + enabled: false, + permissionEnabled: false, + }, + }, + }; + const workspaces = null; + + const result = isAbleToUpdateUiSettings({ workspaces, application }); + + expect(result).toBe(true); + }); + + it('should return true when workspace is enabled but permission is not enabled', () => { + const application = { + capabilities: { + workspaces: { + enabled: true, + permissionEnabled: false, + }, + }, + }; + const workspaces = null; + + const result = isAbleToUpdateUiSettings({ workspaces, application }); + + expect(result).toBe(true); + }); + + it('should return true when current workspace owner', () => { + const application = { + capabilities: { + workspaces: { + enabled: true, + permissionEnabled: true, + }, + }, + }; + const workspaces = { + currentWorkspace$: { + getValue: () => ({ owner: true }), + }, + }; + + const result = isAbleToUpdateUiSettings({ workspaces, application }); + + expect(result).toBe(true); + }); + + it('should return true when user is dashboards admin', () => { + const application = { + capabilities: { + workspaces: { + enabled: true, + permissionEnabled: true, + }, + dashboards: { + isDashboardAdmin: true, + }, + }, + }; + const workspaces = { + currentWorkspace$: { + getValue: () => ({ owner: false }), + }, + }; + + const result = isAbleToUpdateUiSettings({ workspaces, application }); + + expect(result).toBe(true); + }); + + it('should return false when user is not current workspace owner and not dashboards admin', () => { + const application = { + capabilities: { + workspaces: { + enabled: true, + permissionEnabled: true, + }, + dashboards: { + isDashboardAdmin: false, + }, + }, + }; + const workspaces = { + currentWorkspace$: { + getValue: () => ({ owner: false }), + }, + }; + + const result = isAbleToUpdateUiSettings({ workspaces, application }); + + expect(result).toBe(false); + }); +}); diff --git a/src/plugins/home/public/application/utils.ts b/src/plugins/home/public/application/utils.ts new file mode 100644 index 000000000000..e7a1cfe0a022 --- /dev/null +++ b/src/plugins/home/public/application/utils.ts @@ -0,0 +1,35 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { HomeOpenSearchDashboardsServices } from './opensearch_dashboards_services'; + +export const isAbleToUpdateUiSettings = ({ + workspaces, + application, +}: Pick) => { + // Able to update ui settings when workspace not enabled and permission not enabled + if ( + !application.capabilities || + !application.capabilities.workspaces || + !application.capabilities.workspaces.enabled || + !application.capabilities.workspaces.permissionEnabled + ) { + return true; + } + + // Able to update ui settings when is current workspace owner + if (workspaces) { + const currentWorkspace = workspaces.currentWorkspace$.getValue(); + if (currentWorkspace && currentWorkspace.owner) { + return true; + } + } + + // Able to update ui settings when is dashboards admin + return ( + application.capabilities.dashboards && + application.capabilities.dashboards.isDashboardAdmin !== false + ); +}; diff --git a/src/plugins/home/public/plugin.ts b/src/plugins/home/public/plugin.ts index 435c7d4d3b9f..f294fd1f3752 100644 --- a/src/plugins/home/public/plugin.ts +++ b/src/plugins/home/public/plugin.ts @@ -156,6 +156,7 @@ export class HomePublicPlugin injectedMetadata: coreStart.injectedMetadata, dataSource, sectionTypes: this.sectionTypeService, + workspaces: coreStart.workspaces, ...homeOpenSearchDashboardsServices, }); }; From f1992909666253e9603b6eff1963f74a3f53c768 Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Mon, 11 Nov 2024 08:01:36 +0000 Subject: [PATCH 2/2] Changeset file for PR #8842 created/updated --- changelogs/fragments/8842.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/8842.yml diff --git a/changelogs/fragments/8842.yml b/changelogs/fragments/8842.yml new file mode 100644 index 000000000000..b9973f347f9e --- /dev/null +++ b/changelogs/fragments/8842.yml @@ -0,0 +1,2 @@ +fix: +- [Workspace]Fix error toasts in sample data page ([#8842](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8842)) \ No newline at end of file