v2 conversion error: Throw DashboardVersionError correctly to redirecr to v1 API (#114772)
v2 conversion error: Throw DashboardVersionError correctly
This commit is contained in:
@@ -13,6 +13,10 @@ export function isV2StoredVersion(version: string | undefined): boolean {
|
||||
return version === 'v2alpha1' || version === 'v2beta1';
|
||||
}
|
||||
|
||||
export function isV0V1StoredVersion(version: string | undefined): boolean {
|
||||
return version === 'v0alpha1' || version === 'v1alpha1' || version === 'v1beta1';
|
||||
}
|
||||
|
||||
export function getDashboardsApiVersion(responseFormat?: 'v1' | 'v2') {
|
||||
const isDashboardSceneEnabled = config.featureToggles.dashboardScene;
|
||||
const isKubernetesDashboardsEnabled = config.featureToggles.kubernetesDashboards;
|
||||
|
||||
@@ -317,7 +317,7 @@ describe('v2 dashboard API', () => {
|
||||
});
|
||||
|
||||
describe('version error handling', () => {
|
||||
it('should not throw DashboardVersionError for v0alpha1 conversion error and v2 spec', async () => {
|
||||
it('should throw DashboardVersionError for v0alpha1 conversion error and v2 spec', async () => {
|
||||
const mockDashboardWithError = {
|
||||
...mockDashboardDto,
|
||||
status: {
|
||||
@@ -332,7 +332,7 @@ describe('v2 dashboard API', () => {
|
||||
mockGet.mockResolvedValueOnce(mockDashboardWithError);
|
||||
|
||||
const api = new K8sDashboardV2API();
|
||||
await expect(api.getDashboardDTO('test')).resolves.toBe(mockDashboardWithError);
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented');
|
||||
});
|
||||
|
||||
it('should throw DashboardVersionError for v0alpha1 conversion error and v1 spec', async () => {
|
||||
@@ -358,7 +358,7 @@ describe('v2 dashboard API', () => {
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented');
|
||||
});
|
||||
|
||||
it('should not throw DashboardVersionError for v1beta1 conversion error and v2 spec', async () => {
|
||||
it('should throw DashboardVersionError for v1beta1 conversion error and v2 spec', async () => {
|
||||
const mockDashboardWithError = {
|
||||
...mockDashboardDto,
|
||||
status: {
|
||||
@@ -373,7 +373,7 @@ describe('v2 dashboard API', () => {
|
||||
mockGet.mockResolvedValueOnce(mockDashboardWithError);
|
||||
|
||||
const api = new K8sDashboardV2API();
|
||||
await expect(api.getDashboardDTO('test')).resolves.toBe(mockDashboardWithError);
|
||||
await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented');
|
||||
});
|
||||
|
||||
it('should throw DashboardVersionError for v1beta1 conversion error and v1 spec', async () => {
|
||||
|
||||
@@ -24,7 +24,7 @@ import { DashboardDTO, SaveDashboardResponseDTO } from 'app/types/dashboard';
|
||||
import { SaveDashboardCommand } from '../components/SaveDashboard/types';
|
||||
|
||||
import { DashboardAPI, DashboardVersionError, DashboardWithAccessInfo, ListDeletedDashboardsOptions } from './types';
|
||||
import { isDashboardV2Spec } from './utils';
|
||||
import { isV0V1StoredVersion } from './utils';
|
||||
|
||||
export const K8S_V2_DASHBOARD_API_CONFIG = {
|
||||
group: 'dashboard.grafana.app',
|
||||
@@ -44,16 +44,10 @@ export class K8sDashboardV2API
|
||||
async getDashboardDTO(uid: string) {
|
||||
try {
|
||||
const dashboard = await this.client.subresource<DashboardWithAccessInfo<DashboardV2Spec>>(uid, 'dto');
|
||||
|
||||
// FOR /dto calls returning v2 spec we are ignoring the conversion status to avoid runtime errors caused by the status
|
||||
// being saved for v2 resources that's been client-side converted to v2 and then PUT to the API server.
|
||||
if (
|
||||
!isDashboardV2Spec(dashboard.spec) &&
|
||||
dashboard.status?.conversion?.failed &&
|
||||
(dashboard.status.conversion.storedVersion === 'v1alpha1' ||
|
||||
dashboard.status.conversion.storedVersion === 'v1beta1' ||
|
||||
dashboard.status.conversion.storedVersion === 'v0alpha1')
|
||||
) {
|
||||
// This could come as conversion error from v0 or v2 to V1.
|
||||
if (dashboard.status?.conversion?.failed && isV0V1StoredVersion(dashboard.status.conversion.storedVersion)) {
|
||||
throw new DashboardVersionError(dashboard.status.conversion.storedVersion, dashboard.status.conversion.error);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user