Add dashboard UID auto-generation and Gitea CI workflow
This commit is contained in:
49
environments/modules/grafana_dashboard_folder_xt5/locals.tf
Normal file
49
environments/modules/grafana_dashboard_folder_xt5/locals.tf
Normal file
@ -0,0 +1,49 @@
|
||||
locals {
|
||||
# Create a map of groups by dashboard_alert_group_name for parent lookup
|
||||
groups_by_name = {
|
||||
for group in var.groups : group.dashboard_alert_group_name => group
|
||||
}
|
||||
|
||||
# Helper function to extract parent folder name and child folder name
|
||||
# If dashboard_alert_group_name contains "/", split it into parent and child
|
||||
folder_structure = {
|
||||
for group in var.groups : group.dashboard_alert_group_name => {
|
||||
group = group
|
||||
parts = split("/", group.dashboard_alert_group_name)
|
||||
has_parent = length(split("/", group.dashboard_alert_group_name)) > 1
|
||||
parent_folder_name = length(split("/", group.dashboard_alert_group_name)) > 1 ? join("/", slice(split("/", group.dashboard_alert_group_name), 0, length(split("/", group.dashboard_alert_group_name)) - 1)) : null
|
||||
folder_title = length(split("/", group.dashboard_alert_group_name)) > 1 ? element(split("/", group.dashboard_alert_group_name), length(split("/", group.dashboard_alert_group_name)) - 1) : group.dashboard_alert_group_name
|
||||
# Get parent folder UID from parent group's folder_uid
|
||||
parent_folder_uid = length(split("/", group.dashboard_alert_group_name)) > 1 ? try(local.groups_by_name[join("/", slice(split("/", group.dashboard_alert_group_name), 0, length(split("/", group.dashboard_alert_group_name)) - 1))].folder_uid, null) : null
|
||||
}
|
||||
}
|
||||
|
||||
# Group folders by whether they should keep manual changes or be protected from destruction
|
||||
folders_ignore_only = {
|
||||
for group_name, folder_info in local.folder_structure : group_name => folder_info
|
||||
if lookup(folder_info.group, "keep_manual_changes", false) && !lookup(folder_info.group, "prevent_destroy_on_recreate", false)
|
||||
}
|
||||
|
||||
folders_protect_only = {
|
||||
for group_name, folder_info in local.folder_structure : group_name => folder_info
|
||||
if !lookup(folder_info.group, "keep_manual_changes", false) && lookup(folder_info.group, "prevent_destroy_on_recreate", false)
|
||||
}
|
||||
|
||||
folders_ignore_and_protect = {
|
||||
for group_name, folder_info in local.folder_structure : group_name => folder_info
|
||||
if lookup(folder_info.group, "keep_manual_changes", false) && lookup(folder_info.group, "prevent_destroy_on_recreate", false)
|
||||
}
|
||||
|
||||
folders_standard = {
|
||||
for group_name, folder_info in local.folder_structure : group_name => folder_info
|
||||
if !lookup(folder_info.group, "keep_manual_changes", false) && !lookup(folder_info.group, "prevent_destroy_on_recreate", false)
|
||||
}
|
||||
|
||||
# Create a map of all folders for parent UID lookup
|
||||
all_folders = merge(
|
||||
local.folders_ignore_and_protect,
|
||||
local.folders_ignore_only,
|
||||
local.folders_protect_only,
|
||||
local.folders_standard
|
||||
)
|
||||
}
|
||||
51
environments/modules/grafana_dashboard_folder_xt5/main.tf
Normal file
51
environments/modules/grafana_dashboard_folder_xt5/main.tf
Normal file
@ -0,0 +1,51 @@
|
||||
# Folders with both manual changes allowed and destroy protection
|
||||
# Note: Currently, Terraform Grafana provider doesn't support nested folders directly.
|
||||
# Folders will be created as flat structure. Nested structure can be configured
|
||||
# manually in Grafana UI or via API after creation.
|
||||
resource "grafana_folder" "folders_ignore_and_protect" {
|
||||
for_each = local.folders_ignore_and_protect
|
||||
|
||||
title = each.value.folder_title
|
||||
uid = each.value.group.folder_uid
|
||||
org_id = var.org_id
|
||||
|
||||
lifecycle {
|
||||
ignore_changes = [title]
|
||||
prevent_destroy = true
|
||||
}
|
||||
}
|
||||
|
||||
# Folders with only manual changes allowed
|
||||
resource "grafana_folder" "folders_ignore_only" {
|
||||
for_each = local.folders_ignore_only
|
||||
|
||||
title = each.value.folder_title
|
||||
uid = each.value.group.folder_uid
|
||||
org_id = var.org_id
|
||||
|
||||
lifecycle {
|
||||
ignore_changes = [title]
|
||||
}
|
||||
}
|
||||
|
||||
# Folders with only destroy protection
|
||||
resource "grafana_folder" "folders_protect_only" {
|
||||
for_each = local.folders_protect_only
|
||||
|
||||
title = each.value.folder_title
|
||||
uid = each.value.group.folder_uid
|
||||
org_id = var.org_id
|
||||
|
||||
lifecycle {
|
||||
prevent_destroy = true
|
||||
}
|
||||
}
|
||||
|
||||
# Standard folders without any special lifecycle management
|
||||
resource "grafana_folder" "folders_standard" {
|
||||
for_each = local.folders_standard
|
||||
|
||||
title = each.value.folder_title
|
||||
uid = each.value.group.folder_uid
|
||||
org_id = var.org_id
|
||||
}
|
||||
21
environments/modules/grafana_dashboard_folder_xt5/outputs.tf
Normal file
21
environments/modules/grafana_dashboard_folder_xt5/outputs.tf
Normal file
@ -0,0 +1,21 @@
|
||||
# Output for mapping of alert group names to folder IDs
|
||||
output "folder_ids" {
|
||||
description = "Mapping of alert group names to their folder IDs in Grafana"
|
||||
value = merge(
|
||||
{ for group_name, folder in grafana_folder.folders_ignore_and_protect : group_name => folder.id },
|
||||
{ for group_name, folder in grafana_folder.folders_ignore_only : group_name => folder.id },
|
||||
{ for group_name, folder in grafana_folder.folders_protect_only : group_name => folder.id },
|
||||
{ for group_name, folder in grafana_folder.folders_standard : group_name => folder.id }
|
||||
)
|
||||
}
|
||||
|
||||
# Output for mapping of alert group names to folder UIDs
|
||||
output "folder_uids" {
|
||||
description = "Mapping of alert group names to their folder UIDs in Grafana"
|
||||
value = merge(
|
||||
{ for group_name, folder in grafana_folder.folders_ignore_and_protect : group_name => folder.uid },
|
||||
{ for group_name, folder in grafana_folder.folders_ignore_only : group_name => folder.uid },
|
||||
{ for group_name, folder in grafana_folder.folders_protect_only : group_name => folder.uid },
|
||||
{ for group_name, folder in grafana_folder.folders_standard : group_name => folder.uid }
|
||||
)
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
variable "groups" {
|
||||
description = "List of alert groups with their definitions"
|
||||
type = list(object({
|
||||
dashboard_alert_group_name = string
|
||||
folder_uid = string
|
||||
alert_definitions_path = string
|
||||
dashboard_path_if_exist = optional(string, null)
|
||||
keep_manual_changes = optional(bool, false)
|
||||
prevent_destroy_on_recreate = optional(bool, false)
|
||||
alerts_on_datasources_uid = list(string)
|
||||
}))
|
||||
}
|
||||
|
||||
variable "org_id" {
|
||||
description = "ID of the Grafana organization"
|
||||
type = string
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
grafana = {
|
||||
source = "grafana/grafana"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user