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

Relax NRQL condition limitations #136

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions newrelic/resource_newrelic_nrql_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func resourceNewRelicNrqlAlertCondition() *schema.Resource {
"since_value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"1", "2", "3", "4", "5"}, false),
ValidateFunc: StringIntBetween(1, 20),
},
},
},
Expand All @@ -64,7 +64,7 @@ func resourceNewRelicNrqlAlertCondition() *schema.Resource {
"duration": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: intInSlice([]int{1, 2, 3, 4, 5, 10, 15, 30, 60, 120}),
ValidateFunc: validation.IntAtLeast(1),
},
"operator": {
Type: schema.TypeString,
Expand Down
16 changes: 8 additions & 8 deletions newrelic/resource_newrelic_nrql_alert_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccNewRelicNrqlAlertCondition_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "term.#", "1"),
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "term.0.duration", "5"),
"newrelic_nrql_alert_condition.foo", "term.0.duration", "55"),
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "term.0.operator", "below"),
resource.TestCheckResourceAttr(
Expand All @@ -43,7 +43,7 @@ func TestAccNewRelicNrqlAlertCondition_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "nrql.0.query", "SELECT uniqueCount(hostname) FROM ComputeSample"),
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "nrql.0.since_value", "5"),
"newrelic_nrql_alert_condition.foo", "nrql.0.since_value", "20"),
),
},
{
Expand All @@ -59,7 +59,7 @@ func TestAccNewRelicNrqlAlertCondition_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "term.#", "1"),
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "term.0.duration", "10"),
"newrelic_nrql_alert_condition.foo", "term.0.duration", "100"),
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "term.0.operator", "below"),
resource.TestCheckResourceAttr(
Expand All @@ -73,7 +73,7 @@ func TestAccNewRelicNrqlAlertCondition_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "nrql.0.query", "SELECT uniqueCount(hostname) as Hosts FROM ComputeSample"),
resource.TestCheckResourceAttr(
"newrelic_nrql_alert_condition.foo", "nrql.0.since_value", "3"),
"newrelic_nrql_alert_condition.foo", "nrql.0.since_value", "1"),
),
},
},
Expand Down Expand Up @@ -154,15 +154,15 @@ resource "newrelic_nrql_alert_condition" "foo" {
enabled = false

term {
duration = 5
duration = 55
operator = "below"
priority = "critical"
threshold = "0.75"
time_function = "all"
}
nrql {
query = "SELECT uniqueCount(hostname) FROM ComputeSample"
since_value = "5"
since_value = "20"
}
value_function = "single_value"
}
Expand All @@ -184,15 +184,15 @@ resource "newrelic_nrql_alert_condition" "foo" {
enabled = false

term {
duration = 10
duration = 100
operator = "below"
priority = "critical"
threshold = "0.65"
time_function = "all"
}
nrql {
query = "SELECT uniqueCount(hostname) as Hosts FROM ComputeSample"
since_value = "3"
since_value = "1"
}
value_function = "single_value"
}
Expand Down
25 changes: 24 additions & 1 deletion newrelic/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package newrelic

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"strconv"
)

func float64Gte(gte float64) schema.SchemaValidateFunc {
Expand Down Expand Up @@ -41,3 +41,26 @@ func intInSlice(valid []int) schema.SchemaValidateFunc {
return
}
}

func StringIntBetween(min, max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

intValue, err := strconv.Atoi(v)
if err != nil {
es = append(es, fmt.Errorf("expected %s to be convertable to int, got %s", k, v))
return
}

if intValue < min || intValue > max {
es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %s", k, min, max, v))
return
}

return
}
}
30 changes: 29 additions & 1 deletion newrelic/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ type testCase struct {
expectedErr *regexp.Regexp
}

func TestValidationIntBetweenStrings(t *testing.T) {
runTestCases(t, []testCase{
{
val: "1",
f: StringIntBetween(1, 3),
},
{
val: "3",
f: StringIntBetween(1, 3),
},
{
val: "notanumber",
f: StringIntBetween(1, 3),
expectedErr: regexp.MustCompile("expected [\\w]+ to be convertable to int, got notanumber"),
},
{
val: 2,
f: StringIntBetween(1, 3),
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"),
},
{
val: "4",
f: StringIntBetween(1, 3),
expectedErr: regexp.MustCompile("expected [\\w]+ to be in the range \\(1 - 3\\), got 4"),
},
})
}

func TestValidationIntInInSlice(t *testing.T) {
runTestCases(t, []testCase{
{
Expand Down Expand Up @@ -75,7 +103,7 @@ func runTestCases(t *testing.T, cases []testCase) {
}

if !matchErr(errs, tc.expectedErr) {
t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
t.Fatalf("expected test case %d to produce error matching \"%s\", got '%v'", i, tc.expectedErr, errs[0])
}
}
}
4 changes: 2 additions & 2 deletions website/docs/r/nrql_alert_condition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The following arguments are supported:

The `term` mapping supports the following arguments:

* `duration` - (Required) In minutes, must be: `1`, `2`, `3`, `4`, `5`, `10`, `15`, `30`, `60`, or `120`.
* `duration` - (Required) Query duration in minutes.
* `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 All @@ -66,7 +66,7 @@ The `term` mapping supports the following arguments:
The `nrql` attribute supports the following arguments:

* `query` - (Required) The NRQL query to execute for the condition.
* `since_value` - (Required) The value to be used in the `SINCE <X> MINUTES AGO` clause for the NRQL query. Must be: `1`, `2`, `3`, `4`, or `5`.
* `since_value` - (Required) The value to be used in the `SINCE <X> MINUTES AGO` clause for the NRQL query. Must be integer between `1` and `20` (inclusive).

## Attributes Reference

Expand Down