K8S Dashboards: Prevent duplicate dashboards when updating an existing dashboard (#102835)

* use metadata for k8s v1

* handle version and uid update in the api layer

* add test

* cleanup
pull/102953/head
Haris Rozajac 4 months ago committed by GitHub
parent e5ff56ae7b
commit 960c13c1e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      public/app/features/dashboard/api/v1.test.ts
  2. 6
      public/app/features/dashboard/api/v1.ts

@ -15,10 +15,13 @@ const mockDashboardDto: DashboardWithAccessInfo<DashboardDataDTO> = {
resourceVersion: '1',
creationTimestamp: '1',
annotations: {},
generation: 1,
},
spec: {
title: 'test',
uid: 'test',
// V1 API doesn't return the uid or version in the spec
// setting it as empty string here because it's required in DashboardDataDTO
uid: '',
schemaVersion: 0,
},
access: {},
@ -142,6 +145,17 @@ describe('v1 dashboard API', () => {
expect(result.meta.folderUid).toBe('new-folder');
});
it('should correctly set uid and version in the spec', async () => {
const api = new K8sDashboardAPI();
// we are fetching the mockDashboardDTO, which doesn't have a uid or version
// and this is expected because V1 API doesn't return the uid or version in the spec
// however, we need these fields to be set in the dashboard object to avoid creating duplicates when editing an existing dashboard
// getDashboardDTO should set the uid and version from the metadata.name (uid) and metadata.generation (version)
const result = await api.getDashboardDTO('dash-uid');
expect(result.dashboard.uid).toBe('dash-uid');
expect(result.dashboard.version).toBe(1);
});
it('throws an error if folder is not found', async () => {
mockGet.mockResolvedValueOnce({
...mockDashboardDto,

@ -110,7 +110,11 @@ export class K8sDashboardAPI implements DashboardAPI<DashboardDTO, Dashboard> {
k8s: dash.metadata,
version: dash.metadata.generation,
},
dashboard: dash.spec,
dashboard: {
...dash.spec,
version: dash.metadata.generation,
uid: dash.metadata.name,
},
};
if (dash.metadata.labels?.[DeprecatedInternalId]) {

Loading…
Cancel
Save