resource "grafana_folder" "folders" {
  for_each = var.manage_grafana_dashboards ? var.grafana_folders : {}

  title = each.value.title
  uid   = each.value.uid
}

resource "grafana_dashboard" "dashboards" {
  for_each = var.manage_grafana_dashboards ? var.grafana_dashboards : {}

  config_json = file(startswith(each.value.path, "/") ? each.value.path : "${path.module}/${each.value.path}")
  folder      = each.value.folder
  message     = each.value.message
  overwrite   = each.value.overwrite

  depends_on = [grafana_folder.folders]
}
