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