Skip to content

Commit

Permalink
LS-61620: add dashbaord chart thresholds to provider (#231)
Browse files Browse the repository at this point in the history
* support thresholds in dashboard chart

* actually add tresholds to resource and fix tests

* add updated config test

* update version

* added terraform docs

* remove superfluous color and operator check

* update version

* remove color validation

* replace label point with string and remove short circuit

---------

Co-authored-by: srjames90 <[email protected]>
  • Loading branch information
srjames90 and srjames90 authored Jun 24, 2024
1 parent 35ffff7 commit 5652a21
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.95.8
1.96.0
10 changes: 10 additions & 0 deletions client/metric_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type UnifiedChart struct {
YAxis *YAxis `json:"y-axis"`
MetricQueries []MetricQueryWithAttributes `json:"metric-queries"`
Text string `json:"text"`
Thresholds []Threshold `json:"thresholds"`
Subtitle *string `json:"subtitle,omitempty"`
}

Expand All @@ -67,6 +68,15 @@ type Panel struct {
Body map[string]any `json:"body"`
}

type Threshold struct {
// enum: E,GE,GT,LE,LT
Operator string `json:"operator"`
Value float64 `json:"value"`
// An alpha hex color
Color string `json:"color"`
Label string `json:"label"`
}

type YAxis struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
Expand Down
30 changes: 30 additions & 0 deletions docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Optional:
- `description` (String)
- `height` (Number)
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--chart--threshold))
- `width` (Number)
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--chart--y_axis))
Expand Down Expand Up @@ -143,6 +144,20 @@ Optional:



<a id="nestedblock--chart--threshold"></a>
### Nested Schema for `chart.threshold`

Required:

- `color` (String)
- `operator` (String)
- `value` (Number)

Optional:

- `label` (String)


<a id="nestedblock--chart--y_axis"></a>
### Nested Schema for `chart.y_axis`

Expand Down Expand Up @@ -244,6 +259,7 @@ Optional:
- `description` (String)
- `height` (Number)
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--group--chart--threshold))
- `width` (Number)
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--group--chart--y_axis))
Expand Down Expand Up @@ -295,6 +311,20 @@ Optional:



<a id="nestedblock--group--chart--threshold"></a>
### Nested Schema for `group.chart.threshold`

Required:

- `color` (String)
- `operator` (String)
- `value` (Number)

Optional:

- `label` (String)


<a id="nestedblock--group--chart--y_axis"></a>
### Nested Schema for `group.chart.y_axis`

Expand Down
30 changes: 30 additions & 0 deletions docs/resources/metric_dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Optional:
- `description` (String)
- `height` (Number)
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--chart--threshold))
- `width` (Number)
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--chart--y_axis))
Expand Down Expand Up @@ -180,6 +181,20 @@ Optional:



<a id="nestedblock--chart--threshold"></a>
### Nested Schema for `chart.threshold`

Required:

- `color` (String)
- `operator` (String)
- `value` (Number)

Optional:

- `label` (String)


<a id="nestedblock--chart--y_axis"></a>
### Nested Schema for `chart.y_axis`

Expand Down Expand Up @@ -281,6 +296,7 @@ Optional:
- `description` (String)
- `height` (Number)
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--group--chart--threshold))
- `width` (Number)
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--group--chart--y_axis))
Expand Down Expand Up @@ -346,6 +362,20 @@ Optional:



<a id="nestedblock--group--chart--threshold"></a>
### Nested Schema for `group.chart.threshold`

Required:

- `color` (String)
- `operator` (String)
- `value` (Number)

Optional:

- `label` (String)


<a id="nestedblock--group--chart--y_axis"></a>
### Nested Schema for `group.chart.y_axis`

Expand Down
28 changes: 28 additions & 0 deletions lightstep/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func getThresholdSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"color": {
Type: schema.TypeString,
Required: true,
},
"label": {
Type: schema.TypeString,
Optional: true,
},
"operator": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"E",
"GE",
"GT",
"LE",
"LT",
}, false),
},
"value": {
Type: schema.TypeFloat,
Required: true,
},
}
}

func getUnifiedQuerySchemaMap() map[string]*schema.Schema {
sma := map[string]*schema.Schema{
"hidden": {
Expand Down
63 changes: 63 additions & 0 deletions lightstep/resource_metric_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ func getChartSchema(chartSchemaType ChartSchemaType) map[string]*schema.Schema {
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 256),
},
"threshold": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: getThresholdSchema(),
},
},
},
)
}
Expand Down Expand Up @@ -580,6 +587,11 @@ func buildCharts(chartsIn []interface{}) ([]client.UnifiedChart, error) {
return nil, err
}
c.MetricQueries = queries
thresholds, err := buildChartThresholds(chart["threshold"].([]interface{}))
if err != nil {
return nil, err
}
c.Thresholds = thresholds

yaxis, err := buildYAxis(chart["y_axis"].([]interface{}))
if err != nil {
Expand All @@ -599,6 +611,42 @@ func buildCharts(chartsIn []interface{}) ([]client.UnifiedChart, error) {
return newCharts, nil
}

func buildChartThresholds(thresholdsIn []interface{}) ([]client.Threshold, error) {
var thresholds []client.Threshold
if len(thresholdsIn) < 1 {
return thresholds, nil
}
for _, t := range thresholdsIn {
threshold := t.(map[string]interface{})
color, ok := threshold["color"].(string)

if !ok {
return []client.Threshold{}, fmt.Errorf("missing required attribute 'color' for threshold")
}

operator, ok := threshold["operator"].(string)
if !ok {
return []client.Threshold{}, fmt.Errorf("missing required attribute 'operator' for threshold")
}

label := threshold["label"].(string)

value, ok := threshold["value"].(float64)

if !ok {
return []client.Threshold{}, fmt.Errorf("missing required attribute 'value' for threshold")
}

thresholds = append(thresholds, client.Threshold{
Color: color,
Label: label,
Operator: operator,
Value: value,
})
}
return thresholds, nil
}

func buildYAxis(yAxisIn []interface{}) (*client.YAxis, error) {
if len(yAxisIn) < 1 {
return nil, nil
Expand Down Expand Up @@ -845,11 +893,26 @@ func assembleCharts(
resource["query"] = queries
}

resource["threshold"] = getThresholdsFromResourceData(c.Thresholds)

chartResources = append(chartResources, resource)
}
return chartResources, nil
}

func getThresholdsFromResourceData(thresholdsIn []client.Threshold) []interface{} {
var thresholds []interface{}
for _, t := range thresholdsIn {
thresholds = append(thresholds, map[string]interface{}{
"color": t.Color,
"label": t.Label,
"operator": t.Operator,
"value": t.Value,
})
}
return thresholds
}

// isLegacyImplicitGroup defines the logic for determining if the charts in this dashboard need to be unwrapped to
// maintain backwards compatibility with the pre group definition
func isLegacyImplicitGroup(groups []client.UnifiedGroup, hasLegacyChartsIn bool) bool {
Expand Down
Loading

0 comments on commit 5652a21

Please sign in to comment.