Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LS-61620: add dashbaord chart thresholds to provider #231

Merged
merged 9 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.95.8
1.95.9
srjames90 marked this conversation as resolved.
Show resolved Hide resolved
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"`
srjames90 marked this conversation as resolved.
Show resolved Hide resolved
}

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
43 changes: 43 additions & 0 deletions lightstep/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,49 @@ 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,
ValidateFunc: validation.StringInSlice([]string{
"#AA3018",
"#B67D0C",
"#3C864F",
"#1B7BBB",
"#DC7847",
"#78469B",
"#37A2AE",
"#B03B7F",
"#1F40C1",
"#8B7255",
"#826CEF",
"#D56DD5",
"#6699CC",
}, false),
srjames90 marked this conversation as resolved.
Show resolved Hide resolved
},
"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
99 changes: 99 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,78 @@ func buildCharts(chartsIn []interface{}) ([]client.UnifiedChart, error) {
return newCharts, nil
}

var colors = map[string]bool{
"#AA3018": true,
"#B67D0C": true,
"#3C864F": true,
"#1B7BBB": true,
"#DC7847": true,
"#78469B": true,
"#37A2AE": true,
"#B03B7F": true,
"#1F40C1": true,
"#8B7255": true,
"#826CEF": true,
"#D56DD5": true,
"#6699CC": true,
}

var operators = map[string]bool{
"E": true,
"GE": true,
"GT": true,
"LE": true,
"LT": true,
}
srjames90 marked this conversation as resolved.
Show resolved Hide resolved

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")
}

if !colors[color] {
return []client.Threshold{}, fmt.Errorf("invalid value for attribute 'color' for threshold")
}
srjames90 marked this conversation as resolved.
Show resolved Hide resolved

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

if !operators[operator] {
return []client.Threshold{}, fmt.Errorf("invalid value for attribute 'operator' for threshold")
}
srjames90 marked this conversation as resolved.
Show resolved Hide resolved

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

if !ok {
return []client.Threshold{}, fmt.Errorf("missing required attribute 'label' for threshold")
srjames90 marked this conversation as resolved.
Show resolved Hide resolved
}

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 +929,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