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

* Failing tests

* Fixed
This commit is contained in:
Dominik Prokop 2021-02-19 12:31:28 +01:00 committed by GitHub
parent ce63df425d
commit 0d6e5298b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -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;
}

View File

@ -11,9 +11,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;
},
};
@ -117,6 +126,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', () => {
@ -135,7 +150,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', () => {