Skip to content
Draft
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
28 changes: 14 additions & 14 deletions docs/resources/synthetic_monitoring_check_alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ resource "grafana_synthetic_monitoring_check" "main" {
resource "grafana_synthetic_monitoring_check_alerts" "main" {
check_id = grafana_synthetic_monitoring_check.main.id
alerts = [{
name = "ProbeFailedExecutionsTooHigh"
threshold = 1
period = "15m"
runbook_url = ""
name = "ProbeFailedExecutionsTooHigh"
threshold = 1
period = "15m"
},
{
name = "TLSTargetCertificateCloseToExpiring"
threshold = 14
period = ""
runbook_url = ""
name = "TLSTargetCertificateCloseToExpiring"
threshold = 14
},
{
name = "HTTPRequestDurationTooHighAvg"
Expand All @@ -58,22 +55,25 @@ resource "grafana_synthetic_monitoring_check_alerts" "main" {

### Required

- `alerts` (Set of Object) List of alerts for the check. (see [below for nested schema](#nestedatt--alerts))
- `alerts` (Block List, Min: 1) List of alerts for the check. (see [below for nested schema](#nestedblock--alerts))
- `check_id` (Number) The ID of the check to manage alerts for.

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedatt--alerts"></a>
<a id="nestedblock--alerts"></a>
### Nested Schema for `alerts`

Required:

- `name` (String)
- `period` (String)
- `runbook_url` (String)
- `threshold` (Number)
- `name` (String) Name of the alert. Required.
- `threshold` (Number) Threshold value for the alert.

Optional:

- `period` (String) Period for the alert. Required and must be one of: `5m`, `10m`, `15m`, `20m`, `30m`, `1h`.
- `runbook_url` (String) URL to runbook documentation for this alert.

## Import

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ resource "grafana_synthetic_monitoring_check" "main" {
resource "grafana_synthetic_monitoring_check_alerts" "main" {
check_id = grafana_synthetic_monitoring_check.main.id
alerts = [{
name = "ProbeFailedExecutionsTooHigh"
threshold = 1
period = "15m"
runbook_url = ""
name = "ProbeFailedExecutionsTooHigh"
threshold = 1
period = "15m"
},
{
name = "TLSTargetCertificateCloseToExpiring"
threshold = 14
period = ""
runbook_url = ""
name = "TLSTargetCertificateCloseToExpiring"
threshold = 14
},
{
name = "HTTPRequestDurationTooHighAvg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ resource "grafana_synthetic_monitoring_check" "main" {
resource "grafana_synthetic_monitoring_check_alerts" "main" {
check_id = grafana_synthetic_monitoring_check.main.id
alerts = [{
name = "ProbeFailedExecutionsTooHigh"
threshold = 2
period = "10m"
runbook_url = ""
name = "ProbeFailedExecutionsTooHigh"
threshold = 2
period = "10m"
},
{
name = "TLSTargetCertificateCloseToExpiring"
threshold = 7
period = ""
runbook_url = ""
name = "TLSTargetCertificateCloseToExpiring"
threshold = 7
}]
}
18 changes: 7 additions & 11 deletions internal/resources/syntheticmonitoring/resource_check_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ Manages alerts for a check in Grafana Synthetic Monitoring.
},
"alerts": {
Description: "List of alerts for the check.",
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
ConfigMode: schema.SchemaConfigModeAttr,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -77,9 +76,7 @@ Manages alerts for a check in Grafana Synthetic Monitoring.
"period": {
Description: "Period for the alert. Required and must be one of: `5m`, `10m`, `15m`, `20m`, `30m`, `1h`.",
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{
"", "5m", "10m", "15m", "20m", "30m", "1h",
}, false),
Expand All @@ -88,7 +85,6 @@ Manages alerts for a check in Grafana Synthetic Monitoring.
Description: "URL to runbook documentation for this alert.",
Type: schema.TypeString,
Optional: true,
Default: "",
},
},
},
Expand Down Expand Up @@ -184,16 +180,13 @@ func resourceCheckAlertDelete(ctx context.Context, d *schema.ResourceData, c *sm
}

func makeCheckAlerts(d *schema.ResourceData) ([]model.CheckAlert, error) {
alertsSet := d.Get("alerts").(*schema.Set)
alertsList := alertsSet.List()
alertsList := d.Get("alerts").([]interface{})
alerts := make([]model.CheckAlert, len(alertsList))

for i, alertMap := range alertsList {
alertData := alertMap.(map[string]any)
name := alertData["name"].(string)
period, hasPeriod := alertData["period"].(string)
runbookURL, hasRunbookURL := alertData["runbook_url"].(string)

alert := model.CheckAlert{
Name: name,
Threshold: alertData["threshold"].(float64),
Expand All @@ -203,8 +196,11 @@ func makeCheckAlerts(d *schema.ResourceData) ([]model.CheckAlert, error) {
alert.Period = period
}

if hasRunbookURL {
alert.RunbookUrl = runbookURL
// Handle runbook_url: support omitted, empty string, or valid string
if runbookURLVal, exists := alertData["runbook_url"]; exists {
if runbookURL, ok := runbookURLVal.(string); ok {
alert.RunbookUrl = runbookURL
}
}

alerts[i] = alert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,17 @@ func TestAccResourceCheckAlerts(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check_alerts.main", "id"),
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check_alerts.main", "check_id"),
resource.TestCheckTypeSetElemNestedAttrs("grafana_synthetic_monitoring_check_alerts.main", "alerts.*", map[string]string{
"name": "ProbeFailedExecutionsTooHigh",
"threshold": "1",
"period": "15m",
"runbook_url": "",
}),
resource.TestCheckTypeSetElemNestedAttrs("grafana_synthetic_monitoring_check_alerts.main", "alerts.*", map[string]string{
"name": "TLSTargetCertificateCloseToExpiring",
"threshold": "14",
"period": "",
"runbook_url": "",
}),
resource.TestCheckTypeSetElemNestedAttrs("grafana_synthetic_monitoring_check_alerts.main", "alerts.*", map[string]string{
"name": "HTTPRequestDurationTooHighAvg",
"threshold": "5000",
"period": "10m",
"runbook_url": "https://wiki.company.com/runbooks/http-duration",
}),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.0.name", "ProbeFailedExecutionsTooHigh"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.0.threshold", "1"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.0.period", "15m"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.name", "TLSTargetCertificateCloseToExpiring"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.threshold", "14"),
resource.TestCheckNoResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.period"),
resource.TestCheckNoResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.runbook_url"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.2.name", "HTTPRequestDurationTooHighAvg"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.2.threshold", "5000"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.2.period", "10m"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.2.runbook_url", "https://wiki.company.com/runbooks/http-duration"),
),
},
{
Expand All @@ -53,18 +46,13 @@ func TestAccResourceCheckAlerts(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check_alerts.main", "id"),
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check_alerts.main", "check_id"),
resource.TestCheckTypeSetElemNestedAttrs("grafana_synthetic_monitoring_check_alerts.main", "alerts.*", map[string]string{
"name": "ProbeFailedExecutionsTooHigh",
"threshold": "2",
"period": "10m",
"runbook_url": "",
}),
resource.TestCheckTypeSetElemNestedAttrs("grafana_synthetic_monitoring_check_alerts.main", "alerts.*", map[string]string{
"name": "TLSTargetCertificateCloseToExpiring",
"threshold": "7",
"period": "",
"runbook_url": "",
}),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.0.name", "ProbeFailedExecutionsTooHigh"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.0.threshold", "2"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.0.period", "10m"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.name", "TLSTargetCertificateCloseToExpiring"),
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.threshold", "7"),
resource.TestCheckNoResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.period"),
resource.TestCheckNoResourceAttr("grafana_synthetic_monitoring_check_alerts.main", "alerts.1.runbook_url"),
),
},
},
Expand Down Expand Up @@ -112,7 +100,5 @@ resource "grafana_synthetic_monitoring_check_alerts" "main" {
alerts = [{
name = "InvalidAlertName"
threshold = 0.5
period = ""
runbook_url = ""
}]
}`
2 changes: 1 addition & 1 deletion provider_schema.json

Large diffs are not rendered by default.

Loading