From 3dffe4099b97bd86f0930af24eb9da4d116d66d3 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 6 Feb 2023 05:36:00 -0500 Subject: [PATCH] [v9.4.x] Refactor: Use data source UID rather than ID (#62937) Refactor: Use data source UID rather than ID (#62781) (cherry picked from commit 9893408af988e275893a9cd3579dcfa0c2c830ed) Co-authored-by: Andres Martinez Gotor --- .../src/utils/DataSourceWithBackend.test.ts | 51 +++++++++++++++++++ .../src/utils/DataSourceWithBackend.ts | 6 +-- .../dashboard/services/DashboardLoaderSrv.ts | 2 +- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/packages/grafana-runtime/src/utils/DataSourceWithBackend.test.ts b/packages/grafana-runtime/src/utils/DataSourceWithBackend.test.ts index 34a60eb96bb..ab5d5baf508 100644 --- a/packages/grafana-runtime/src/utils/DataSourceWithBackend.test.ts +++ b/packages/grafana-runtime/src/utils/DataSourceWithBackend.test.ts @@ -182,6 +182,57 @@ describe('DataSourceWithBackend', () => { obs = toStreamingDataResponse(rsp, request, standardStreamOptionsProvider); expect(obs).toBeDefined(); }); + + test('check that getResource uses the data source UID', () => { + const { mock, ds } = createMockDatasource(); + ds.getResource('foo'); + + const args = mock.calls[0][0]; + + expect(mock.calls.length).toBe(1); + expect(args).toMatchObject({ + headers: { + 'X-Datasource-Uid': 'abc', + 'X-Plugin-Id': 'dummy', + }, + method: 'GET', + url: '/api/datasources/uid/abc/resources/foo', + }); + }); + + test('check that postResource uses the data source UID', () => { + const { mock, ds } = createMockDatasource(); + ds.postResource('foo'); + + const args = mock.calls[0][0]; + + expect(mock.calls.length).toBe(1); + expect(args).toMatchObject({ + headers: { + 'X-Datasource-Uid': 'abc', + 'X-Plugin-Id': 'dummy', + }, + method: 'POST', + url: '/api/datasources/uid/abc/resources/foo', + }); + }); + + test('check that callHealthCheck uses the data source UID', () => { + const { mock, ds } = createMockDatasource(); + ds.callHealthCheck(); + + const args = mock.calls[0][0]; + + expect(mock.calls.length).toBe(1); + expect(args).toMatchObject({ + headers: { + 'X-Datasource-Uid': 'abc', + 'X-Plugin-Id': 'dummy', + }, + method: 'GET', + url: '/api/datasources/uid/abc/health', + }); + }); }); function createMockDatasource() { diff --git a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts index 1063fb1a2ce..da3c3d9f522 100644 --- a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts +++ b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts @@ -281,7 +281,7 @@ class DataSourceWithBackend< method: 'GET', headers: options?.headers ? { ...options.headers, ...headers } : headers, params: params ?? options?.params, - url: `/api/datasources/${this.id}/resources/${path}`, + url: `/api/datasources/uid/${this.uid}/resources/${path}`, }) ); return result.data; @@ -302,7 +302,7 @@ class DataSourceWithBackend< method: 'POST', headers: options?.headers ? { ...options.headers, ...headers } : headers, data: data ?? { ...data }, - url: `/api/datasources/${this.id}/resources/${path}`, + url: `/api/datasources/uid/${this.uid}/resources/${path}`, }) ); return result.data; @@ -315,7 +315,7 @@ class DataSourceWithBackend< return lastValueFrom( getBackendSrv().fetch({ method: 'GET', - url: `/api/datasources/${this.id}/health`, + url: `/api/datasources/uid/${this.uid}/health`, showErrorAlert: false, headers: this.getRequestHeaders(), }) diff --git a/public/app/features/dashboard/services/DashboardLoaderSrv.ts b/public/app/features/dashboard/services/DashboardLoaderSrv.ts index 5927eb975e1..3fb7f7da586 100644 --- a/public/app/features/dashboard/services/DashboardLoaderSrv.ts +++ b/public/app/features/dashboard/services/DashboardLoaderSrv.ts @@ -133,7 +133,7 @@ export class DashboardLoaderSrv { }); return getBackendSrv() - .get(`/api/datasources/${ds.id}/resources/${path}`, queryParams) + .get(`/api/datasources/uid/${ds.uid}/resources/${path}`, queryParams) .then((data) => { return { meta: {