Skip to content

Commit

Permalink
Merge pull request #202 from terraform-providers/sblue/fix/alert-cond…
Browse files Browse the repository at this point in the history
…ition-duration-contraints

fix: align alert condition duration constraints to NR's API constraints
  • Loading branch information
sanderblue authored Nov 11, 2019
2 parents 5299364 + 7548fce commit 46ad9a9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion newrelic/resource_newrelic_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func resourceNewRelicAlertCondition() *schema.Resource {
"duration": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: intInSlice([]int{5, 10, 15, 30, 60, 120}),
ValidateFunc: validation.IntBetween(5, 120),
},
"operator": {
Type: schema.TypeString,
Expand Down
88 changes: 86 additions & 2 deletions newrelic/resource_newrelic_alert_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ resource "newrelic_alert_condition" "foo" {
policy_id = "${newrelic_alert_policy.foo.id}"
name = "tf-test-updated-%[1]s"
enabled = true
enabled = true
type = "apm_app_metric"
entities = ["${data.newrelic_application.app.id}"]
metric = "apdex"
Expand Down Expand Up @@ -318,7 +318,7 @@ resource "newrelic_alert_condition" "foo" {
policy_id = "${newrelic_alert_policy.foo.id}"
name = "tf-test-%[1]s"
enabled = false
enabled = false
type = "apm_app_metric"
entities = ["${data.newrelic_application.app.id}"]
metric = "apdex"
Expand Down Expand Up @@ -405,4 +405,88 @@ resource "newrelic_alert_condition" "foo" {
`
}

func TestErrorThrownUponConditionTermDurationGreaterThan120(t *testing.T) {
expectedErrorMsg, _ := regexp.Compile(`expected term.0.duration to be in the range \(5 - 120\)`)
resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testErrorThrownUponConditionTermDurationGreaterThan120(),
ExpectError: expectedErrorMsg,
},
},
})
}

func testErrorThrownUponConditionTermDurationGreaterThan120() string {
return `
provider "newrelic" {
api_key = "foo"
}
resource "newrelic_alert_policy" "foo" {
name = "tf-test-%[1]s"
}
resource "newrelic_alert_condition" "foo" {
policy_id = "${newrelic_alert_policy.foo.id}"
name = "test-term-duration"
type = "apm_app_metric"
entities = ["12345"]
metric = "apdex"
runbook_url = "https://foo.example.com"
condition_scope = "application"
term {
duration = 121
operator = "below"
priority = "critical"
threshold = "0.75"
time_function = "all"
}
}
`
}

func TestErrorThrownUponConditionTermDurationLessThan5(t *testing.T) {
expectedErrorMsg, _ := regexp.Compile(`expected term.0.duration to be in the range \(5 - 120\)`)
resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testErrorThrownUponConditionTermDurationLessThan5(),
ExpectError: expectedErrorMsg,
},
},
})
}

func testErrorThrownUponConditionTermDurationLessThan5() string {
return `
provider "newrelic" {
api_key = "foo"
}
resource "newrelic_alert_policy" "foo" {
name = "tf-test-%[1]s"
}
resource "newrelic_alert_condition" "foo" {
policy_id = "${newrelic_alert_policy.foo.id}"
name = "test-term-duration"
type = "apm_app_metric"
entities = ["12345"]
metric = "apdex"
runbook_url = "https://foo.example.com"
condition_scope = "application"
term {
duration = 4
operator = "below"
priority = "critical"
threshold = "0.75"
time_function = "all"
}
}
`
}

// TODO: const testAccCheckNewRelicAlertConditionConfigMulti = `
4 changes: 2 additions & 2 deletions website/docs/r/alert_condition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resource "newrelic_alert_condition" "foo" {
metric = "apdex"
runbook_url = "https://www.example.com"
condition_scope = "application"
term {
duration = 5
operator = "below"
Expand Down Expand Up @@ -60,7 +60,7 @@ The following arguments are supported:

The `term` mapping supports the following arguments:

* `duration` - (Required) In minutes, must be: `5`, `10`, `15`, `30`, `60`, or `120`.
* `duration` - (Required) In minutes, must be in the range of `5` to `120`, inclusive.
* `operator` - (Optional) `above`, `below`, or `equal`. Defaults to `equal`.
* `priority` - (Optional) `critical` or `warning`. Defaults to `critical`.
* `threshold` - (Required) Must be 0 or greater.
Expand Down

0 comments on commit 46ad9a9

Please sign in to comment.