From 3795f0d5a920423fb49d4d2e27565d89f8790a73 Mon Sep 17 00:00:00 2001 From: kay delaney <45561153+kaydelaney@users.noreply.github.com> Date: Tue, 16 Apr 2024 13:29:27 +0100 Subject: [PATCH] Dashboards: Fix issue when importing external dashboard with datasource variables (#86273) --- .../DashExportModal/DashboardExporter.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/public/app/features/dashboard/components/DashExportModal/DashboardExporter.ts b/public/app/features/dashboard/components/DashExportModal/DashboardExporter.ts index ebfcfd555d5..8548434f913 100644 --- a/public/app/features/dashboard/components/DashExportModal/DashboardExporter.ts +++ b/public/app/features/dashboard/components/DashExportModal/DashboardExporter.ts @@ -5,6 +5,7 @@ import { getDataSourceSrv } from '@grafana/runtime'; import config from 'app/core/config'; import { PanelModel } from 'app/features/dashboard/state'; import { getLibraryPanel } from 'app/features/library-panels/state/api'; +import { variableRegex } from 'app/features/variables/utils'; import { isPanelModelLibraryPanel } from '../../../library-panels/guard'; import { LibraryElementKind } from '../../../library-panels/types'; @@ -110,14 +111,15 @@ export class DashboardExporter { let datasource = obj.datasource; let datasourceVariable: any = null; - const datasourceUid: string = datasource?.uid; + const datasourceUid: string | undefined = datasource?.uid; + const match = datasourceUid && variableRegex.exec(datasourceUid); + // ignore data source properties that contain a variable - if (datasourceUid) { - if (datasourceUid.indexOf('$') === 0) { - datasourceVariable = variableLookup[datasourceUid.substring(1)]; - if (datasourceVariable && datasourceVariable.current) { - datasource = datasourceVariable.current.value; - } + if (match) { + const varName = match[1] || match[2] || match[4]; + datasourceVariable = variableLookup[varName]; + if (datasourceVariable && datasourceVariable.current) { + datasource = datasourceVariable.current.value; } }