Skip to content

Commit

Permalink
Merge pull request #215 from K-Phoen/dashboards-links
Browse files Browse the repository at this point in the history
Dashboards links
  • Loading branch information
K-Phoen authored Mar 12, 2023
2 parents 709e039 + 818cff3 commit 4a55299
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 3 deletions.
16 changes: 14 additions & 2 deletions dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ func VariableAsDatasource(name string, options ...datasource.Option) Option {
}
}

// ExternalLinks adds a dashboard-level link.
// See https://grafana.com/docs/grafana/latest/linking/dashboard-links/
// ExternalLinks adds a dashboard-level external links.
// See https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/manage-dashboard-links/#add-a-url-link-to-a-dashboard
func ExternalLinks(links ...ExternalLink) Option {
return func(builder *Builder) error {
for _, link := range links {
Expand All @@ -225,6 +225,18 @@ func ExternalLinks(links ...ExternalLink) Option {
}
}

// DashboardLinks adds a dashboard-level links to other dashboards.
// See https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/manage-dashboard-links/#dashboard-links
func DashboardLinks(links ...DashboardLink) Option {
return func(builder *Builder) error {
for _, link := range links {
builder.board.Links = append(builder.board.Links, link.asSdk())
}

return nil
}
}

// Row adds a row to the dashboard.
func Row(title string, options ...row.Option) Option {
return func(builder *Builder) error {
Expand Down
9 changes: 9 additions & 0 deletions dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,12 @@ func TestDashboardCanHaveExternalLinks(t *testing.T) {
req.NoError(err)
req.Len(panel.board.Links, 2)
}

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

panel, err := New("", DashboardLinks(DashboardLink{}, DashboardLink{}))

req.NoError(err)
req.Len(panel.board.Links, 2)
}
26 changes: 25 additions & 1 deletion dashboard/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

// ExternalLink describes dashboard-level external link.
// See https://grafana.com/docs/grafana/latest/linking/dashboard-links/
// See https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/manage-dashboard-links/#add-a-url-link-to-a-dashboard
type ExternalLink struct {
Title string
Description string
Expand Down Expand Up @@ -46,3 +46,27 @@ func (link ExternalLink) asSdk() sdk.Link {
Type: "link",
}
}

// DashboardLink describes dashboard-level links to other dashboards.
// See https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/manage-dashboard-links/#dashboard-links
type DashboardLink struct {
Title string
Tags []string

AsDropdown bool
IncludeTimeRange bool
IncludeVariableValues bool
OpenInNewTab bool
}

func (link DashboardLink) asSdk() sdk.Link {
return sdk.Link{
Title: link.Title,
Tags: link.Tags,
AsDropdown: &link.AsDropdown,
IncludeVars: link.IncludeVariableValues,
KeepTime: &link.IncludeTimeRange,
TargetBlank: &link.OpenInNewTab,
Type: "dashboards",
}
}
22 changes: 22 additions & 0 deletions dashboard/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ func TestExternalLinkIconHasDefault(t *testing.T) {

req.Equal("external", *sdkLink.Icon)
}

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

link := DashboardLink{
Title: "my link",
Tags: []string{"my-service"},
AsDropdown: true,
IncludeTimeRange: true,
IncludeVariableValues: true,
OpenInNewTab: true,
}
sdkLink := link.asSdk()

req.Equal("my link", sdkLink.Title)
req.ElementsMatch([]string{"my-service"}, link.Tags)
req.True(*sdkLink.AsDropdown)
req.True(sdkLink.IncludeVars)
req.True(*sdkLink.KeepTime)
req.True(*sdkLink.TargetBlank)
req.Equal("dashboards", sdkLink.Type)
}
5 changes: 5 additions & 0 deletions decoder/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
}
Expand Down
25 changes: 25 additions & 0 deletions decoder/dashboardlink.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
29 changes: 29 additions & 0 deletions decoder/dashboardlink_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4a55299

Please sign in to comment.