mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
DatasourceSrv: Fix instance retrieval when datasource variable value set to "default" (#31347)
* Failing tests * Fixed
This commit is contained in:
parent
ce63df425d
commit
0d6e5298b7
@ -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;
|
||||
}
|
||||
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user