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
This commit is contained in:
Ben Sully 2024-09-25 12:04:45 +01:00 committed by GitHub
parent a0de3ef867
commit 4791d91408
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

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

View File

@ -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');