From 818cff33dc6c20542103d418d21b78cb02deae63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sun, 12 Mar 2023 23:01:21 +0100 Subject: [PATCH] Support links to dashboards in yaml decoder --- decoder/dashboard.go | 5 +++++ decoder/dashboardlink.go | 25 +++++++++++++++++++++++++ decoder/dashboardlink_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 decoder/dashboardlink.go create mode 100644 decoder/dashboardlink_test.go diff --git a/decoder/dashboard.go b/decoder/dashboard.go index a80f3f20..71d25c36 100644 --- a/decoder/dashboard.go +++ b/decoder/dashboard.go @@ -25,6 +25,7 @@ type DashboardModel struct { TagsAnnotation []dashboard.TagAnnotation `yaml:"tags_annotations,omitempty"` Variables []DashboardVariable `yaml:",omitempty"` ExternalLinks []DashboardExternalLink `yaml:"external_links,omitempty"` + DashboardLinks []DashboardInternalLink `yaml:"dashboard_links,omitempty"` Rows []DashboardRow } @@ -52,6 +53,10 @@ func (d *DashboardModel) ToBuilder() (dashboard.Builder, error) { opts = append(opts, dashboard.ExternalLinks(externalLink.toModel())) } + for _, dashboardLink := range d.DashboardLinks { + opts = append(opts, dashboard.DashboardLinks(dashboardLink.toModel())) + } + if d.AutoRefresh != "" { opts = append(opts, dashboard.AutoRefresh(d.AutoRefresh)) } diff --git a/decoder/dashboardlink.go b/decoder/dashboardlink.go new file mode 100644 index 00000000..e752ea76 --- /dev/null +++ b/decoder/dashboardlink.go @@ -0,0 +1,25 @@ +package decoder + +import ( + "github.com/K-Phoen/grabana/dashboard" +) + +type DashboardInternalLink struct { + Title string `yaml:"title"` + Tags []string `yaml:"tags"` + AsDropdown bool `yaml:"as_dropdown,omitempty"` + IncludeTimeRange bool `yaml:"include_time_range,omitempty"` + IncludeVariableValues bool `yaml:"include_variable_values,omitempty"` + OpenInNewTab bool `yaml:"open_in_new_tab,omitempty"` +} + +func (l DashboardInternalLink) toModel() dashboard.DashboardLink { + return dashboard.DashboardLink{ + Title: l.Title, + Tags: l.Tags, + AsDropdown: l.AsDropdown, + IncludeTimeRange: l.IncludeTimeRange, + IncludeVariableValues: l.IncludeVariableValues, + OpenInNewTab: l.OpenInNewTab, + } +} diff --git a/decoder/dashboardlink_test.go b/decoder/dashboardlink_test.go new file mode 100644 index 00000000..9156f841 --- /dev/null +++ b/decoder/dashboardlink_test.go @@ -0,0 +1,29 @@ +package decoder + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDashboardLink(t *testing.T) { + req := require.New(t) + + yamlLink := DashboardInternalLink{ + Title: "joe", + Tags: []string{"my-service"}, + AsDropdown: true, + IncludeTimeRange: true, + IncludeVariableValues: true, + OpenInNewTab: true, + } + + model := yamlLink.toModel() + + req.Equal("joe", model.Title) + req.ElementsMatch([]string{"my-service"}, model.Tags) + req.True(model.AsDropdown) + req.True(model.IncludeTimeRange) + req.True(model.IncludeVariableValues) + req.True(model.OpenInNewTab) +}