Skip to content

Commit

Permalink
Support defining "textbox" dashboard variables in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Mar 21, 2023
1 parent 82d0161 commit aae4483
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions decoder/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"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"
)

var ErrVariableNotConfigured = fmt.Errorf("variable not configured")
Expand All @@ -20,6 +21,7 @@ type DashboardVariable struct {
Query *VariableQuery `yaml:",omitempty"`
Const *VariableConst `yaml:",omitempty"`
Datasource *VariableDatasource `yaml:",omitempty"`
Text *VariableText `yaml:",omitempty"`
}

func (variable *DashboardVariable) toOption() (dashboard.Option, error) {
Expand All @@ -38,6 +40,9 @@ func (variable *DashboardVariable) toOption() (dashboard.Option, error) {
if variable.Datasource != nil {
return variable.Datasource.toOption()
}
if variable.Text != nil {
return variable.Text.toOption()
}

return nil, ErrVariableNotConfigured
}
Expand Down Expand Up @@ -260,3 +265,31 @@ func (variable *VariableDatasource) toOption() (dashboard.Option, error) {

return dashboard.VariableAsDatasource(variable.Name, opts...), nil
}

type VariableText struct {
Name string
Label string
Hide string `yaml:",omitempty"`
}

func (variable *VariableText) toOption() (dashboard.Option, error) {
var opts []text.Option

if variable.Label != "" {
opts = append(opts, text.Label(variable.Label))
}

switch variable.Hide {
case "":
// Nothing to do
break
case "label":
opts = append(opts, text.HideLabel())
case "variable":
opts = append(opts, text.Hide())
default:
return dashboard.VariableAsQuery(variable.Name), ErrInvalidHideValue
}

return dashboard.VariableAsText(variable.Name, opts...), nil
}

0 comments on commit aae4483

Please sign in to comment.