Skip to content

Commit

Permalink
Agent alerts fix1 (#229)
Browse files Browse the repository at this point in the history
* Updating rate limiting example

* Update resource_site_agent_alert.go

* Bug fix for site agent alerts

* Bug fix for site agent alerts
* Up'ed the sigsci SDK version to 0.1.21

* Using sigsci go SDK version 0.1.22
  • Loading branch information
BrooksCunningham authored Aug 9, 2024
1 parent 6fca946 commit 704f538
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/hashicorp/terraform-plugin-docs v0.14.1
github.com/hashicorp/terraform-plugin-sdk v1.14.0
github.com/signalsciences/go-sigsci v0.1.20
github.com/signalsciences/go-sigsci v0.1.22
golang.org/x/lint v0.0.0-20190409202823-959b441ac422
honnef.co/go/tools v0.4.2
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/signalsciences/go-sigsci v0.1.20 h1:lMirioZwNWhnizOqryzvr1dsvOpmOn2e4MK+XEzlXrA=
github.com/signalsciences/go-sigsci v0.1.20/go.mod h1:CXwoXk81ZwFdne6o8cnAYwxvke5kcLg7zE6Bl/e1KUo=
github.com/signalsciences/go-sigsci v0.1.21 h1:5lkqv66jlPxqe7BHv46eEefttGo/6nRqLjqAqNuA5xA=
github.com/signalsciences/go-sigsci v0.1.21/go.mod h1:CXwoXk81ZwFdne6o8cnAYwxvke5kcLg7zE6Bl/e1KUo=
github.com/signalsciences/go-sigsci v0.1.22 h1:WDDYLS5sVbUcPgDAOrxjDfIWdfuBjISOtHvwDIR1U7M=
github.com/signalsciences/go-sigsci v0.1.22/go.mod h1:CXwoXk81ZwFdne6o8cnAYwxvke5kcLg7zE6Bl/e1KUo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
Expand Down
159 changes: 151 additions & 8 deletions provider/resource_site_agent_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"errors"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/signalsciences/go-sigsci"
)

