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

Reference:
https://registry.terraform.io/providers/opsgenie/opsgenie/latest/docs/resources/notification_policy#delay_action
  • Loading branch information
diraol committed Jun 14, 2024
1 parent 83362b5 commit 10b4578
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 20 deletions.
12 changes: 12 additions & 0 deletions examples/config/resources/notification_policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ notification_policies:
- field: priority
operation: less-than
expected_value: P3
de_duplication_action:
de_duplication_action_type: frequency-based
count: 2
duration:
time_unit: minutes
time_amount: 5

delay_action:
delay_option: for-duration
duration:
time_unit: minutes
time_amount: 5
17 changes: 17 additions & 0 deletions examples/notification_policy/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ module "notification_policy" {
}]
}

de_duplication_action = {
de_duplication_action_type = "frequency-based"
count = 2
duration = {
time_unit = "minutes"
time_amount = 5
}
}

delay_action = {
delay_option = "for-duration"
duration = {
time_unit = "minutes"
time_amount = 10
}
}

auto_close_action = {
time_unit = "minutes"
time_amount = 5
Expand Down
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)
}
19 changes: 18 additions & 1 deletion modules/notification_policy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ module "notification_policy" {
}]
}
de_duplication_action = {
de_duplication_action_type = "frequency-based"
count = 2
duration = {
time_unit = "minutes"
time_amount = 5
}
}
delay_action = {
delay_option = "for-duration"
duration = {
time_unit = "minutes"
time_amount = 10
}
}
auto_close_action = {
time_unit = "minutes"
time_amount = 5
Expand All @@ -36,7 +53,7 @@ module "notification_policy" {

## Inputs

**Note:** `notification_policy` is a map for two reasons:
**Note:** `notification_policy` is a map for two reasons:
- to be able to put whole configuration in yaml file
- variables defined with type set are not robust enough (can't set default values)

Expand Down
112 changes: 102 additions & 10 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,20 +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)
}
}
}

auto_close_action {
duration {
time_amount = try(var.notification_policy.auto_close_action.time_amount, null)
time_unit = try(var.notification_policy.auto_close_action.time_unit, null)
dynamic "time_restriction" {
for_each = try(var.notification_policy.time_restriction, null) != null ? [var.notification_policy.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(var.notification_policy.de_duplication_action, null) != null ? [var.notification_policy.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(var.notification_policy.delay_action, null) != null ? [var.notification_policy.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)
}

0 comments on commit 10b4578

Please sign in to comment.