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

Adding functionality for Agent Alerts #225

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions docs/resources/site_agent_alert.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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.


4 changes: 3 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(),
Expand Down
83 changes: 83 additions & 0 deletions provider/resource_site_agent_alert.go
Original file line number Diff line number Diff line change
@@ -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",
shawnps marked this conversation as resolved.
Show resolved Hide resolved
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,
},
},
}
}
77 changes: 77 additions & 0 deletions provider/resource_site_agent_alert_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}
Loading