Skip to content

Commit

Permalink
Merge pull request #887 from newrelic/issue882
Browse files Browse the repository at this point in the history
fix(alerts): avoid bad index reference
  • Loading branch information
zlesnr authored Sep 17, 2020
2 parents cea00d5 + 90f933a commit 4160949
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
20 changes: 14 additions & 6 deletions newrelic/structures_newrelic_nrql_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,16 +530,24 @@ func flattenNrqlTerms(terms []alerts.NrqlConditionTerm, configTerms []interface{
"threshold": term.Threshold,
}

setDuration := configuredTerms[i]["duration"]
if setDuration != nil && setDuration.(int) > 0 {
dst["duration"] = term.ThresholdDuration / 60 // convert to minutes for old way
if i < len(configuredTerms) {
setDuration := configuredTerms[i]["duration"]
if setDuration != nil && setDuration.(int) > 0 {
dst["duration"] = term.ThresholdDuration / 60 // convert to minutes for old way
} else {
dst["threshold_duration"] = term.ThresholdDuration
}
} else {
dst["threshold_duration"] = term.ThresholdDuration
}

setTimeFunction := configuredTerms[i]["time_function"]
if setTimeFunction != nil && setTimeFunction.(string) != "" {
dst["time_function"] = timeFunctionMapNewOld[term.ThresholdOccurrences]
if i < len(configuredTerms) {
setTimeFunction := configuredTerms[i]["time_function"]
if setTimeFunction != nil && setTimeFunction.(string) != "" {
dst["time_function"] = timeFunctionMapNewOld[term.ThresholdOccurrences]
} else {
dst["threshold_occurrences"] = strings.ToLower(string(term.ThresholdOccurrences))
}
} else {
dst["threshold_occurrences"] = strings.ToLower(string(term.ThresholdOccurrences))
}
Expand Down
26 changes: 25 additions & 1 deletion newrelic/structures_newrelic_nrql_alert_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package newrelic

import (
"strings"
"testing"

"github.com/newrelic/newrelic-client-go/pkg/alerts"
Expand Down Expand Up @@ -394,8 +395,31 @@ func TestFlattenNrqlAlertCondition(t *testing.T) {
&nrqlConditionOutlier,
}

for _, condition := range conditions {
// Use the API object above to construct a "user-configured" critical term.
// This ensures that in cases where the user has one configured, but there
// are two present in the API, we avoid crashing.
// https://github.com/newrelic/terraform-provider-newrelic/issues/882
term := nrqlCondition.Terms[0]
testingTerms := make([]map[string]interface{}, 0)
crit := map[string]interface{}{
"operator": strings.ToLower(string(term.Operator)),
// This is a critical term, so the priority is inferred.
// "priority": strings.ToLower(string(term.Priority)),
"threshold": term.Threshold,
"threshold_duration": term.ThresholdDuration,
"threshold_occurrences": strings.ToLower(string(term.ThresholdOccurrences)),
}
testingTerms = append(testingTerms, crit)

for i, condition := range conditions {
d := r.TestResourceData()

// Configure the critical testingTerms for one of the conditions.
if i == 0 {
err := d.Set("critical", testingTerms)
require.NoError(t, err)
}

err := flattenNrqlAlertCondition(nr.TestAccountID, condition, d)
require.NoError(t, err)

Expand Down

0 comments on commit 4160949

Please sign in to comment.