Skip to content

Commit

Permalink
Merge pull request #92 from grafana/http-api-envelope
Browse files Browse the repository at this point in the history
Support HTTP API style resources
  • Loading branch information
K-Phoen authored Sep 24, 2024
2 parents 6652419 + fe5b943 commit 59cbc3b
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/grafana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import vscode from 'vscode';
import YAML from 'yaml';

type Format = 'json' | 'yaml';
type Envelope = 'none' | 'grizzly';
type Envelope = 'none' | 'grizzly' | 'http_api';

export class Resource {
private readonly viewType = "grafana.dashboard";
Expand Down Expand Up @@ -43,6 +43,16 @@ export class Resource {
return new Resource(filename, format, 'none', resourceData);
}

// HTTP API envelope
// See https://grafana.com/docs/grafana/latest/developers/http_api/dashboard/#get-dashboard-by-uid
if (resourceData.dashboard && resourceData.meta) {
if (!resourceData.dashboard.uid) {
throw new Error(`malformed HTTP envelope in '${filename}: dashboard.uid field not found`);
}

return new Resource(filename, format, 'http_api', resourceData);
}

// grizzly envelope
if (resourceData.apiVersion === 'grizzly.grafana.com/v1alpha1') {
if (!resourceData.metadata || !resourceData.metadata.name) {
Expand Down Expand Up @@ -70,14 +80,23 @@ export class Resource {
return this.data.uid;
}

if (this.envelope === 'http_api') {
return this.data.dashboard.uid;
}

// grizzly style
return this.data.metadata.name;
}

public spec(): any {
if (this.envelope === 'none') {
return this.data;
}
if (this.envelope === 'http_api') {
return this.data.dashboard;
}

// grizzly style
return this.data.spec;
}

Expand All @@ -87,6 +106,11 @@ export class Resource {
return new Resource(this.filename, this.format, this.envelope, newData);
}

if (this.envelope === 'http_api') {
const newData = {...this.data, ...{dashboard: newSpec}};
return new Resource(this.filename, this.format, this.envelope, newData);
}

return new Resource(this.filename, this.format, this.envelope, newSpec);
}

Expand Down

0 comments on commit 59cbc3b

Please sign in to comment.