feat: add schema changes for a switch type of dashboard variable
CodeQL checks / Detect whether code changed (push) Has been cancelled Details
CodeQL checks / Analyze (actions) (push) Has been cancelled Details
CodeQL checks / Analyze (go) (push) Has been cancelled Details
CodeQL checks / Analyze (javascript) (push) Has been cancelled Details

This commit is contained in:
Levente Balogh 2025-10-06 14:25:53 +02:00
parent a6f92d4487
commit 8bb1f2c1ef
No known key found for this signature in database
10 changed files with 143 additions and 20 deletions

View File

@ -710,9 +710,9 @@ VariableCustomFormatterFn: {
// `custom`: Define the variable options manually using a comma-separated list.
// `system`: Variables defined by Grafana. See: https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
VariableType: "query" | "adhoc" | "groupby" | "constant" | "datasource" | "interval" | "textbox" | "custom" |
"system" | "snapshot"
"system" | "snapshot" | "switch"
VariableKind: QueryVariableKind | TextVariableKind | ConstantVariableKind | DatasourceVariableKind | IntervalVariableKind | CustomVariableKind | GroupByVariableKind | AdhocVariableKind
VariableKind: QueryVariableKind | TextVariableKind | ConstantVariableKind | DatasourceVariableKind | IntervalVariableKind | CustomVariableKind | GroupByVariableKind | AdhocVariableKind | SwitchVariableKind
// Sort variable options
// Accepted values are:
@ -904,6 +904,22 @@ CustomVariableKind: {
spec: CustomVariableSpec
}
SwitchVariableSpec: {
name: string | *""
current: string | *"false"
enabledValue: string | *"true"
disabledValue: string | *"false"
label?: string
hide: VariableHide
skipUrlSync: bool | *false
description?: string
}
SwitchVariableKind: {
kind: "SwitchVariable"
spec: SwitchVariableSpec
}
// GroupBy variable specification
GroupByVariableSpec: {
name: string | *""

View File

@ -364,8 +364,9 @@ lineage: schemas: [{
// `textbox`: Display a free text input field with an optional default value.
// `custom`: Define the variable options manually using a comma-separated list.
// `system`: Variables defined by Grafana. See: https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
// `switch`: Boolean variables rendered as a switch
#VariableType: "query" | "adhoc" | "groupby" | "constant" | "datasource" | "interval" | "textbox" | "custom" |
"system" | "snapshot" @cuetsy(kind="type") @grafanamaturity(NeedsExpertReview)
"system" | "snapshot" | "switch" @cuetsy(kind="type") @grafanamaturity(NeedsExpertReview)
// Color mode for a field. You can specify a single color, or select a continuous (gradient) color schemes, based on a value.
// Continuous color interpolates a color using the percentage of a value relative to min and max.

View File

@ -364,8 +364,9 @@ lineage: schemas: [{
// `textbox`: Display a free text input field with an optional default value.
// `custom`: Define the variable options manually using a comma-separated list.
// `system`: Variables defined by Grafana. See: https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
// `switch`: Boolean variables rendered as a switch
#VariableType: "query" | "adhoc" | "groupby" | "constant" | "datasource" | "interval" | "textbox" | "custom" |
"system" | "snapshot" @cuetsy(kind="type") @grafanamaturity(NeedsExpertReview)
"system" | "snapshot" | "switch" @cuetsy(kind="type") @grafanamaturity(NeedsExpertReview)
// Color mode for a field. You can specify a single color, or select a continuous (gradient) color schemes, based on a value.
// Continuous color interpolates a color using the percentage of a value relative to min and max.

View File

@ -714,9 +714,9 @@ VariableCustomFormatterFn: {
// `custom`: Define the variable options manually using a comma-separated list.
// `system`: Variables defined by Grafana. See: https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
VariableType: "query" | "adhoc" | "groupby" | "constant" | "datasource" | "interval" | "textbox" | "custom" |
"system" | "snapshot"
"system" | "snapshot" | "switch"
VariableKind: QueryVariableKind | TextVariableKind | ConstantVariableKind | DatasourceVariableKind | IntervalVariableKind | CustomVariableKind | GroupByVariableKind | AdhocVariableKind
VariableKind: QueryVariableKind | TextVariableKind | ConstantVariableKind | DatasourceVariableKind | IntervalVariableKind | CustomVariableKind | GroupByVariableKind | AdhocVariableKind | SwitchVariableKind
// Sort variable options
// Accepted values are:
@ -908,6 +908,22 @@ CustomVariableKind: {
spec: CustomVariableSpec
}
SwitchVariableSpec: {
name: string | *""
current: string | *"false"
enabledValue: string | *"true"
disabledValue: string | *"false"
label?: string
hide: VariableHide
skipUrlSync: bool | *false
description?: string
}
SwitchVariableKind: {
kind: "SwitchVariable"
spec: SwitchVariableSpec
}
// GroupBy variable specification
GroupByVariableSpec: {
name: string | *""

View File

@ -1290,11 +1290,11 @@ func NewDashboardTimeRangeOption() *DashboardTimeRangeOption {
}
// +k8s:openapi-gen=true
type DashboardVariableKind = DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind
type DashboardVariableKind = DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind
// NewDashboardVariableKind creates a new DashboardVariableKind object.
func NewDashboardVariableKind() *DashboardVariableKind {
return NewDashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind()
return NewDashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind()
}
// Query variable kind
@ -1815,6 +1815,44 @@ func NewDashboardMetricFindValue() *DashboardMetricFindValue {
return &DashboardMetricFindValue{}
}
// +k8s:openapi-gen=true
type DashboardSwitchVariableKind struct {
Kind string `json:"kind"`
Spec DashboardSwitchVariableSpec `json:"spec"`
}
// NewDashboardSwitchVariableKind creates a new DashboardSwitchVariableKind object.
func NewDashboardSwitchVariableKind() *DashboardSwitchVariableKind {
return &DashboardSwitchVariableKind{
Kind: "SwitchVariable",
Spec: *NewDashboardSwitchVariableSpec(),
}
}
// +k8s:openapi-gen=true
type DashboardSwitchVariableSpec struct {
Name string `json:"name"`
Current string `json:"current"`
EnabledValue string `json:"enabledValue"`
DisabledValue string `json:"disabledValue"`
Label *string `json:"label,omitempty"`
Hide DashboardVariableHide `json:"hide"`
SkipUrlSync bool `json:"skipUrlSync"`
Description *string `json:"description,omitempty"`
}
// NewDashboardSwitchVariableSpec creates a new DashboardSwitchVariableSpec object.
func NewDashboardSwitchVariableSpec() *DashboardSwitchVariableSpec {
return &DashboardSwitchVariableSpec{
Name: "",
Current: "false",
EnabledValue: "true",
DisabledValue: "false",
Hide: DashboardVariableHideDontHide,
SkipUrlSync: false,
}
}
// +k8s:openapi-gen=true
type DashboardSpec struct {
Annotations []DashboardAnnotationQueryKind `json:"annotations"`
@ -2433,7 +2471,7 @@ func (resource *DashboardGridLayoutKindOrRowsLayoutKindOrAutoGridLayoutKindOrTab
}
// +k8s:openapi-gen=true
type DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind struct {
type DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind struct {
QueryVariableKind *DashboardQueryVariableKind `json:"QueryVariableKind,omitempty"`
TextVariableKind *DashboardTextVariableKind `json:"TextVariableKind,omitempty"`
ConstantVariableKind *DashboardConstantVariableKind `json:"ConstantVariableKind,omitempty"`
@ -2442,15 +2480,16 @@ type DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasou
CustomVariableKind *DashboardCustomVariableKind `json:"CustomVariableKind,omitempty"`
GroupByVariableKind *DashboardGroupByVariableKind `json:"GroupByVariableKind,omitempty"`
AdhocVariableKind *DashboardAdhocVariableKind `json:"AdhocVariableKind,omitempty"`
SwitchVariableKind *DashboardSwitchVariableKind `json:"SwitchVariableKind,omitempty"`
}
// NewDashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind creates a new DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind object.
func NewDashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind() *DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind {
return &DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind{}
// NewDashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind creates a new DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind object.
func NewDashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind() *DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind {
return &DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind{}
}
// MarshalJSON implements a custom JSON marshalling logic to encode `DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind` as JSON.
func (resource DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind) MarshalJSON() ([]byte, error) {
// MarshalJSON implements a custom JSON marshalling logic to encode `DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind` as JSON.
func (resource DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind) MarshalJSON() ([]byte, error) {
if resource.QueryVariableKind != nil {
return json.Marshal(resource.QueryVariableKind)
}
@ -2475,12 +2514,15 @@ func (resource DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKin
if resource.AdhocVariableKind != nil {
return json.Marshal(resource.AdhocVariableKind)
}
if resource.SwitchVariableKind != nil {
return json.Marshal(resource.SwitchVariableKind)
}
return []byte("null"), nil
}
// UnmarshalJSON implements a custom JSON unmarshalling logic to decode `DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind` from JSON.
func (resource *DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKind) UnmarshalJSON(raw []byte) error {
// UnmarshalJSON implements a custom JSON unmarshalling logic to decode `DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind` from JSON.
func (resource *DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKindOrDatasourceVariableKindOrIntervalVariableKindOrCustomVariableKindOrGroupByVariableKindOrAdhocVariableKindOrSwitchVariableKind) UnmarshalJSON(raw []byte) error {
if raw == nil {
return nil
}
@ -2553,6 +2595,14 @@ func (resource *DashboardQueryVariableKindOrTextVariableKindOrConstantVariableKi
resource.QueryVariableKind = &dashboardQueryVariableKind
return nil
case "SwitchVariable":
var dashboardSwitchVariableKind DashboardSwitchVariableKind
if err := json.Unmarshal(raw, &dashboardSwitchVariableKind); err != nil {
return err
}
resource.SwitchVariableKind = &dashboardSwitchVariableKind
return nil
case "TextVariable":
var dashboardTextVariableKind DashboardTextVariableKind
if err := json.Unmarshal(raw, &dashboardTextVariableKind); err != nil {

View File

@ -360,8 +360,9 @@ lineage: schemas: [{
// `textbox`: Display a free text input field with an optional default value.
// `custom`: Define the variable options manually using a comma-separated list.
// `system`: Variables defined by Grafana. See: https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
// `switch`: Boolean variables rendered as a switch
#VariableType: "query" | "adhoc" | "groupby" | "constant" | "datasource" | "interval" | "textbox" | "custom" |
"system" | "snapshot" @cuetsy(kind="type") @grafanamaturity(NeedsExpertReview)
"system" | "snapshot" | "switch" @cuetsy(kind="type") @grafanamaturity(NeedsExpertReview)
// Color mode for a field. You can specify a single color, or select a continuous (gradient) color schemes, based on a value.
// Continuous color interpolates a color using the percentage of a value relative to min and max.

View File

@ -23,7 +23,8 @@ export type TypedVariableModel =
| UserVariableModel
| OrgVariableModel
| DashboardVariableModel
| SnapshotVariableModel;
| SnapshotVariableModel
| SwitchVariableModel;
export enum VariableRefresh {
never, // removed from the UI
@ -130,6 +131,10 @@ export interface ConstantVariableModel extends VariableWithOptions {
type: 'constant';
}
export interface SwitchVariableModel extends VariableWithOptions {
type: 'switch';
}
export interface VariableWithMultiSupport extends VariableWithOptions {
multi: boolean;
includeAll: boolean;

View File

@ -454,8 +454,9 @@ export const defaultAction: Partial<Action> = {
* `textbox`: Display a free text input field with an optional default value.
* `custom`: Define the variable options manually using a comma-separated list.
* `system`: Variables defined by Grafana. See: https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
* `switch`: Boolean variables rendered as a switch
*/
export type VariableType = ('query' | 'adhoc' | 'groupby' | 'constant' | 'datasource' | 'interval' | 'textbox' | 'custom' | 'system' | 'snapshot');
export type VariableType = ('query' | 'adhoc' | 'groupby' | 'constant' | 'datasource' | 'interval' | 'textbox' | 'custom' | 'system' | 'snapshot' | 'switch');
/**
* Color mode for a field. You can specify a single color, or select a continuous (gradient) color schemes, based on a value.

View File

@ -1052,7 +1052,7 @@ export const defaultTimeRangeOption = (): TimeRangeOption => ({
to: "now",
});
export type VariableKind = QueryVariableKind | TextVariableKind | ConstantVariableKind | DatasourceVariableKind | IntervalVariableKind | CustomVariableKind | GroupByVariableKind | AdhocVariableKind;
export type VariableKind = QueryVariableKind | TextVariableKind | ConstantVariableKind | DatasourceVariableKind | IntervalVariableKind | CustomVariableKind | GroupByVariableKind | AdhocVariableKind | SwitchVariableKind;
export const defaultVariableKind = (): VariableKind => (defaultQueryVariableKind());
@ -1444,6 +1444,36 @@ export const defaultMetricFindValue = (): MetricFindValue => ({
text: "",
});
export interface SwitchVariableKind {
kind: "SwitchVariable";
spec: SwitchVariableSpec;
}
export const defaultSwitchVariableKind = (): SwitchVariableKind => ({
kind: "SwitchVariable",
spec: defaultSwitchVariableSpec(),
});
export interface SwitchVariableSpec {
name: string;
current: string;
enabledValue: string;
disabledValue: string;
label?: string;
hide: VariableHide;
skipUrlSync: boolean;
description?: string;
}
export const defaultSwitchVariableSpec = (): SwitchVariableSpec => ({
name: "",
current: "false",
enabledValue: "true",
disabledValue: "false",
hide: "dontHide",
skipUrlSync: false,
});
export interface Spec {
annotations: AnnotationQueryKind[];
// Configuration of dashboard cursor sync behavior.

View File

@ -845,6 +845,7 @@ func NewVariableModel() *VariableModel {
// `textbox`: Display a free text input field with an optional default value.
// `custom`: Define the variable options manually using a comma-separated list.
// `system`: Variables defined by Grafana. See: https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables
// `switch`: Boolean variables rendered as a switch
type VariableType string
const (
@ -858,6 +859,7 @@ const (
VariableTypeCustom VariableType = "custom"
VariableTypeSystem VariableType = "system"
VariableTypeSnapshot VariableType = "snapshot"
VariableTypeSwitch VariableType = "switch"
)
// Determine if the variable shows on dashboard