Skip to content

Commit

Permalink
enable creating alerts from json files in folder (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
samidbb authored Feb 13, 2024
1 parent 70a63c8 commit fd4873a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
34 changes: 34 additions & 0 deletions grafana_alert/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

resource "grafana_rule_group" "alertrule" {
for_each = { for file in var.alertrule_files : file => jsondecode(file("${file}"))
}
name = each.value.groups[0].name
folder_uid = var.folder # Folder specified in the json will be skipped since it does not contain UID.
# This solve the example for having to convert "1m" to integer seconds
interval_seconds = regex("^[0-9]*", each.value.groups[0].interval) * (regex("[m|h|s]$", each.value.groups[0].interval) == "m" ? 60 : (regex("[m|h|s]$", each.value.groups[0].interval) == "h" ? 1440 : 1))
dynamic "rule" {
for_each = each.value.groups[0].rules
content {
name = rule.value.title
for = rule.value.for
condition = rule.value.condition
no_data_state = rule.value.noDataState
annotations = rule.value.annotations
exec_err_state = rule.value.execErrState
is_paused = rule.value.isPaused
labels = rule.value.labels
dynamic "data" {
for_each = rule.value.data
content {
datasource_uid = data.value.datasourceUid
ref_id = data.value.refId
relative_time_range {
from = data.value.relativeTimeRange.from
to = data.value.relativeTimeRange.to
}
model = jsonencode(data.value.model)
}
}
}
}
}
9 changes: 9 additions & 0 deletions grafana_alert/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "folder" {
description = "UID of the folder to create the alert rules in"
type = string
}

variable "alertrule_files" {
description = "Path to the json files with alert rules"
type = list(string)
}
10 changes: 10 additions & 0 deletions grafana_alert/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.3.0, < 1.6.0"

required_providers {
grafana = {
source = "grafana/grafana"
version = ">= 2.9.0"
}
}
}

0 comments on commit fd4873a

Please sign in to comment.