DatasourceSrv: Fix instance retrieval when datasource variable value set to "default" (#31347) (#31348)

* Failing tests

* Fixed

(cherry picked from commit 0d6e5298b7)

Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2021-02-19 12:59:46 +01:00
committed by GitHub
co-authored by Dominik Prokop
parent 0423d54afd
commit e106dfdaad
2 changed files with 27 additions and 3 deletions
@@ -54,7 +54,15 @@ export class DatasourceSrv implements DataSourceService {
// For this we just pick the current or first data source in the variable
if (nameOrUid[0] === '$') {
const interpolatedName = this.templateSrv.replace(nameOrUid, {}, variableInterpolation);
const dsSettings = this.settingsMapByUid[interpolatedName] ?? this.settingsMapByName[interpolatedName];
let dsSettings;
if (interpolatedName === 'default') {
dsSettings = this.settingsMapByName[this.defaultName];
} else {
dsSettings = this.settingsMapByUid[interpolatedName] ?? this.settingsMapByName[interpolatedName];
}
if (!dsSettings) {
return undefined;
}
@@ -12,9 +12,18 @@ const templateSrv: any = {
value: 'BBB',
},
},
{
type: 'datasource',
name: 'datasourceDefault',
current: {
value: 'default',
},
},
],
replace: (v: string) => {
return v.replace('${datasource}', 'BBB');
let result = v.replace('${datasource}', 'BBB');
result = result.replace('${datasourceDefault}', 'default');
return result;
},
};
@@ -118,6 +127,12 @@ describe('datasource_srv', () => {
expect(ds?.name).toBe('${datasource}');
expect(ds?.uid).toBe('uid-code-BBB');
});
it('should work with variable', () => {
const ds = dataSourceSrv.getInstanceSettings('${datasourceDefault}');
expect(ds?.name).toBe('${datasourceDefault}');
expect(ds?.uid).toBe('uid-code-BBB');
});
});
describe('when getting external metric sources', () => {
@@ -136,7 +151,8 @@ describe('datasource_srv', () => {
it('Can get list of data sources with variables: true', () => {
const list = dataSourceSrv.getList({ metrics: true, variables: true });
expect(list[0].name).toBe('${datasource}');
expect(list[0].name).toBe('${datasourceDefault}');
expect(list[1].name).toBe('${datasource}');
});
it('Can get list of data sources with tracing: true', () => {