Skip to content

Commit

Permalink
Add support for setting slug and uid in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenlj committed Feb 14, 2023
1 parent 5553b84 commit 5013996
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions decoder/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()))
}
Expand Down
2 changes: 2 additions & 0 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion decoder/testdata/general_options.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"slug": "",
"slug": "this_is_a_slug",
"uid": "my_uid",
"title": "Awesome dashboard",
"originalTitle": "",
"tags": ["generated", "yaml"],
Expand Down

0 comments on commit 5013996

Please sign in to comment.