-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable creating alerts from json files in folder (#11)
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |