mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Export: Remove DS input when dashboard is imported with a lib panel that already exists (#69412)
This commit is contained in:
@@ -164,8 +164,8 @@ it('replaces datasource ref in library panel', async () => {
|
||||
if ('error' in exported) {
|
||||
throw new Error('error should not be returned when making exportable json');
|
||||
}
|
||||
expect(exported.__elements['c46a6b49-de40-43b3-982c-1b5e1ec084a4'].model.datasource.uid).toBe('${DS_GFDB}');
|
||||
expect(exported.__inputs[0].name).toBe('DS_GFDB');
|
||||
expect(exported.__elements!['c46a6b49-de40-43b3-982c-1b5e1ec084a4'].model.datasource.uid).toBe('${DS_GFDB}');
|
||||
expect(exported.__inputs![0].name).toBe('DS_GFDB');
|
||||
});
|
||||
|
||||
it('If a panel queries has no datasource prop ignore it', async () => {
|
||||
|
||||
@@ -13,12 +13,21 @@ import { VariableOption, VariableRefresh } from '../../../variables/types';
|
||||
import { DashboardModel } from '../../state/DashboardModel';
|
||||
import { GridPos } from '../../state/PanelModel';
|
||||
|
||||
interface Input {
|
||||
export interface InputUsage {
|
||||
libraryPanels?: LibraryPanel[];
|
||||
}
|
||||
|
||||
export interface LibraryPanel {
|
||||
name: string;
|
||||
uid: string;
|
||||
}
|
||||
export interface Input {
|
||||
name: string;
|
||||
type: string;
|
||||
label: string;
|
||||
value: any;
|
||||
description: string;
|
||||
usage?: InputUsage;
|
||||
}
|
||||
|
||||
interface Requires {
|
||||
@@ -30,20 +39,17 @@ interface Requires {
|
||||
};
|
||||
}
|
||||
|
||||
interface ExternalDashboard {
|
||||
__inputs: Input[];
|
||||
__elements: Record<string, LibraryElementExport>;
|
||||
__requires: Array<Requires[string]>;
|
||||
export interface ExternalDashboard {
|
||||
__inputs?: Input[];
|
||||
__elements?: Record<string, LibraryElementExport>;
|
||||
__requires?: Array<Requires[string]>;
|
||||
panels: Array<PanelModel | PanelWithExportableLibraryPanel>;
|
||||
}
|
||||
|
||||
interface PanelWithExportableLibraryPanel {
|
||||
gridPos: GridPos;
|
||||
id: number;
|
||||
libraryPanel: {
|
||||
name: string;
|
||||
uid: string;
|
||||
};
|
||||
libraryPanel: LibraryPanel;
|
||||
}
|
||||
|
||||
function isExportableLibraryPanel(p: any): p is PanelWithExportableLibraryPanel {
|
||||
@@ -58,6 +64,7 @@ interface DataSources {
|
||||
type: string;
|
||||
pluginId: string;
|
||||
pluginName: string;
|
||||
usage?: InputUsage;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -132,7 +139,10 @@ export class DashboardExporter {
|
||||
return;
|
||||
}
|
||||
|
||||
const refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase();
|
||||
const libraryPanel = obj.libraryPanel;
|
||||
const libraryPanelSuffix = !!libraryPanel ? '-for-library-panel' : '';
|
||||
let refName = 'DS_' + ds.name.replace(' ', '_').toUpperCase() + libraryPanelSuffix.toUpperCase();
|
||||
|
||||
datasources[refName] = {
|
||||
name: refName,
|
||||
label: ds.name,
|
||||
@@ -140,8 +150,18 @@ export class DashboardExporter {
|
||||
type: 'datasource',
|
||||
pluginId: ds.meta?.id,
|
||||
pluginName: ds.meta?.name,
|
||||
usage: datasources[refName]?.usage,
|
||||
};
|
||||
|
||||
if (!!libraryPanel) {
|
||||
const libPanels = datasources[refName]?.usage?.libraryPanels || [];
|
||||
libPanels.push({ name: libraryPanel.name, uid: libraryPanel.uid });
|
||||
|
||||
datasources[refName].usage = {
|
||||
libraryPanels: libPanels,
|
||||
};
|
||||
}
|
||||
|
||||
obj.datasource = { type: ds.meta.id, uid: '${' + refName + '}' };
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user