Skip to content

Commit

Permalink
Support defining "textbox" dashboard variables in Go
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Mar 21, 2023
1 parent 5dcfa2c commit 82d0161
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
14 changes: 13 additions & 1 deletion dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"encoding/json"

"github.com/K-Phoen/grabana/alert"

"github.com/K-Phoen/grabana/row"
"github.com/K-Phoen/grabana/variable/constant"
"github.com/K-Phoen/grabana/variable/custom"
"github.com/K-Phoen/grabana/variable/datasource"
"github.com/K-Phoen/grabana/variable/interval"
"github.com/K-Phoen/grabana/variable/query"
"github.com/K-Phoen/grabana/variable/text"
"github.com/K-Phoen/sdk"
)

Expand Down Expand Up @@ -213,6 +213,18 @@ func VariableAsDatasource(name string, options ...datasource.Option) Option {
}
}

// VariableAsText adds a templated variable, defined as a free text input.
// See https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#add-a-text-box-variable
func VariableAsText(name string, options ...text.Option) Option {
return func(builder *Builder) error {
templatedVar := text.New(name, options...)

builder.board.Templating.List = append(builder.board.Templating.List, templatedVar.Builder)

return nil
}
}

// 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 {
Expand Down
13 changes: 11 additions & 2 deletions dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

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

func requireJSON(t *testing.T, payload []byte) {
Expand Down Expand Up @@ -215,6 +215,15 @@ func TestDashboardCanHaveVariablesAsDatasource(t *testing.T) {
req.Len(panel.board.Templating.List, 1)
}

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

panel, err := New("", VariableAsText("filter", text.Label("Filter")))

req.NoError(err)
req.Len(panel.board.Templating.List, 1)
}

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

Expand Down
50 changes: 50 additions & 0 deletions variable/text/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package text

import (
"github.com/K-Phoen/sdk"
)

// Option represents an option that can be used to configure a textbox variable.
type Option func(constant *Text)

// Text represents a "textbox" templated variable.
type Text struct {
Builder sdk.TemplateVar
}

// New creates a new "query" templated variable.
func New(name string, options ...Option) *Text {
query := &Text{Builder: sdk.TemplateVar{
Name: name,
Label: name,
Type: "textbox",
Options: []sdk.Option{},
}}

for _, opt := range options {
opt(query)
}

return query
}

// Label sets the label of the variable.
func Label(label string) Option {
return func(query *Text) {
query.Builder.Label = label
}
}

// HideLabel ensures that this variable's label will not be displayed.
func HideLabel() Option {
return func(query *Text) {
query.Builder.Hide = 1
}
}

// Hide ensures that the variable will not be displayed.
func Hide() Option {
return func(query *Text) {
query.Builder.Hide = 2
}
}
42 changes: 42 additions & 0 deletions variable/text/text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package text

import (
"testing"

"github.com/stretchr/testify/require"
)

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

panel := New("filter")

req.Equal("filter", panel.Builder.Name)
req.Equal("filter", panel.Builder.Label)
req.Equal("textbox", panel.Builder.Type)
}

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

panel := New("filter", Label("Filter"))

req.Equal("filter", panel.Builder.Name)
req.Equal("Filter", panel.Builder.Label)
}

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

panel := New("", HideLabel())

req.Equal(uint8(1), panel.Builder.Hide)
}

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

panel := New("", Hide())

req.Equal(uint8(2), panel.Builder.Hide)
}

0 comments on commit 82d0161

Please sign in to comment.