Skip to content

Commit

Permalink
fix composite alert crash (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterSquishy authored May 28, 2024
1 parent 7c38f79 commit a769a12
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.95.3
1.95.4
75 changes: 75 additions & 0 deletions lightstep/resource_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,3 +1312,78 @@ resource "lightstep_alert" "test" {
},
})
}

func TestCompositeCrash(t *testing.T) {
var condition client.UnifiedCondition

conditionConfig := `
resource "lightstep_alert" "test" {
name = "test"
project_name = "` + testProject + `"
composite_alert {
alert {
name = "A"
title = "cpu"
query {
hidden = false
query_name = "a"
query_string = "metric a | delta 30s | group_by[], sum"
display = "line"
hidden_queries = { "a" = "%s" }
}
expression {
operand = "above"
thresholds {
critical = 20
}
}
}
alert {
name = "B"
title = "b"
query {
hidden = false
query_name = "a"
query_string = "metric b | delta 30s | group_by[], sum"
display = "line"
}
expression {
operand = "above"
thresholds {
critical = 20
}
}
}
}
}
`

resourceName := "lightstep_alert.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccMetricConditionDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(conditionConfig, "false"),
Check: resource.ComposeTestCheckFunc(
testAccCheckLightstepAlertExists(resourceName, &condition),
),
},
{
Config: fmt.Sprintf(conditionConfig, "true"),
Check: resource.ComposeTestCheckFunc(
testAccCheckLightstepAlertExists(resourceName, &condition),
),
},
},
})
}
9 changes: 7 additions & 2 deletions lightstep/resource_metric_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,8 +1105,13 @@ func buildCompositeAlert(d *schema.ResourceData) (*client.CompositeAlert, error)
if !ok {
return nil, fmt.Errorf("could not parse alert")
}

subAlertExpression, err := buildSubAlertExpression(subAlertIn["expression"].([]interface{})[0].(map[string]interface{}))
subAlertExpressionsUntyped := subAlertIn["expression"].([]interface{})
if len(subAlertExpressionsUntyped) == 0 {
// if a subalert is being dropped and recreated, we will get a zero-value
// subalert in the list. ignore it.
continue
}
subAlertExpression, err := buildSubAlertExpression(subAlertExpressionsUntyped[0].(map[string]interface{}))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit a769a12

Please sign in to comment.