Skip to content

Commit

Permalink
change: added ValidateFunc for sigsci_site_alert (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
ponkio-o authored Mar 20, 2023
1 parent c9d0ad8 commit b1af5b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions provider/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ func existsInString(needle string, haystack ...string) bool {
return false
}

func existsInRange(needle int, min, max int) bool {
if needle >= min && needle <= max {
return true
}
return false
}

func expandRuleConditions(conditionsResource *schema.Set) []sigsci.Condition {
var conditions []sigsci.Condition
for _, genericElement := range conditionsResource.List() {
Expand Down
20 changes: 20 additions & 0 deletions provider/resource_site_alert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package provider

import (
"errors"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/signalsciences/go-sigsci"
)
Expand Down Expand Up @@ -33,11 +35,23 @@ func resourceSiteAlert() *schema.Resource {
Type: schema.TypeInt,
Description: "The number of minutes of past traffic to examine. Must be 1, 10 or 60.",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if existsInInt(val.(int), 1, 10, 60) {
return nil, nil
}
return nil, []error{errors.New("interval must be 1, 10, or 60")}
},
},
"threshold": {
Type: schema.TypeInt,
Description: "The number of occurrences of the tag in the interval needed to trigger the alert. Min 1, Max 10000",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if existsInRange(val.(int), 1, 10000) {
return nil, nil
}
return nil, []error{errors.New("threshold must be between 1 and 10000")}
},
},
"enabled": {
Type: schema.TypeBool,
Expand All @@ -48,6 +62,12 @@ func resourceSiteAlert() *schema.Resource {
Type: schema.TypeString,
Description: "A flag that describes what happens when the alert is triggered. 'info' creates an incident in the dashboard. 'flagged' creates an incident and blocks traffic for 24 hours. Must be info or flagged.",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if existsInString(val.(string), "info", "flagged") {
return nil, nil
}
return nil, []error{errors.New("action must be 'info' or 'flagged'")}
},
},
"skip_notifications": {
Type: schema.TypeBool,
Expand Down

0 comments on commit b1af5b3

Please sign in to comment.