diff --git a/dashboard/dashboard.go b/dashboard/dashboard.go index 58eb3acb..39966b09 100644 --- a/dashboard/dashboard.go +++ b/dashboard/dashboard.go @@ -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" ) @@ -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 { diff --git a/dashboard/dashboard_test.go b/dashboard/dashboard_test.go index 406b1fb5..71bb8a54 100644 --- a/dashboard/dashboard_test.go +++ b/dashboard/dashboard_test.go @@ -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) { @@ -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) diff --git a/variable/text/text.go b/variable/text/text.go new file mode 100644 index 00000000..09462f1d --- /dev/null +++ b/variable/text/text.go @@ -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 + } +} diff --git a/variable/text/text_test.go b/variable/text/text_test.go new file mode 100644 index 00000000..261a5408 --- /dev/null +++ b/variable/text/text_test.go @@ -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) +}