Skip to content

Commit

Permalink
[notification_policy] Improvements
Browse files Browse the repository at this point in the history
Major improvements on the notification_policy module (and its
counterpart on config module) with more fields and some refactoring
  • Loading branch information
diraol committed Jun 14, 2024
1 parent 72aaca6 commit 017aeff
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 53 deletions.
108 changes: 99 additions & 9 deletions modules/config/notification_policies.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resource "opsgenie_notification_policy" "this" {
for_each = module.this.enabled ? { for policy in local.notification_policies : policy.name => policy } : tomap()

enabled = try(each.value.enabled, true)
name = each.value.name
name = each.key

# Look up our team id by name
team_id = opsgenie_team.this[each.value.team_name].id
Expand All @@ -15,20 +15,110 @@ resource "opsgenie_notification_policy" "this" {
for_each = try(each.value.filter.conditions, [])

content {
expected_value = try(conditions.value.expected_value, null)
field = try(conditions.value.field, null)
field = conditions.value.field
operation = conditions.value.operation
expected_value = try(conditions.value.expected_value, "")
key = try(conditions.value.key, null)
not = try(conditions.value.not, null)
operation = try(conditions.value.operation, null)
not = try(conditions.value.not, false)
order = try(conditions.value.order, null)
}
}
}

auto_close_action {
duration {
time_amount = try(each.value.auto_close_action.time_amount, null)
time_unit = try(each.value.auto_close_action.time_unit, "minutes")
dynamic "time_restriction" {
for_each = try(each.value.time_restriction, null) != null ? [each.value.time_restriction] : []

content {
type = time_restriction.value.type

dynamic "restrictions" {
for_each = try(time_restriction.value.restrictions, null) != null ? [time_restriction.value.restrictions] : []

content {
start_day = restrictions.value.start_day
end_day = restrictions.value.end_day
start_hour = restrictions.value.start_hour
end_hour = restrictions.value.end_hour
start_min = restrictions.value.start_min
end_min = restrictions.value.end_min
}
}

dynamic "restriction" {
for_each = try(time_restriction.value.restriction, null) != null ? [time_restriction.value.restriction] : []

content {
start_hour = restriction.value.start_hour
end_hour = restriction.value.end_hour
start_min = restriction.value.start_min
end_min = restriction.value.end_min
}
}
}
}

dynamic "auto_close_action" {
for_each = try(each.value.auto_close_action, null) != null ? [each.value.auto_close_action] : []

content {
duration {
time_amount = auto_close_action.value.duration.time_amount
time_unit = try(auto_close_action.value.duration.time_unit, "minutes")
}
}

}

dynamic "auto_restart_action" {
for_each = try(each.value.auto_restart_action, null) != null ? [each.value.auto_restart_action] : []

content {
duration {
time_amount = auto_restart_action.value.duration.time_amount
time_unit = try(auto_restart_action.value.duration.time_unit, "minutes")
}
max_repeat_count = auto_restart_action.value.max_repeat_count
}

}

dynamic "de_duplication_action" {
for_each = try(each.value.de_duplication_action, null) != null ? [each.value.de_duplication_action] : []

content {
de_duplication_action_type = de_duplication_action.value.de_duplication_action_type
count = de_duplication_action.value.count

dynamic "duration" {
for_each = de_duplication_action.value.de_duplication_action_type == "frequency-based" ? [de_duplication_action.value.duration] : [try(de_duplication_action.value.duration, null)]

content {
time_amount = duration.value.time_amount
time_unit = try(duration.value.time_unit, "minutes")
}
}
}
}

dynamic "delay_action" {
for_each = try(each.value.delay_action, null) != null ? [each.value.delay_action] : []

content {
delay_option = delay_action.value.delay_option

dynamic "duration" {
for_each = delay_action.value.delay_option == "for-duration" ? [delay_action.value.duration] : []

content {
time_amount = duration.value.time_amount
time_unit = try(duration.value.time_unit, "minutes")
}
}

until_hour = delay_action.value.delay_option != "for-duration" ? delay_action.value.until_hour : null
until_minute = delay_action.value.delay_option != "for-duration" ? delay_action.value.until_minute : null
}
}

suppress = try(each.value.suppress, null)
}
117 changes: 73 additions & 44 deletions modules/notification_policy/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
resource "opsgenie_notification_policy" "this" {
count = module.this.enabled ? 1 : 0

enabled = try(var.notification_policy.enabled, true)
name = var.notification_policy.name
enabled = try(var.notification_policy.enabled, true)
name = var.notification_policy.name

# Look up our team id by name
team_id = var.notification_policy.team_id
policy_description = try(var.notification_policy.description, var.notification_policy.name)

Expand All @@ -13,83 +15,110 @@ resource "opsgenie_notification_policy" "this" {
for_each = try(var.notification_policy.filter.conditions, [])

content {
expected_value = try(conditions.value.expected_value, null)
field = try(conditions.value.field, null)
field = conditions.value.field
operation = conditions.value.operation
expected_value = try(conditions.value.expected_value, "")
key = try(conditions.value.key, null)
not = try(conditions.value.not, null)
operation = try(conditions.value.operation, null)
not = try(conditions.value.not, false)
order = try(conditions.value.order, null)
}
}
}

time_restriction {
type = try(var.notification_policy.time_restriction.type, null)
dynamic "time_restriction" {
for_each = try(var.notification_policy.time_restriction, null) != null ? [var.notification_policy.time_restriction] : []

dynamic "restrictions" {
for_each = try(var.notification_policy.filter.conditions, [])
content {
type = time_restriction.value.type

content {
start_day = try(conditions.value.start_day, null)
end_day = try(conditions.value.end_day, null)
start_hour = try(conditions.value.start_hour, null)
end_hour = try(conditions.value.end_hour, null)
start_min = try(conditions.value.start_min, null)
end_min = try(conditions.value.end_min, null)
dynamic "restrictions" {
for_each = try(time_restriction.value.restrictions, null) != null ? [time_restriction.value.restrictions] : []

content {
start_day = restrictions.value.start_day
end_day = restrictions.value.end_day
start_hour = restrictions.value.start_hour
end_hour = restrictions.value.end_hour
start_min = restrictions.value.start_min
end_min = restrictions.value.end_min
}
}

dynamic "restriction" {
for_each = try(time_restriction.value.restriction, null) != null ? [time_restriction.value.restriction] : []

content {
start_hour = restriction.value.start_hour
end_hour = restriction.value.end_hour
start_min = restriction.value.start_min
end_min = restriction.value.end_min
}
}
}
}

dynamic "auto_close_action" {
for_each = try(each.value.auto_close_action, null) != null ? [each.value.auto_close_action] : []

content {
duration {
time_amount = auto_close_action.value.duration.time_amount
time_unit = try(auto_close_action.value.duration.time_unit, "minutes")
}
}

}

dynamic "auto_restart_action" {
for_each = try(each.value.auto_restart_action, null) != null ? [each.value.auto_restart_action] : []

content {
duration {
time_amount = auto_restart_action.value.duration.time_amount
time_unit = try(auto_restart_action.value.duration.time_unit, "minutes")
}
max_repeat_count = auto_restart_action.value.max_repeat_count
}

}

dynamic "de_duplication_action" {
for_each = try(var.notification_policy.de_duplication_action, null) != null ? ["true"] : []
for_each = try(var.notification_policy.de_duplication_action, null) != null ? [var.notification_policy.de_duplication_action] : []

content {
de_duplication_action_type = var.notification_policy.de_duplication_action.de_duplication_action_type
count = var.notification_policy.de_duplication_action.count
de_duplication_action_type = de_duplication_action.value.de_duplication_action_type
count = de_duplication_action.value.count

dynamic "duration" {
for_each = var.notification_policy.de_duplication_action.de_duplication_action_type == "frequency-based" ? ["true"] : [try(var.notification_policy.de_duplication_action.duration, null)]
for_each = de_duplication_action.value.de_duplication_action_type == "frequency-based" ? [de_duplication_action.value.duration] : [try(de_duplication_action.value.duration, null)]

content {
time_amount = var.notification_policy.de_duplication_action.duration.time_amount
time_unit = try(var.notification_policy.de_duplication_action.duration.time_unit, null)
time_amount = duration.value.time_amount
time_unit = try(duration.value.time_unit, "minutes")
}
}

until_hour = var.notification_policy.delay_action.delay_option != "for-duration" ? var.notification_policy.delay_action.until_hour : null
until_minute = var.notification_policy.delay_action.delay_option != "for-duration" ? var.notification_policy.delay_action.until_minute : null
}
}

dynamic "delay_action" {
for_each = try(var.notification_policy.delay_action, null) != null ? ["true"] : []
for_each = try(var.notification_policy.delay_action, null) != null ? [var.notification_policy.delay_action] : []

content {
delay_option = var.notification_policy.delay_action.delay_option
delay_option = delay_action.value.delay_option

dynamic "duration" {
for_each = var.notification_policy.delay_action.delay_option == "for-duration" ? ["true"] : []
for_each = delay_action.value.delay_option == "for-duration" ? [delay_action.value.duration] : []

content {
time_amount = var.notification_policy.delay_action.duration.time_amount
time_unit = try(var.notification_policy.delay_action.duration.time_unit, null)
time_amount = duration.value.time_amount
time_unit = try(duration.value.time_unit, "minutes")
}
}

until_hour = var.notification_policy.delay_action.delay_option != "for-duration" ? var.notification_policy.delay_action.until_hour : null
until_minute = var.notification_policy.delay_action.delay_option != "for-duration" ? var.notification_policy.delay_action.until_minute : null
until_hour = delay_action.value.delay_option != "for-duration" ? delay_action.value.until_hour : null
until_minute = delay_action.value.delay_option != "for-duration" ? delay_action.value.until_minute : null
}
}

dynamic "auto_close_action" {
for_each = try(var.notification_policy.auto_close_action, null) != null ? ["true"] : []

content {
duration {
time_amount = var.notification_policy.auto_close_action.time_amount
time_unit = var.notification_policy.auto_close_action.time_unit
}
}

}
suppress = try(each.value.suppress, null)
}

0 comments on commit 017aeff

Please sign in to comment.