Hello!

Let’s check how you can use go to export dashboards from Grafana. For interaction with Grafana I use sdk. Sdk can be installed using go get github.com/grafana-tools/sdk.

Let’s start writing code. At the beginning of the file will be the package, import and main function

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/grafana-tools/sdk"
)

func main() {

}
)

I will add some parameters:

	var (
		boardLinks []sdk.FoundBoard
		rawBoard   sdk.Board
		meta       sdk.BoardProperties
        err        error
        grafanaURL string
        apiKey     string
        directory  string
    )

Links to dashboards in Grafana will be added to boardLinks. The rawBoard will store the dashboard itself. meta will be used to get the name of the dashboard and save it with that name in a file. directory directory in which dashboards will be stored. grafanaURL - Grafana url with port in the format http:/127.0.0.1:3030 and apiKey for authorization in Grafana. You need to enter your value in grafanaUR, apiKey and directory.

Next i am creating a Grafana client

	ctx := context.Background()

    c := sdk.NewClient(grafanaURL, apiKey, sdk.DefaultHTTPClient)

And now with the help of the client you can make requests to Grafana api. And the first thing you can get is a list of links to dashboards

	if boardLinks, err = c.Search(ctx); err != nil {
		log.Fatalln(err)
	}

Now that the link to the dashboards has been obtained, you can get the dashboards themselves and write them to a file.

	for _, link := range boardLinks {
		if rawBoard, meta, err = c.GetDashboardByUID(ctx, link.UID); err != nil {
			log.Printf("%s for %s\n", err, link.URI)
			continue
		}
		rawBoard.ID = 0
		writeDashboardToFile(directory, rawBoard, meta.Slug)
	}

In a cycle I pass on all links to dashboards in slice boardLinks and on each of them I call method GetDashboardByUID which I accept in parameters a context and UID of a dashboard.

It is also important that the ID field is not present in saved dashboard, otherwise Grafana will not allow you to export it. Because in sdk.Board it has type uint and omitempty I can give it a value of 0 and this field will not be added.

ID              uint       `json:"id,omitempty"`

And in the end the writeDashboardToFile method which accepts three parameters the directory where to store a dashboard, the dashboard itself, and the name of a dashboard which can be received from parameter meta.

What is left is to create the writeDashboardToFile method

func writeDashboardToFile(directory string, dashboard sdk.Board, name string, tag string) {
	var (
		err           error
		dashboardFile *os.File
		fileName      string
	)

	if _, err = os.Stat(directory); os.IsNotExist(err) {
		os.MkdirAll(directory, 0755)
	}

	fileName = fmt.Sprintf("%s/%s.json", directory, name)
	dashboardFile, err = os.Create(fileName)
	if err != nil {
		log.Printf("failed to create file for dashboard %s", fileName)
	}

	defer dashboardFile.Close()

	err = json.NewEncoder(dashboardFile).Encode(dashboard)
	if err != nil {
		log.Printf("failed to encode dashboard json to file %s", fileName)
	}
}

In this method I check if there is a directory, if not I create it. Next in this directory, I create a file for dashboard using meta parameter as name and use json.NewEncoder to write json to a file.