Provisioning: Load dashboard (#102050)
* Provisioning: Load dashboard * Add v2 loader
This commit is contained in:
@@ -13,6 +13,7 @@ import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
||||
import { DashboardDTO } from 'app/types';
|
||||
|
||||
import { appEvents } from '../../../core/core';
|
||||
import { loadDashboardFromProvisioning } from '../../provisioning/dashboardLoader';
|
||||
import { ResponseTransformers } from '../api/ResponseTransformers';
|
||||
import { getDashboardAPI } from '../api/dashboard_api';
|
||||
import { DashboardVersionError, DashboardWithAccessInfo } from '../api/types';
|
||||
@@ -123,6 +124,8 @@ export class DashboardLoaderSrv extends DashboardLoaderSrvBase<DashboardDTO> {
|
||||
|
||||
if (type === 'script' && slug) {
|
||||
promise = this.loadScriptedDashboard(slug);
|
||||
} else if (type === 'provisioning' && uid && slug) {
|
||||
promise = loadDashboardFromProvisioning(slug, uid);
|
||||
// needed for the old architecture
|
||||
// in scenes this is handled through loadSnapshot method
|
||||
} else if (type === 'snapshot' && slug) {
|
||||
@@ -197,6 +200,8 @@ export class DashboardLoaderSrvV2 extends DashboardLoaderSrvBase<DashboardWithAc
|
||||
promise = backendSrv.getPublicDashboardByUid(uid).then((result) => {
|
||||
return ResponseTransformers.ensureV2Response(result);
|
||||
});
|
||||
} else if (type === 'provisioning' && uid && slug) {
|
||||
promise = loadDashboardFromProvisioning(slug, uid).then((r) => ResponseTransformers.ensureV2Response(r));
|
||||
} else if (uid) {
|
||||
if (!params) {
|
||||
const cachedDashboard = stateManager.getDashboardFromCache(uid);
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { getBackendSrv } from '@grafana/runtime';
|
||||
import { DashboardDTO } from 'app/types';
|
||||
|
||||
import { AnnoKeyManagerIdentity, AnnoKeyManagerKind, AnnoKeySourcePath } from '../apiserver/types';
|
||||
|
||||
import { BASE_URL } from './api/baseAPI';
|
||||
|
||||
/**
|
||||
*
|
||||
* Load a dashboard from repository
|
||||
*/
|
||||
export async function loadDashboardFromProvisioning(repo: string, path: string): Promise<DashboardDTO> {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const ref = params.get('ref'); // commit hash or branch
|
||||
|
||||
const url = `${BASE_URL}/repositories/${repo}/files/${path}`;
|
||||
return getBackendSrv()
|
||||
.get(url, ref ? { ref } : undefined)
|
||||
.then((v) => {
|
||||
// Load the results from dryRun
|
||||
const dryRun = v.resource.dryRun;
|
||||
if (!dryRun) {
|
||||
return Promise.reject('failed to read provisioned dashboard');
|
||||
}
|
||||
|
||||
if (!dryRun.apiVersion.startsWith('dashboard.grafana.app')) {
|
||||
return Promise.reject('unexpected resource type: ' + dryRun.apiVersion);
|
||||
}
|
||||
|
||||
// Make sure the annotation key exists
|
||||
let anno = dryRun.metadata.annotations;
|
||||
if (!anno) {
|
||||
dryRun.metadata.annotations = {};
|
||||
}
|
||||
anno[AnnoKeyManagerKind] = 'repo';
|
||||
anno[AnnoKeyManagerIdentity] = repo;
|
||||
anno[AnnoKeySourcePath] = ref ? path + '#' + ref : path;
|
||||
|
||||
return {
|
||||
meta: {
|
||||
canStar: false,
|
||||
isSnapshot: false,
|
||||
canShare: false,
|
||||
|
||||
// Should come from the repo settings
|
||||
canDelete: true,
|
||||
canSave: true,
|
||||
canEdit: true,
|
||||
|
||||
// Includes additional k8s metadata
|
||||
k8s: dryRun.metadata,
|
||||
|
||||
// lookup info
|
||||
provisioning: {
|
||||
file: url,
|
||||
ref: ref,
|
||||
repo: repo,
|
||||
},
|
||||
},
|
||||
dashboard: dryRun.spec,
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user