mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Dashboards: Use dashboard_api k8s wrapper for save options also (#89598)
This commit is contained in:
parent
1040dc1baf
commit
529d4e1169
@ -3100,6 +3100,9 @@ exports[`better eslint`] = {
|
||||
"public/app/features/dashboard-scene/utils/PanelModelCompatibilityWrapper.ts:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
],
|
||||
"public/app/features/dashboard/api/dashboard_api.ts:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
],
|
||||
"public/app/features/dashboard/components/AddLibraryPanelWidget/index.ts:5381": [
|
||||
[0, 0, 0, "Do not re-export imported variable (\`./AddLibraryPanelWidget\`)", "0"]
|
||||
],
|
||||
|
@ -11,6 +11,9 @@ import {
|
||||
ResourceList,
|
||||
ResourceClient,
|
||||
ObjectMeta,
|
||||
AnnoKeyOriginPath,
|
||||
AnnoKeyOriginHash,
|
||||
AnnoKeyOriginName,
|
||||
} from './types';
|
||||
|
||||
export interface GroupVersionResource {
|
||||
@ -103,7 +106,7 @@ function setOriginAsUI(meta: Partial<ObjectMeta>) {
|
||||
if (!meta.annotations) {
|
||||
meta.annotations = {};
|
||||
}
|
||||
meta.annotations.AnnoKeyOriginName = 'UI';
|
||||
meta.annotations.AnnoKeyOriginPath = window.location.pathname;
|
||||
meta.annotations.AnnoKeyOriginHash = config.buildInfo.versionString;
|
||||
meta.annotations[AnnoKeyOriginName] = 'UI';
|
||||
meta.annotations[AnnoKeyOriginPath] = window.location.pathname;
|
||||
meta.annotations[AnnoKeyOriginHash] = config.buildInfo.versionString;
|
||||
}
|
||||
|
@ -37,9 +37,9 @@ export const AnnoKeyMessage = 'grafana.app/message';
|
||||
export const AnnoKeySlug = 'grafana.app/slug';
|
||||
|
||||
// Identify where values came from
|
||||
const AnnoKeyOriginName = 'grafana.app/originName';
|
||||
const AnnoKeyOriginPath = 'grafana.app/originPath';
|
||||
const AnnoKeyOriginHash = 'grafana.app/originHash';
|
||||
export const AnnoKeyOriginName = 'grafana.app/originName';
|
||||
export const AnnoKeyOriginPath = 'grafana.app/originPath';
|
||||
export const AnnoKeyOriginHash = 'grafana.app/originHash';
|
||||
const AnnoKeyOriginTimestamp = 'grafana.app/originTimestamp';
|
||||
|
||||
type GrafanaAnnotations = {
|
||||
|
@ -331,17 +331,15 @@ export const browseDashboardsAPI = createApi({
|
||||
|
||||
// save an existing dashboard
|
||||
saveDashboard: builder.mutation<SaveDashboardResponseDTO, SaveDashboardCommand>({
|
||||
query: ({ dashboard, folderUid, message, overwrite, showErrorAlert }) => ({
|
||||
url: `/dashboards/db`,
|
||||
method: 'POST',
|
||||
showErrorAlert,
|
||||
data: {
|
||||
dashboard,
|
||||
folderUid,
|
||||
message: message ?? '',
|
||||
overwrite: Boolean(overwrite),
|
||||
},
|
||||
}),
|
||||
queryFn: async (cmd) => {
|
||||
try {
|
||||
const rsp = await getDashboardAPI().saveDashboard(cmd);
|
||||
return { data: rsp };
|
||||
} catch (error) {
|
||||
return { error };
|
||||
}
|
||||
},
|
||||
|
||||
onQueryStarted: ({ folderUid }, { queryFulfilled, dispatch }) => {
|
||||
dashboardWatcher.ignoreNextSave();
|
||||
queryFulfilled.then(async () => {
|
||||
|
@ -28,6 +28,7 @@ export function useSaveDashboard(isCopy = false) {
|
||||
message: options.message,
|
||||
overwrite: options.overwrite,
|
||||
showErrorAlert: false,
|
||||
k8s: undefined, // TODO? pass the original metadata
|
||||
});
|
||||
|
||||
if ('error' in result) {
|
||||
|
@ -1,6 +1,12 @@
|
||||
import { config, getBackendSrv } from '@grafana/runtime';
|
||||
import { ScopedResourceClient } from 'app/features/apiserver/client';
|
||||
import { Resource, ResourceClient } from 'app/features/apiserver/types';
|
||||
import {
|
||||
AnnoKeyFolder,
|
||||
AnnoKeyMessage,
|
||||
Resource,
|
||||
ResourceClient,
|
||||
ResourceForCreate,
|
||||
} from 'app/features/apiserver/types';
|
||||
import { SaveDashboardCommand } from 'app/features/dashboard/components/SaveDashboard/types';
|
||||
import { dashboardWatcher } from 'app/features/live/dashboard/dashboardWatcher';
|
||||
import { DeleteDashboardResponse } from 'app/features/manage-dashboards/types';
|
||||
@ -53,7 +59,7 @@ interface DashboardWithAccessInfo extends Resource<DashboardDataDTO, 'DashboardW
|
||||
class K8sDashboardAPI implements DashboardAPI {
|
||||
private client: ResourceClient<DashboardDataDTO>;
|
||||
|
||||
constructor(private legacy: DashboardAPI) {
|
||||
constructor() {
|
||||
this.client = new ScopedResourceClient<DashboardDataDTO>({
|
||||
group: 'dashboard.grafana.app',
|
||||
version: 'v0alpha1',
|
||||
@ -62,23 +68,69 @@ class K8sDashboardAPI implements DashboardAPI {
|
||||
}
|
||||
|
||||
saveDashboard(options: SaveDashboardCommand): Promise<SaveDashboardResponseDTO> {
|
||||
return this.legacy.saveDashboard(options);
|
||||
const dashboard = options.dashboard as DashboardDataDTO; // type for the uid property
|
||||
const obj: ResourceForCreate<DashboardDataDTO> = {
|
||||
metadata: {
|
||||
...options?.k8s,
|
||||
},
|
||||
spec: {
|
||||
...dashboard,
|
||||
},
|
||||
};
|
||||
|
||||
if (options.message) {
|
||||
obj.metadata.annotations = {
|
||||
...obj.metadata.annotations,
|
||||
[AnnoKeyMessage]: options.message,
|
||||
};
|
||||
} else if (obj.metadata.annotations) {
|
||||
delete obj.metadata.annotations[AnnoKeyMessage];
|
||||
}
|
||||
|
||||
if (options.folderUid) {
|
||||
obj.metadata.annotations = {
|
||||
...obj.metadata.annotations,
|
||||
[AnnoKeyFolder]: options.folderUid,
|
||||
};
|
||||
}
|
||||
|
||||
if (dashboard.uid) {
|
||||
obj.metadata.name = dashboard.uid;
|
||||
return this.client.update(obj).then((v) => this.asSaveDashboardResponseDTO(v));
|
||||
}
|
||||
return this.client.create(obj).then((v) => this.asSaveDashboardResponseDTO(v));
|
||||
}
|
||||
|
||||
asSaveDashboardResponseDTO(v: Resource<DashboardDataDTO>): SaveDashboardResponseDTO {
|
||||
return {
|
||||
uid: v.metadata.name,
|
||||
version: v.spec.version ?? 0,
|
||||
id: v.spec.id ?? 0,
|
||||
status: 'success',
|
||||
slug: '',
|
||||
url: '',
|
||||
};
|
||||
}
|
||||
|
||||
deleteDashboard(uid: string, showSuccessAlert: boolean): Promise<DeleteDashboardResponse> {
|
||||
return this.legacy.deleteDashboard(uid, showSuccessAlert);
|
||||
return this.client.delete(uid).then((v) => ({
|
||||
id: 0,
|
||||
message: v.message,
|
||||
title: 'deleted',
|
||||
}));
|
||||
}
|
||||
|
||||
async getDashboardDTO(uid: string): Promise<DashboardDTO> {
|
||||
const dto = await this.client.subresource<DashboardWithAccessInfo>(uid, 'dto');
|
||||
const dash = await this.client.subresource<DashboardWithAccessInfo>(uid, 'dto');
|
||||
return {
|
||||
meta: {
|
||||
...dto.access,
|
||||
...dash.access,
|
||||
isNew: false,
|
||||
isFolder: false,
|
||||
uid: dto.metadata.name,
|
||||
uid: dash.metadata.name,
|
||||
k8s: dash.metadata,
|
||||
},
|
||||
dashboard: dto.spec,
|
||||
dashboard: dash.spec,
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -87,8 +139,7 @@ let instance: DashboardAPI | undefined = undefined;
|
||||
|
||||
export function getDashboardAPI() {
|
||||
if (!instance) {
|
||||
const legacy = new LegacyDashboardAPI();
|
||||
instance = config.featureToggles.kubernetesDashboards ? new K8sDashboardAPI(legacy) : legacy;
|
||||
instance = config.featureToggles.kubernetesDashboards ? new K8sDashboardAPI() : new LegacyDashboardAPI();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { Dashboard } from '@grafana/schema';
|
||||
import { ObjectMeta } from 'app/features/apiserver/types';
|
||||
import { CloneOptions, DashboardModel } from 'app/features/dashboard/state/DashboardModel';
|
||||
import { Diffs } from 'app/features/dashboard-scene/settings/version-history/utils';
|
||||
|
||||
@ -22,6 +23,9 @@ export interface SaveDashboardCommand {
|
||||
folderUid?: string;
|
||||
overwrite?: boolean;
|
||||
showErrorAlert?: boolean;
|
||||
|
||||
// When loading dashboards from k8s, we need to have access to the metadata wrapper
|
||||
k8s?: Partial<ObjectMeta>;
|
||||
}
|
||||
|
||||
export interface SaveDashboardFormProps {
|
||||
|
@ -26,6 +26,7 @@ const saveDashboard = async (
|
||||
folderUid: options.folderUid ?? dashboard.meta.folderUid ?? saveModel.meta?.folderUid,
|
||||
message: options.message,
|
||||
overwrite: options.overwrite,
|
||||
k8s: dashboard.meta.k8s,
|
||||
});
|
||||
|
||||
if ('error' in query) {
|
||||
@ -70,7 +71,7 @@ export const useDashboardSave = (isCopy = false) => {
|
||||
const currentPath = locationService.getLocation().pathname;
|
||||
const newUrl = locationUtil.stripBaseFromUrl(result.url);
|
||||
|
||||
if (newUrl !== currentPath) {
|
||||
if (newUrl !== currentPath && result.url) {
|
||||
setTimeout(() => locationService.replace(newUrl));
|
||||
}
|
||||
if (dashboard.meta.isStarred) {
|
||||
|
@ -1,5 +1,3 @@
|
||||
import { lastValueFrom } from 'rxjs';
|
||||
|
||||
import { AppEvents } from '@grafana/data';
|
||||
import { BackendSrvRequest } from '@grafana/runtime';
|
||||
import { Dashboard } from '@grafana/schema';
|
||||
@ -28,15 +26,6 @@ export interface SaveDashboardOptions {
|
||||
refresh?: string;
|
||||
}
|
||||
|
||||
interface SaveDashboardResponse {
|
||||
id: number;
|
||||
slug: string;
|
||||
status: string;
|
||||
uid: string;
|
||||
url: string;
|
||||
version: number;
|
||||
}
|
||||
|
||||
export class DashboardSrv {
|
||||
dashboard?: DashboardModel;
|
||||
|
||||
@ -75,17 +64,12 @@ export class DashboardSrv {
|
||||
data: SaveDashboardOptions,
|
||||
requestOptions?: Pick<BackendSrvRequest, 'showErrorAlert' | 'showSuccessAlert'>
|
||||
) {
|
||||
return lastValueFrom(
|
||||
getBackendSrv().fetch<SaveDashboardResponse>({
|
||||
url: '/api/dashboards/db/',
|
||||
method: 'POST',
|
||||
data: {
|
||||
...data,
|
||||
dashboard: data.dashboard.getSaveModelClone(),
|
||||
},
|
||||
...requestOptions,
|
||||
})
|
||||
);
|
||||
return getDashboardAPI().saveDashboard({
|
||||
message: data.message,
|
||||
folderUid: data.folderUid,
|
||||
dashboard: data.dashboard.getSaveModelClone(),
|
||||
showErrorAlert: requestOptions?.showErrorAlert,
|
||||
});
|
||||
}
|
||||
|
||||
starDashboard(dashboardUid: string, isStarred: boolean) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { DataQuery } from '@grafana/data';
|
||||
import { Dashboard, DataSourceRef } from '@grafana/schema';
|
||||
import { ObjectMeta } from 'app/features/apiserver/types';
|
||||
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
|
||||
|
||||
export interface DashboardDTO {
|
||||
@ -67,6 +68,11 @@ export interface DashboardMeta {
|
||||
dashboardNotFound?: boolean;
|
||||
isEmbedded?: boolean;
|
||||
isNew?: boolean;
|
||||
|
||||
// When loaded from kubernetes, we stick the raw metadata here
|
||||
// yes weird, but this means all the editor structures can exist unchanged
|
||||
// until we use the resource as the main container
|
||||
k8s?: Partial<ObjectMeta>;
|
||||
}
|
||||
|
||||
export interface AnnotationActions {
|
||||
|
Loading…
Reference in New Issue
Block a user