Skip to content

Commit

Permalink
Merge pull request #61 from SumoLogic/new-tf-monitors
Browse files Browse the repository at this point in the history
Updating TF modules to use trigger_conditions
  • Loading branch information
himsharma01 authored Nov 15, 2022
2 parents 574967a + a6204f3 commit d5bd64b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 13 deletions.
118 changes: 106 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
resource "sumologic_monitor" "tf_monitor_1" {
locals {
# Logs Monitor
LogsCriticalAlert = [for d in var.triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Logs"]
LogsResolveCriticalAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedCritical" && var.monitor_monitor_type == "Logs"]
LogsWarningAlert = [for d in var.triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Logs"]
LogsResolveWarningAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedWarning" && var.monitor_monitor_type == "Logs"]
LogsMissingData = [for d in var.triggers: d if d.trigger_type =="MissingData" && var.monitor_monitor_type == "Logs"]

# Metrics Monitor
MetricsCriticalAlert = [for d in var.triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Metrics"]
MetricsResolveCriticalAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedCritical" && var.monitor_monitor_type == "Metrics"]
MetricsWarningAlert = [for d in var.triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Metrics"]
MetricsResolveWarningAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedWarning" && var.monitor_monitor_type == "Metrics"]
MetricsMissingData = [for d in var.triggers: d if d.trigger_type =="MissingData" && var.monitor_monitor_type == "Metrics"]

hasLogsCriticalAlert = (length(local.LogsCriticalAlert) + length(local.LogsResolveCriticalAlert)) == 2
hasLogsWarningAlert = (length(local.LogsWarningAlert) + length(local.LogsResolveWarningAlert)) == 2
hasMetricsCriticalAlert = (length(local.MetricsCriticalAlert) + length(local.MetricsResolveCriticalAlert)) == 2
hasMetricsWarningAlert = (length(local.MetricsWarningAlert)+ length(local.MetricsResolveWarningAlert)) == 2
hasLogsMissingData = length(local.LogsMissingData) == 1
hasMetricsMissingData = length(local.MetricsMissingData) == 1
}

resource "sumologic_monitor" "tf_monitor" {
name = var.monitor_name
parent_id = var.monitor_parent_id
description = var.monitor_description
Expand All @@ -16,17 +39,88 @@ resource "sumologic_monitor" "tf_monitor_1" {
}
}

dynamic "triggers" {
for_each = var.triggers
content {
threshold_type = triggers.value.threshold_type
threshold = triggers.value.threshold
time_range = triggers.value.time_range
occurrence_type = triggers.value.occurrence_type
trigger_source = triggers.value.trigger_source
trigger_type = triggers.value.trigger_type
detection_method = triggers.value.detection_method
}
trigger_conditions {
dynamic "logs_static_condition" {
for_each = toset(var.monitor_monitor_type == "Logs" ? ["1"] : [])
content {
dynamic "critical" {
for_each = local.hasLogsCriticalAlert ? ["1"] : []
content {
time_range = local.LogsCriticalAlert[0].time_range
alert {
threshold = local.LogsCriticalAlert[0].threshold
threshold_type = local.LogsCriticalAlert[0].threshold_type
}
resolution {
threshold = local.LogsResolveCriticalAlert[0].threshold
threshold_type = local.LogsResolveCriticalAlert[0].threshold_type
}
}
}
dynamic "warning" {
for_each = local.hasLogsWarningAlert ? ["1"] : []
content {
time_range = local.LogsWarningAlert[0].time_range
alert {
threshold = local.LogsWarningAlert[0].threshold
threshold_type = local.LogsWarningAlert[0].threshold_type
}
resolution {
threshold = local.LogsResolveWarningAlert[0].threshold
threshold_type = local.LogsResolveWarningAlert[0].threshold_type
}
}
}
}
}
dynamic "logs_missing_data_condition" {
for_each = local.hasLogsMissingData ? ["1"] : []
content {
time_range = local.LogsMissingData[0].time_range
}
}
dynamic "metrics_static_condition" {
for_each = toset(var.monitor_monitor_type == "Metrics" ? ["1"] : [])
content {
dynamic "critical" {
for_each = local.hasMetricsCriticalAlert ? ["1"] : []
content {
time_range = local.MetricsCriticalAlert[0].time_range
occurrence_type = local.MetricsCriticalAlert[0].occurrence_type
alert {
threshold = local.MetricsCriticalAlert[0].threshold
threshold_type = local.MetricsCriticalAlert[0].threshold_type
}
resolution {
threshold = local.MetricsResolveCriticalAlert[0].threshold
threshold_type = local.MetricsResolveCriticalAlert[0].threshold_type
}
}
}
dynamic "warning" {
for_each = local.hasMetricsWarningAlert ? ["1"] : []
content {
time_range = local.MetricsWarningAlert[0].time_range
occurrence_type = local.MetricsCriticalAlert[0].occurrence_type
alert {
threshold = local.MetricsWarningAlert[0].threshold
threshold_type = local.MetricsWarningAlert[0].threshold_type
}
resolution {
threshold = local.MetricsResolveWarningAlert[0].threshold
threshold_type = local.MetricsResolveWarningAlert[0].threshold_type
}
}
}
}
}
dynamic "metrics_missing_data_condition" {
for_each = local.hasMetricsMissingData ? ["1"] : []
content {
time_range = local.MetricsMissingData[0].time_range
trigger_source = local.MetricsMissingData[0].trigger_source
}
}
}

dynamic "notifications" {
Expand Down
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "sumologic_monitor" {
value = sumologic_monitor.tf_monitor_1
value = sumologic_monitor.tf_monitor
description = "This output contains details of the Sumo Logic monitor."
}

0 comments on commit d5bd64b

Please sign in to comment.