[v10.2.x] Bug: Fix DatasourceSrv getList with aliases (#77346)

Bug: Fix DatasourceSrv getList with aliases (#77027)

(cherry picked from commit 314bea935f)

Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2023-10-30 11:18:51 +02:00
committed by GitHub
co-authored by Andres Martinez Gotor
parent 3c55fe25a5
commit c82b563bbd
2 changed files with 30 additions and 2 deletions
@@ -1,3 +1,5 @@
import { some } from 'lodash';
import {
AppEvents,
DataSourceApi,
@@ -222,7 +224,10 @@ export class DatasourceSrv implements DataSourceService {
if (filters.alerting && !x.meta.alerting) {
return false;
}
if (filters.pluginId && x.meta.id !== filters.pluginId) {
if (
filters.pluginId &&
!(x.meta.id === filters.pluginId || some(x.meta.aliasIDs, (id) => id === filters.pluginId))
) {
return false;
}
if (filters.filter && !filters.filter(x)) {
@@ -129,6 +129,11 @@ describe('datasource_srv', () => {
uid: 'no-query',
meta: { id: 'no-query' },
},
TestData: {
type: 'grafana-testdata-datasource',
name: 'TestData',
meta: { metrics: true, id: 'grafana-testdata-datasource', aliasIDs: ['testdata'] },
},
};
describe('Given a list of data sources', () => {
@@ -273,7 +278,7 @@ describe('datasource_srv', () => {
describe('when getting external metric sources', () => {
it('should return list of explore sources', () => {
const externalSources = dataSourceSrv.getExternal();
expect(externalSources.length).toBe(7);
expect(externalSources.length).toBe(8);
});
});
@@ -307,6 +312,12 @@ describe('datasource_srv', () => {
expect(list.length).toBe(1);
});
it('Can get get list and filter by an alias', () => {
const list = dataSourceSrv.getList({ pluginId: 'testdata' });
expect(list[0].name).toBe('TestData');
expect(list.length).toBe(1);
});
it('Can get list of data sources with metrics: true, builtIn: true, mixed: true', () => {
expect(dataSourceSrv.getList({ metrics: true, dashboard: true, mixed: true })).toMatchInlineSnapshot(`
[
@@ -344,6 +355,18 @@ describe('datasource_srv', () => {
"type": "test-db",
"uid": "uid-code-mmm",
},
{
"meta": {
"aliasIDs": [
"testdata",
],
"id": "grafana-testdata-datasource",
"metrics": true,
},
"name": "TestData",
"type": "grafana-testdata-datasource",
"uid": "TestData",
},
{
"meta": {
"metrics": true,