From 5013996fa3930c605a114be9f24f0a8dd56b5d1d Mon Sep 17 00:00:00 2001 From: Morten Lied Johansen Date: Tue, 14 Feb 2023 11:07:12 +0100 Subject: [PATCH] Add support for setting slug and uid in YAML --- dashboard/dashboard.go | 9 +++++++++ decoder/dashboard.go | 10 ++++++++++ decoder/dashboard_test.go | 2 ++ decoder/testdata/general_options.json | 3 ++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/dashboard/dashboard.go b/dashboard/dashboard.go index d3b07b0e..b1767ef8 100644 --- a/dashboard/dashboard.go +++ b/dashboard/dashboard.go @@ -155,6 +155,15 @@ func UID(uid string) Option { } } +// Slug sets the Slug used by the dashboard. +func Slug(slug string) Option { + return func(builder *Builder) error { + builder.board.Slug = slug + + return nil + } +} + // VariableAsCustom adds a templated variable, defined as a set of custom // values. // See https://grafana.com/docs/grafana/latest/reference/templating/#variable-types diff --git a/decoder/dashboard.go b/decoder/dashboard.go index 1537eecb..ee6200e8 100644 --- a/decoder/dashboard.go +++ b/decoder/dashboard.go @@ -12,6 +12,8 @@ var ErrInvalidTimezone = fmt.Errorf("invalid timezone") type DashboardModel struct { Title string + Slug string + UID string `yaml:",omitempty"` Editable bool SharedCrosshair bool `yaml:"shared_crosshair"` Tags []string @@ -38,6 +40,14 @@ func (d *DashboardModel) ToBuilder() (dashboard.Builder, error) { opts = append(opts, dashboard.Tags(d.Tags)) } + if len(d.Slug) != 0 { + opts = append(opts, dashboard.Slug(d.Slug)) + } + + if len(d.UID) != 0 { + opts = append(opts, dashboard.UID(d.UID)) + } + for _, externalLink := range d.ExternalLinks { opts = append(opts, dashboard.ExternalLinks(externalLink.toModel())) } diff --git a/decoder/dashboard_test.go b/decoder/dashboard_test.go index 5416d5c6..df83e871 100644 --- a/decoder/dashboard_test.go +++ b/decoder/dashboard_test.go @@ -314,6 +314,8 @@ rows: func generalOptions() testCase { yaml := `title: Awesome dashboard +slug: this_is_a_slug +uid: my_uid editable: true shared_crosshair: true tags: [generated, yaml] diff --git a/decoder/testdata/general_options.json b/decoder/testdata/general_options.json index 3880d495..cf748464 100644 --- a/decoder/testdata/general_options.json +++ b/decoder/testdata/general_options.json @@ -1,5 +1,6 @@ { - "slug": "", + "slug": "this_is_a_slug", + "uid": "my_uid", "title": "Awesome dashboard", "originalTitle": "", "tags": ["generated", "yaml"],