Export: Explicitly include default datasources in exported dashboard json (#47244)

* Export: Explicitly include default datasources in exported dashboard json
This commit is contained in:
kay delaney 2022-04-20 10:02:26 +01:00 committed by GitHub
parent cbd2d09d70
commit f9f4a4cbf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View File

@ -276,6 +276,10 @@ describe('given dashboard with repeated panels', () => {
},
],
},
{
id: 5,
targets: [{ scenarioId: 'random_walk', refId: 'A' }],
},
],
};
@ -312,6 +316,23 @@ describe('given dashboard with repeated panels', () => {
expect(panel.datasource.uid).toBe('${DS_GFDB}');
});
it('should explicitly specify default datasources', () => {
const panel = exported.panels[7];
expect(exported.__inputs.some((ds: Record<string, string>) => ds.name === 'DS_GFDB')).toBeTruthy();
expect(panel.datasource.uid).toBe('${DS_GFDB}');
expect(panel.targets[0].datasource).toBe('${DS_GFDB}');
});
it('should not include default datasource in __inputs unnecessarily', async () => {
const testJson: any = {
panels: [{ id: 1, datasource: { uid: 'other', type: 'other' }, type: 'graph' }],
};
const testDash = new DashboardModel(testJson);
const exporter = new DashboardExporter();
const exportedJson: any = await exporter.makeExportable(testDash);
expect(exportedJson.__inputs.some((ds: Record<string, string>) => ds.name === 'DS_GFDB')).toBeFalsy();
});
it('should replace datasource refs in collapsed row', () => {
const panel = exported.panels[6].panels[0];
expect(panel.datasource.uid).toBe('${DS_GFDB}');
@ -453,7 +474,7 @@ stubs['other2'] = {
meta: { id: 'other2', info: { version: '1.2.1' }, name: 'OtherDB_2' },
};
stubs['-- Mixed --'] = {
stubs['mixed'] = {
name: 'mixed',
meta: {
id: 'mixed',
@ -463,7 +484,7 @@ stubs['-- Mixed --'] = {
},
};
stubs['-- Grafana --'] = {
stubs['grafana'] = {
name: '-- Grafana --',
meta: {
id: 'grafana',

View File

@ -126,15 +126,18 @@ export class DashboardExporter {
};
const processPanel = (panel: PanelModel) => {
if (panel.datasource !== undefined && panel.datasource !== null) {
const isRegularPanel =
(panel.repeatPanelId === undefined || panel.repeatPanelId === null) &&
!('collapsed' in panel) &&
!('panels' in panel);
if (isRegularPanel) {
templateizeDatasourceUsage(panel);
}
if (panel.targets) {
for (const target of panel.targets) {
if (target.datasource !== undefined) {
templateizeDatasourceUsage(target);
}
templateizeDatasourceUsage(target);
}
}