diff --git a/docs/resources/site_agent_alert.md b/docs/resources/site_agent_alert.md new file mode 100644 index 0000000..24b2770 --- /dev/null +++ b/docs/resources/site_agent_alert.md @@ -0,0 +1,37 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sigsci_site_agent_alert Resource - terraform-provider-sigsci" +subcategory: "" +description: |- + +--- + +# sigsci_site_agent_alert (Resource) + + + + + + +## Schema + +### Required + +- `action` (String) Action for agent alert. +- `interval` (Number) Integer value for interval. Must be 5, 10 or 60. +- `site_short_name` (String) Site short name +- `tag_name` (String) The name of the tag whose occurrences the alert is watching. Must match an existing tag +- `threshold` (Number) The number of occurrences of the tag in the interval needed to trigger the alert. Min 0, Max 10000 + +### Optional + +- `block_duration_seconds` (Number) The number of seconds this alert is active. +- `enabled` (Boolean) A flag to toggle this alert. +- `long_name` (String) description +- `skip_notifications` (Boolean) A flag to skip notifications + +### Read-Only + +- `id` (String) The ID of this resource. + + diff --git a/provider/provider.go b/provider/provider.go index 315ba67..37529af 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -1,10 +1,11 @@ package provider import ( + "sync" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/terraform" "github.com/signalsciences/go-sigsci" - "sync" ) // Provider is the Signalsciences terraform provider, returns a terraform.ResourceProvider @@ -65,6 +66,7 @@ func Provider() terraform.ResourceProvider { "sigsci_site_signal_tag": resourceSiteSignalTag(), "sigsci_site_redaction": resourceSiteRedaction(), "sigsci_site_alert": resourceSiteAlert(), + "sigsci_site_agent_alert": resourceSiteAgentAlert(), "sigsci_site_templated_rule": resourceSiteTemplatedRule(), "sigsci_site_rule": resourceSiteRule(), "sigsci_site_blocklist": resourceSiteBlocklist(), diff --git a/provider/resource_site_agent_alert.go b/provider/resource_site_agent_alert.go new file mode 100644 index 0000000..d3d1894 --- /dev/null +++ b/provider/resource_site_agent_alert.go @@ -0,0 +1,83 @@ +package provider + +import ( + "errors" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func resourceSiteAgentAlert() *schema.Resource { + return &schema.Resource{ + Create: resourceSiteAlertCreate, + Update: resourceSiteAlertUpdate, + Read: resourceSiteAlertRead, + Delete: resourceSiteAlertDelete, + Importer: &siteImporter, + Schema: map[string]*schema.Schema{ + "site_short_name": { + Type: schema.TypeString, + Description: "Site short name", + Required: true, + ForceNew: true, + }, + "tag_name": { + Type: schema.TypeString, + Description: "The name of the tag whose occurrences the alert is watching. Must match an existing tag", + Required: true, + }, + "long_name": { + Type: schema.TypeString, + Description: "description", + Optional: true, + }, + "interval": { + Type: schema.TypeInt, + 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) { + return nil, nil + } + return nil, []error{errors.New("interval must be 5, 10, or 60")} + }, + }, + "threshold": { + Type: schema.TypeInt, + Description: "The number of occurrences of the tag in the interval needed to trigger the alert. Min 0, Max 10000", + Required: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if existsInRange(val.(int), 0, 10000) { + return nil, nil + } + return nil, []error{errors.New("threshold must be between 0 and 10000")} + }, + }, + "enabled": { + Type: schema.TypeBool, + Description: "A flag to toggle this alert.", + Optional: true, + }, + "action": { + Type: schema.TypeString, + Description: "Action for agent alert.", + Required: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if existsInString(val.(string), "siteMetricInfo") { + return nil, nil + } + return nil, []error{errors.New("action must be 'siteMetricInfo'")} + }, + }, + "skip_notifications": { + Type: schema.TypeBool, + Description: "A flag to skip notifications", + Optional: true, + }, + "block_duration_seconds": { + Type: schema.TypeInt, + Description: "The number of seconds this alert is active.", + Optional: true, + }, + }, + } +} diff --git a/provider/resource_site_agent_alert_test.go b/provider/resource_site_agent_alert_test.go new file mode 100644 index 0000000..bebdf06 --- /dev/null +++ b/provider/resource_site_agent_alert_test.go @@ -0,0 +1,77 @@ +package provider + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +// TODO implement sweepers for everyone +func TestAccResourceSiteAgentAlertCRUD(t *testing.T) { + t.Parallel() + resourceName := "sigsci_site_alert.test_site_alert" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(` + resource "sigsci_site_agent_alert" "test_site_agent_alert" { + action = "siteMetricInfo" + block_duration_seconds = 21600 + enabled = true + interval = 10 + long_name = "test_alert" + site_short_name = "%s" + skip_notifications = true + tag_name = "requests_total" + threshold = 8400 + }`, testSite), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "action", "siteMetricInfo"), + resource.TestCheckResourceAttr(resourceName, "block_duration_seconds", "21600"), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "site_short_name", testSite), + resource.TestCheckResourceAttr(resourceName, "tag_name", "requests_total"), + resource.TestCheckResourceAttr(resourceName, "long_name", "test_alert"), + resource.TestCheckResourceAttr(resourceName, "interval", "10"), + resource.TestCheckResourceAttr(resourceName, "skip_notifications", "true"), + resource.TestCheckResourceAttr(resourceName, "threshold", "8400"), + ), + }, + { + Config: fmt.Sprintf(` + resource "sigsci_site_agent_alert" "test_site_agent_alert" { + action = "siteMetricInfo" + block_duration_seconds = 21600 + enabled = false + interval = 5 + long_name = "test_alert" + site_short_name = "%s" + skip_notifications = true + tag_name = "requests_total" + threshold = 6300 + }`, testSite), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "action", "siteMetricInfo"), + resource.TestCheckResourceAttr(resourceName, "block_duration_seconds", "21600"), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "site_short_name", testSite), + resource.TestCheckResourceAttr(resourceName, "tag_name", "requests_total"), + resource.TestCheckResourceAttr(resourceName, "long_name", "test_alert"), + resource.TestCheckResourceAttr(resourceName, "interval", "5"), + resource.TestCheckResourceAttr(resourceName, "skip_notifications", "true"), + resource.TestCheckResourceAttr(resourceName, "threshold", "6300"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdPrefix: fmt.Sprintf("%s:", testSite), + ImportStateCheck: testAccImportStateCheckFunction(1), + ImportStateVerify: true, + }, + }, + }) +}