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

fix composite alert crash #226

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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.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
Loading