Skip to content

Commit

Permalink
Merge pull request #205 from mortenlj/yaml-uid-slug
Browse files Browse the repository at this point in the history
Add support for setting slug and uid in YAML
  • Loading branch information
K-Phoen authored Feb 19, 2023
2 parents 5553b84 + d709c33 commit e5a4bd2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
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
12 changes: 11 additions & 1 deletion dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/json"
"testing"

"github.com/K-Phoen/grabana/variable/datasource"
"github.com/stretchr/testify/require"

"github.com/K-Phoen/grabana/variable/datasource"
)

func requireJSON(t *testing.T, payload []byte) {
Expand Down Expand Up @@ -82,6 +83,15 @@ func TestDashboardUIDCanBeSet(t *testing.T) {
req.Equal("foo", panel.board.UID)
}

func TestDashboardSlugCanBeSet(t *testing.T) {
req := require.New(t)

panel, err := New("", Slug("super-slug"))

req.NoError(err)
req.Equal("super-slug", panel.board.Slug)
}

func TestDashboardUIDWillBeHashedWhenTooLongForGrafana(t *testing.T) {
req := require.New(t)

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 `yaml:",omitempty"`
UID string `yaml:"uid,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 d.Slug != "" {
opts = append(opts, dashboard.Slug(d.Slug))
}

if d.UID != "" {
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 e5a4bd2

Please sign in to comment.