func resourceSiteAgentAlert() *schema.Resource {
return &schema.Resource{
Create: resourceSiteAlertCreate,
Update: resourceSiteAlertUpdate,
Read: resourceSiteAlertRead,
Delete: resourceSiteAlertDelete,
Create: resourceSiteAgentAlertCreate,
Update: resourceSiteAgentAlertUpdate,
Read: resourceSiteAgentAlertRead,
Delete: resourceSiteAgentAlertDelete,
Importer: &siteImporter,
Schema: map[string]*schema.Schema{
"site_short_name": {
Expand All @@ -32,7 +33,7 @@ func resourceSiteAgentAlert() *schema.Resource {
},
"interval": {
Type: schema.TypeInt,
Description: "Integer value for interval. Must be 5, 10 or 60.",
Description: "Integer value for interval. Must be 5, 10, or 60.",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if existsInInt(val.(int), 5, 10, 60) {
Expand All @@ -43,13 +44,13 @@ func resourceSiteAgentAlert() *schema.Resource {
},
"threshold": {
Type: schema.TypeInt,
Description: "The number of occurrences of the tag in the interval needed to trigger the alert. Min 0, Max 10000",
Description: "The number of occurrences of the tag in the interval needed to trigger the alert. Min 0, Max 3600000",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if existsInRange(val.(int), 0, 10000) {
if existsInRange(val.(int), 0, 3600000) {
return nil, nil
}
return nil, []error{errors.New("threshold must be between 0 and 10000")}
return nil, []error{errors.New("threshold must be between 0 and 3600000")}
},
},
"enabled": {
Expand Down Expand Up @@ -78,6 +79,148 @@ func resourceSiteAgentAlert() *schema.Resource {
Description: "The number of seconds this alert is active.",
Optional: true,
},
"field_name": {
Type: schema.TypeString,
Description: "Field_name for agent alert.",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if existsInString(val.(string), "siteMetric") {
return nil, nil
}
return nil, []error{errors.New("action must be 'siteMetric'")}
},
},
"operator": {
Type: schema.TypeString,
Description: "Operator for agent alert.",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if existsInString(val.(string), "notEqualTo", "equalTo", "lessThan", "greaterThan") {
return nil, nil
}
return nil, []error{errors.New("action must be 'notEqualTo', 'equalTo', 'lessThan', 'greaterThan'")}
},
},
},
}
}

func resourceSiteAgentAlertCreate(d *schema.ResourceData, m interface{}) error {
pm := m.(providerMetadata)
sc := pm.Client

alert, err := sc.CreateCustomAlert(pm.Corp, d.Get("site_short_name").(string), sigsci.CustomAlertBody{
TagName: d.Get("tag_name").(string),
LongName: d.Get("long_name").(string),
Interval: d.Get("interval").(int),
Threshold: d.Get("threshold").(int),
Enabled: d.Get("enabled").(bool),
Action: d.Get("action").(string),
SkipNotifications: d.Get("skip_notifications").(bool),
BlockDurationSeconds: d.Get("block_duration_seconds").(int),
FieldName: d.Get("field_name").(string),
Operator: d.Get("operator").(string),
})

if err != nil {
return err
}
d.SetId(alert.ID)
return resourceSiteAgentAlertRead(d, m)
}

func resourceSiteAgentAlertRead(d *schema.ResourceData, m interface{}) error {
pm := m.(providerMetadata)
sc := pm.Client

alert, err := sc.GetCustomAlert(pm.Corp, d.Get("site_short_name").(string), d.Id())
if err != nil {
d.SetId("")
return nil
}

d.SetId(alert.ID)
err = d.Set("site_short_name", d.Get("site_short_name").(string))
if err != nil {
return err
}
err = d.Set("tag_name", alert.TagName)
if err != nil {
return err
}
err = d.Set("long_name", alert.LongName)
if err != nil {
return err
}
err = d.Set("interval", alert.Interval)
if err != nil {
return err
}
err = d.Set("threshold", alert.Threshold)
if err != nil {
return err
}
err = d.Set("enabled", alert.Enabled)
if err != nil {
return err
}
err = d.Set("action", alert.Action)
if err != nil {
return err
}
err = d.Set("skip_notifications", alert.SkipNotifications)
if err != nil {
return err
}
err = d.Set("block_duration_seconds", alert.BlockDurationSeconds)
if err != nil {
return err
}
err = d.Set("field_name", alert.FieldName)
if err != nil {
return err
}
err = d.Set("operator", alert.Operator)
if err != nil {
return err
}

return nil
}

func resourceSiteAgentAlertUpdate(d *schema.ResourceData, m interface{}) error {
pm := m.(providerMetadata)
sc := pm.Client

alert, err := sc.UpdateCustomAlert(pm.Corp, d.Get("site_short_name").(string), d.Id(), sigsci.CustomAlertBody{
TagName: d.Get("tag_name").(string),
LongName: d.Get("long_name").(string),
Interval: d.Get("interval").(int),
Threshold: d.Get("threshold").(int),
Enabled: d.Get("enabled").(bool),
Action: d.Get("action").(string),
SkipNotifications: d.Get("skip_notifications").(bool),
BlockDurationSeconds: d.Get("block_duration_seconds").(int),
FieldName: d.Get("field_name").(string),
Operator: d.Get("operator").(string),
})
if err != nil {
d.SetId("")
return err
}

d.SetId(alert.ID)
return resourceSiteAgentAlertRead(d, m)
}

func resourceSiteAgentAlertDelete(d *schema.ResourceData, m interface{}) error {
pm := m.(providerMetadata)
sc := pm.Client

err := sc.DeleteCustomAlert(pm.Corp, d.Get("site_short_name").(string), d.Id())
if err != nil {
return err
}
d.SetId("")
return nil
}

0 comments on commit 704f538

Please sign in to comment.