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 4 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
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
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
133 changes: 133 additions & 0 deletions lightstep/resource_metric_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,139 @@ resource "lightstep_metric_dashboard" "test" {
})
}

func TestAccDashboardChartThresholds(t *testing.T) {
var dashboard client.UnifiedDashboard

dashboardConfig := `
resource "lightstep_metric_dashboard" "test" {
project_name = "` + testProject + `"
dashboard_name = "Acceptance Test Dashboard (TestAccDashboardChartThresholds)"
dashboard_description = "Dashboard to test thresholds in charts"

group {
rank = 0
visibility_type = "implicit"

chart {
name = "cpu"
rank = 1
type = "timeseries"

query {
display = "line"
hidden = false
query_name = "a"
tql = "metric cpu.utilization | latest | group_by [], sum"
}

threshold {
color = "#AA3018"
label = "critical"
operator = "GT"
value = 99
}

threshold {
color = "#6699CC"
operator = "GT"
value = 199
}
}
}
}
`
updatedDashboardConfig := `
resource "lightstep_metric_dashboard" "test" {
project_name = "` + testProject + `"
dashboard_name = "Acceptance Test Dashboard (TestAccDashboardChartThresholds)"
dashboard_description = "Dashboard to test thresholds in charts"

group {
rank = 0
visibility_type = "implicit"

chart {
name = "cpu"
rank = 1
type = "timeseries"

query {
display = "line"
hidden = false
query_name = "a"
tql = "metric cpu.utilization | latest | group_by [], sum"
}

threshold {
color = "#AA3018"
label = "critical"
operator = "GT"
value = 99
}

threshold {
color = "#6699CC"
label = "extra critical"
operator = "GT"
value = 199
}
}
}
}
`
resourceName := "lightstep_metric_dashboard.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testGetMetricDashboardDestroy,
Steps: []resource.TestStep{
{
// Create the initial dashboard with a chart and make sure it has thresholds
Config: dashboardConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckMetricDashboardExists(resourceName, &dashboard),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.name", "cpu"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.#", "2"),

// First threshold in chart
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.color", "#AA3018"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.label", "critical"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.operator", "GT"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.value", "99"),

// Second threshold in chart
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.color", "#6699CC"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.operator", "GT"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.value", "199"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.label", ""),
),
},
{
// Updated config will contain the a label for the second threshold
Config: updatedDashboardConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckMetricDashboardExists(resourceName, &dashboard),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.name", "cpu"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.#", "2"),

// First threshold in chart
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.color", "#AA3018"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.label", "critical"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.operator", "GT"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.0.value", "99"),

// Second threshold in chart
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.color", "#6699CC"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.operator", "GT"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.value", "199"),
resource.TestCheckResourceAttr(resourceName, "group.0.chart.0.threshold.1.label", "extra critical"),
),
},
},
})
}

func TestAccDashboardEventQueries(t *testing.T) {
var dashboard client.UnifiedDashboard
var eventQuery client.EventQueryAttributes
Expand Down
Loading