2

I am trying to add a dashboard to a toy Grafana local installation to debug a dashboard and play on how to setup the c.i. on our server.

I try to load the dasboard using e.g.

curl --request POST \
  --url http://localhost:3000/api/dashboards/db \
  --header 'Authorization: Bearer <my key>' \
  --header 'Content-Type: application/json' \
  --data '{<the whole dashboard json code...>}'

but I get "Alert validation error: Data source used by alert rule not found, alertName=Persistence Queues alert, datasource=".

This makes sense, cause I have not localy the datasource, however it stops me from loading the dashboard and playing around with it on my toy server. If I use import, I can load the dashboard, even with errors (but my goal is to test the http api for c.i.).

Is there a way / server config setting that will allow me to create the dashboard via the http api, even without a valid alert?

ntg
  • 12,950
  • 7
  • 74
  • 95

2 Answers2

1

The documented API with a POST to /api/dashboards/db requires a different JSON format than the exported JSON model from the Grafana UI.

You can POST your JSON to /api/dashboards/import in order to execute the same query as the UI does internally. Since this is not officially documented, expect the API to change in future versions of Grafana. Make sure that all metadata (e.g. title, uid, overwrite-flag and folderId) are set as you wish. Some of these parameters are normally shown in the UI while importing and get replaced before making the query to the API.

Example request:

curl -X POST http://localhost:3000/api/dashboards/import \
     -H "Authorization: Bearer xyz" \
     -H "Content-Type: application/json" \
     -d '{"dashboard":{"annotations": ...,"title":"Test-Import","uid":"unique-id-abc",...},"overwrite":true,...,"folderId":0}'

Example response:

{"pluginId":"","title":"Test-Import","imported":true,"importedUri":"db/test-import","importedUrl":"/d/unique-id-abc/test-import","slug":"test-import","dashboardId":15,"folderId":0,"importedRevision":1,"revision":1,"description":"","path":"","removed":false}
Matt
  • 12,848
  • 2
  • 31
  • 53
0

As mentioned by @Matt

The documented API with a POST to /api/dashboards/db requires a different JSON format than the exported JSON model from the Grafana UI.

As far as I can tell, the workaround that @Matt proposes mekes perfect sense and should work for Json files exported through the GUI. In my case, I got the Json File through GET to /api/dashboards/uid/:uid (Get dashboard by uid) In this case, the format is correct, however, one needs to set the "id" of the dashboard to null unless he is editing an existing dashboard, in which case the id should match.

So both solutions are correct, each depending on the initial Json file. In light of this I ll be accepting @Matt's answer as correct.

ntg
  • 12,950
  • 7
  • 74
  • 95