From 4791d914089fc6e8e499a0e3e5504ed07a29724f Mon Sep 17 00:00:00 2001 From: Ben Sully Date: Wed, 25 Sep 2024 12:04:45 +0100 Subject: [PATCH] fix(datasources): add option to avoid adding '-- Grafana --' DS (#90175) * fix(datasources): add option to avoid adding '-- Grafana --' DS Currently the `getList` method of `DatasourceSrv` adds the '-- Grafana --' datasource in the majority of situations, unless a few of the other filters are set, all of which affect the results in other ways. This is the case even if the `filter` function is passed. This causes the `DataSourcePicker` component to include the '-- Grafana --' datasource in cases it's unsupported, such as in Grafana ML where we only support specific datasource types. This commit adds a new optional `grafana` field to the filter interface. If explicitly set to `false`, the '-- Grafana --' datasource will not be added to the list of datasources returned. This should be backwards compatible and should allow developers to prevent that datasource from appearing in the `DataSourcePicker`. Relates to https://github.com/grafana/machine-learning/issues/4578. * Use filter func to see if we should add '-- Grafana --', instead --- public/app/features/plugins/datasource_srv.ts | 2 +- public/app/features/plugins/tests/datasource_srv.test.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/public/app/features/plugins/datasource_srv.ts b/public/app/features/plugins/datasource_srv.ts index 4ac58e3adfd..a71f46e5f9b 100644 --- a/public/app/features/plugins/datasource_srv.ts +++ b/public/app/features/plugins/datasource_srv.ts @@ -299,7 +299,7 @@ export class DatasourceSrv implements DataSourceService { if (!filters.tracing) { const grafanaInstanceSettings = this.getInstanceSettings('-- Grafana --'); - if (grafanaInstanceSettings) { + if (grafanaInstanceSettings && filters.filter?.(grafanaInstanceSettings) !== false) { base.push(grafanaInstanceSettings); } } diff --git a/public/app/features/plugins/tests/datasource_srv.test.ts b/public/app/features/plugins/tests/datasource_srv.test.ts index f9ec6eba97e..9d9dac7b553 100644 --- a/public/app/features/plugins/tests/datasource_srv.test.ts +++ b/public/app/features/plugins/tests/datasource_srv.test.ts @@ -297,6 +297,11 @@ describe('datasource_srv', () => { expect(list[2].name).toBe('${datasource}'); }); + it('Should filter out the -- Grafana -- datasource', () => { + const list = dataSourceSrv.getList({ filter: (x) => x.name !== '-- Grafana --' }); + expect(list.find((x) => x.name === '-- Grafana --')).toBeUndefined(); + }); + it('Can get list of data sources with tracing: true', () => { const list = dataSourceSrv.getList({ tracing: true }); expect(list[0].name).toBe('Jaeger');