Skip to content

Commit

Permalink
Decode dashboard links
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Nov 7, 2021
1 parent e5e1ea0 commit 30de53e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
36 changes: 36 additions & 0 deletions internal/pkg/converter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (converter *JSON) parseInput(input io.Reader) (*grabana.DashboardModel, err
converter.convertGeneralSettings(board, dashboard)
converter.convertVariables(board.Templating.List, dashboard)
converter.convertAnnotations(board.Annotations.List, dashboard)
converter.convertExternalLinks(board.Links, dashboard)
converter.convertPanels(board.Panels, dashboard)

return dashboard, nil
Expand All @@ -143,6 +144,41 @@ func (converter *JSON) convertGeneralSettings(board *sdk.Board, dashboard *graba
}
}

func (converter *JSON) convertExternalLinks(links []sdk.Link, dashboard *grabana.DashboardModel) {
for _, link := range links {
if link.Type != "link" {
converter.logger.Warn("unhandled link type: skipped", zap.String("type", link.Type), zap.String("title", link.Title))
continue
}

if link.URL == nil || *link.URL == "" {
converter.logger.Warn("link URL empty: skipped", zap.String("title", link.Title))
continue
}

extLink := grabana.DashboardExternalLink{
Title: link.Title,
URL: *link.URL,
IncludeVariableValues: link.IncludeVars,
}

if link.Tooltip != nil && *link.Tooltip != "" {
extLink.Description = *link.Tooltip
}
if link.Icon != nil && *link.Icon != "" {
extLink.Icon = *link.Icon
}
if link.TargetBlank != nil {
extLink.OpenInNewTab = *link.TargetBlank
}
if link.KeepTime != nil {
extLink.IncludeTimeRange = *link.KeepTime
}

dashboard.ExternalLinks = append(dashboard.ExternalLinks, extLink)
}
}

func (converter *JSON) convertAnnotations(annotations []sdk.Annotation, dashboard *grabana.DashboardModel) {
for _, annotation := range annotations {
// grafana-sdk doesn't expose the "builtIn" field, so we work around that by skipping
Expand Down
39 changes: 39 additions & 0 deletions internal/pkg/converter/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,41 @@ func TestConvertQueryVar(t *testing.T) {
req.True(query.DefaultAll)
}

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

externalLink := sdk.Link{
Title: "joe",
Type: "link",
Icon: strPtr("cloud"),
IncludeVars: true,
KeepTime: boolPtr(true),
TargetBlank: boolPtr(true),
Tooltip: strPtr("description"),
URL: strPtr("http://lala"),
}
dashLink := sdk.Link{
Title: "not link",
}

converter := NewJSON(zap.NewNop())

dashboard := &grabana.DashboardModel{}
converter.convertExternalLinks([]sdk.Link{externalLink, dashLink}, dashboard)

req.Len(dashboard.ExternalLinks, 1)

link := dashboard.ExternalLinks[0]

req.Equal("joe", link.Title)
req.Equal("description", link.Description)
req.Equal("cloud", link.Icon)
req.Equal("http://lala", link.URL)
req.True(link.OpenInNewTab)
req.True(link.IncludeTimeRange)
req.True(link.IncludeVariableValues)
}

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

Expand Down Expand Up @@ -866,3 +901,7 @@ func TestConvertGraphOverridesWithOneOverride(t *testing.T) {
func strPtr(input string) *string {
return &input
}

func boolPtr(input bool) *bool {
return &input
}

0 comments on commit 30de53e

Please sign in to comment.