grafana / grafonnet-lib

Jsonnet library for generating Grafana dashboard files.
https://grafana.github.io/grafonnet-lib/
Apache License 2.0
1.08k stars 217 forks source link

Issue with Dashboard JSON file for Grafana API #130

Closed sammmoran closed 5 years ago

sammmoran commented 5 years ago

Hello,

I have an issue with the Grafana API being able to use the JSON file generated by the Grafonnet library. It appears that Grafonnet library does not include the "dashboard" notation in the final JSON file that is needed by Grafana's API.

This is the final JSON code:

{

           (json_dashboard_code_goes_here)

}

But, the Grafana API requires the following format:

{
  "dashboard": { 
           (json_dashboard_code_goes_here)
       }
} 

If I manually type in "dashboard" and enclose the JSON Dashboard code in brackets in the file, the API successfully accepts the new Dashboard and it shows up in the Grafana GUI.

The file as it is works only when you manually copy and paste the resulting code into the "Import" option in the Grafana GUI; however, I want to use the API for simplicity and speed.

Is there a way to include the "dashboard" notation described above? Perhaps I am missing something?

Below is my use-case:

I generate the following JSON file from a simple jsonnet file:

dash.jsonnet

local grafana = import 'grafonnet-lib/grafonnet/grafana.libsonnet';
local dashboard = grafana.dashboard;

dashboard.new(
    'CloudWatch Test',
    editable=true,
    schemaVersion=16,
)

This is just a barebones Dashboard titled "CloudWatch Test".

Then, using the command: jsonnet dash.jsonnet > foo.json I successfully generate a standalone JSON file (foo.json) that I want to curl into Grafana.

After I use the following curl command: curl -XPOST -i -H "Authorization: Bearer " --data-binary @. -H "Content-Type: application/json" http://localhost:3000/api/dashboards/db

I get an HTTP 422 error. If I add the "dashboard" notation as described earlier, it works perfectly and the new Dashboard appears in Grafana GUI.

Thank you for looking into this!

roidelapluie commented 5 years ago

The JSON from grafana are for Json provisioning (files on disk).

Indeed, with the HTTP api, adding {dashboard: } is needed.

roidelapluie commented 5 years ago

https://grafana.com/docs/administration/provisioning/#dashboards

sammmoran commented 5 years ago

Thank you for your help! I really enjoy using Grafana and am exploring ways of automating the Grafana dashboard creation and panel set-up functionalities.