Skip to content

Disappearing constant variables after getting and setting the same dashboard #173

@MinusNull

Description

@MinusNull

I use grafana v8.0.2

My dashboard has following json model:

... "templating": { "list": [ { "datasource": null, "description": null, "error": null, "hide": 2, "includeAll": false, "label": "", "multi": false, "name": "period", "query": "1d", "refresh": false, "regex": "", "skipUrlSync": false, "type": "constant" } ] }, ...

After that code my dashboard has constant variables lost

board, properties, err := client.GetDashboardByUID(context.Background(), grafanaDashboardUid)
if err != nil {
	log.Fatal(err)
}

_, err = client.SetDashboard(context.Background(), board, sdk.SetDashboardParams{
	FolderID: properties.FolderID,
	Overwrite:  true,
})
if err != nil {
	log.Fatal(err)
}

Json model:
"templating": { "list": [] },

But if i use raw functions everything is fine

boardJson, properties, err := client.GetRawDashboardByUID(context.Background(), grafanaDashboardUid)
if err != nil {
	log.Fatal(err)
}

_, err = client.SetRawDashboardWithParam(context.Background(), sdk.RawBoardRequest{
	Dashboard:  boardJson,
	Parameters: sdk.SetDashboardParams{
		FolderID:   properties.FolderID,
		Overwrite:  true,
	},
})
if err != nil {
	log.Fatal(err)
}

I've found out that Options field of TemplateVar struct should have omitempty tag not to cause disappearing constant variables.
https://github.com/grafana-tools/sdk/blob/master/board.go#L88

Without omitempty tag Grafana sees that my constant variable has options json field and doesn't apply dashboard correctly.

But why Grafana has such behavior i don't know.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions