ImportDashboard: Fixes issue with importing dashboard and name ending up in uid (#43451)

* ImportDashboard: Fixes issue with importing dashboard and name ending up in uid

* Added unit test

* fixing import issue
This commit is contained in:
Torkel Ödegaard 2022-01-05 15:20:32 +01:00 committed by GitHub
parent 64248edbb0
commit 5c88acd5aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 6 deletions

View File

@ -128,7 +128,7 @@ export const ImportDashboardForm: FC<Props> = ({
noDefault={true}
placeholder={input.info}
pluginId={input.pluginId}
current={current[index]?.name}
current={current[index]?.uid}
/>
)}
control={control}

View File

@ -0,0 +1,75 @@
import { setBackendSrv } from '@grafana/runtime';
import { thunkTester } from 'test/core/thunk/thunkTester';
import { importDashboard } from './actions';
import { DataSourceInput, ImportDashboardDTO, initialImportDashboardState, InputType } from './reducers';
describe('importDashboard', () => {
it('Should send data source uid', async () => {
const form: ImportDashboardDTO = {
title: 'Asda',
uid: '12',
gnetId: 'asd',
constants: [],
dataSources: [
{
id: 1,
uid: 'ds-uid',
name: 'ds-name',
type: 'prometheus',
} as any,
],
elements: [],
folder: {
id: 1,
title: 'title',
},
};
let postArgs: any;
setBackendSrv({
post: (url: string, args: any) => {
postArgs = args;
return Promise.resolve({
importedUrl: '/my/dashboard',
});
},
} as any);
await thunkTester({
importDashboard: {
...initialImportDashboardState,
inputs: {
dataSources: [
{
name: 'ds-name',
pluginId: 'prometheus',
type: InputType.DataSource,
},
] as DataSourceInput[],
constants: [],
libraryPanels: [],
},
},
})
.givenThunk(importDashboard)
.whenThunkIsDispatched(form);
expect(postArgs).toEqual({
dashboard: {
title: 'Asda',
uid: '12',
},
folderId: 1,
inputs: [
{
name: 'ds-name',
pluginId: 'prometheus',
type: 'datasource',
value: 'ds-uid',
},
],
overwrite: true,
});
});
});

View File

@ -1,5 +1,4 @@
import { AppEvents, DataSourceInstanceSettings, locationUtil } from '@grafana/data';
import { getBackendSrv } from 'app/core/services/backend_srv';
import {
clearDashboard,
fetchDashboard,
@ -16,7 +15,7 @@ import {
import { DashboardDataDTO, DashboardDTO, FolderInfo, PermissionLevelString, ThunkResult } from 'app/types';
import { appEvents } from '../../../core/core';
import { dashboardWatcher } from 'app/features/live/dashboard/dashboardWatcher';
import { getDataSourceSrv, locationService } from '@grafana/runtime';
import { getDataSourceSrv, locationService, getBackendSrv } from '@grafana/runtime';
import { DashboardSearchHit } from '../../search/types';
import { getLibraryPanel } from '../../library-panels/state/api';
import { LibraryElementDTO, LibraryElementKind } from '../../library-panels/types';
@ -139,7 +138,7 @@ export function importDashboard(importDashboardForm: ImportDashboardDTO): ThunkR
name: input.name,
type: input.type,
pluginId: input.pluginId,
value: dataSource.name,
value: dataSource.uid,
});
});
@ -195,7 +194,7 @@ export function moveDashboards(dashboardUids: string[], toFolder: FolderInfo) {
}
async function moveDashboard(uid: string, toFolder: FolderInfo) {
const fullDash: DashboardDTO = await getBackendSrv().getDashboardByUid(uid);
const fullDash: DashboardDTO = await getBackendSrv().get(`/api/dashboards/uid/${uid}`);
if ((!fullDash.meta.folderId && toFolder.id === 0) || fullDash.meta.folderId === toFolder.id) {
return { alreadyInFolder: true };
@ -287,7 +286,7 @@ export function createFolder(payload: any) {
}
export function searchFolders(query: any, permission?: PermissionLevelString): Promise<DashboardSearchHit[]> {
return getBackendSrv().search({ query, type: 'dash-folder', permission });
return getBackendSrv().get('/api/search', { query, type: 'dash-folder', permission });
}
export function getFolderById(id: number): Promise<{ id: number; title: string }> {