Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[escalation] Single recipient per rule #58

Merged
merged 8 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/complete/escalation.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ module "escalation" {
name = "${module.this.id}-escalation"
owner_team_id = module.team.team_id

rule = {
recipients = [{
rules = [{
recipient = {
type = "team"
id = module.sub_team.team_id
}]
}
}
}]
}

context = module.this.context
Expand Down
7 changes: 3 additions & 4 deletions examples/config/resources/escalations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ escalations:
- name: acme.dev.some-service-escalation
description: "repo: https://github.com/acme/some-service;owner:David Lightman @David Lightman"
owner_team_name: acme.dev
# please note - config uses `rules` but escalation modules uses `rule`
rules:
condition: if-not-acked
- condition: if-not-acked
notify_type: default
delay: 0
recipients:
- type: team
recipient:
type: team
team_name: acme.dev.some-service
9 changes: 5 additions & 4 deletions examples/escalation/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ module "escalation" {
name = module.this.id
owner_team_id = module.owner_team.team_id

rule = {
recipients = [{
diraol marked this conversation as resolved.
Show resolved Hide resolved
rules = [{
diraol marked this conversation as resolved.
Show resolved Hide resolved
delay = 0
recipient = {
type = "team"
id = module.escalation_team.team_id
}]
}
}
}]
}

context = module.this.context
Expand Down
16 changes: 7 additions & 9 deletions examples/team_routing_rule/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ module "escalation" {
source = "../../modules/escalation"

escalation = {
name = "escalation"
name = module.this.id
owner_team_id = module.owner_team.team_id

rule = {
recipients = [
{
type = "team"
id = module.escalation_team.team_id
}
]
}
rules = [{
recipient = {
type = "team"
id = module.escalation_team.team_id
}
}]
}

context = module.this.context
Expand Down
14 changes: 7 additions & 7 deletions modules/config/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## Config

Terraform module that configures a multitude of [Opsgenie resources](https://registry.terraform.io/providers/opsgenie/opsgenie/latest/docs).
Terraform module that configures a multitude of [Opsgenie resources](https://registry.terraform.io/providers/opsgenie/opsgenie/latest/docs).
Many resources have cross-resource dependencies, which may be simpler to handle within a single module in certain cases, such as using YAML configurations.

This module is designed to accept an input configuration map.
One nice way of handling this is by passing resource definitions from a YAML configuration file.
This module is designed to accept an input configuration map.
One nice way of handling this is by passing resource definitions from a YAML configuration file.

See below for details & examples.

Expand Down Expand Up @@ -46,12 +46,12 @@ escalations:
- name: acme.dev.some-service-escalation
description: "repo: https://github.com/acme/some-service;owner:David Lightman @David Lightman"
owner_team_name: acme.dev
rule:
condition: if-not-acked
rules:
- condition: if-not-acked
notify_type: default
delay: 0
recipients:
- type: team
recipient:
diraol marked this conversation as resolved.
Show resolved Hide resolved
type: team
team_name: acme.dev.some-service
```

Expand Down
22 changes: 9 additions & 13 deletions modules/config/escalations.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@ resource "opsgenie_escalation" "this" {
owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, null)

dynamic "rules" {
for_each = try([each.value.rules], [])
for_each = each.value.rules

content {
condition = try(rules.value.condition, "if-not-acked")
notify_type = try(rules.value.notify_type, "default")
delay = try(rules.value.delay, 0)

dynamic "recipient" {
for_each = try(rules.value.recipients, [])
recipient {
diraol marked this conversation as resolved.
Show resolved Hide resolved
type = rules.value.recipient.type

content {
type = recipient.value.type

id = lookup(recipient.value, "id", null) != null ? recipient.value.id : (
recipient.value.type == "team" ? opsgenie_team.this[recipient.value.team_name].id : (
recipient.value.type == "user" ? try(opsgenie_user.this[recipient.value.user_name].id, data.opsgenie_user.this[recipient.value.user_name].id) : (
recipient.value.type == "schedule" ? opsgenie_schedule.this[recipient.value.schedule_name].id : (
null
)
id = try(rules.value.recipient.id, null) != null ? rules.value.recipient.id : (
rules.value.recipient.type == "team" ? try(opsgenie_team.this[rules.value.recipient.team_name].id, data.opsgenie_team.this[rules.value.recipient.team_name].id) : (
rules.value.recipient.type == "user" ? try(opsgenie_user.this[rules.value.recipient.user_name].id, data.opsgenie_user.this[rules.value.recipient.user_name].id) : (
rules.value.recipient.type == "schedule" ? try(opsgenie_schedule.this[rules.value.recipient.schedule_name].id, data.opsgenie_schedule.this[rules.value.recipient.schedule_name].id) : (
null
)
)
)
}
)
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions modules/escalation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ module "escalation" {
name = module.label.id
owner_team_id = module.owner_team.team_id

rule = {
recipients = [{
type = "team"
id = module.escalation_team.team_id
}]
}
rules = [{
recipient = {
type = "team"
id = module.escalation_team.team_id
}
}]
}

}
Expand All @@ -31,7 +31,7 @@ module "escalation" {

## Inputs

**Note:** `escalation` is a map for two reasons:
**Note:** `escalation` 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
26 changes: 13 additions & 13 deletions modules/escalation/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ resource "opsgenie_escalation" "this" {
description = try(var.escalation.description, var.escalation.name)
owner_team_id = try(var.escalation.owner_team_id, null)

rules {
condition = try(var.escalation.rule.condition, "if-not-acked")
notify_type = try(var.escalation.rule.notify_type, "default")
delay = try(var.escalation.rule.delay, 0)
dynamic "rules" {
for_each = var.escalation.rules

dynamic "recipient" {
for_each = try(var.escalation.rule.recipients, [])
content {
condition = try(rules.value.condition, "if-not-acked")
notify_type = try(rules.value.notify_type, "default")
delay = try(rules.value.delay, 0)

content {
id = recipient.value.id
type = recipient.value.type
recipient {
type = rules.value.recipient.type
id = try(rules.value.recipient.id, null)
}
}
}
Expand All @@ -24,10 +24,10 @@ resource "opsgenie_escalation" "this" {
for_each = try(var.escalation.repeat, null) != null ? ["true"] : []

content {
wait_interval = lookup(var.escalation.repeat, "wait_interval", 5)
count = lookup(var.escalation.repeat, "count", 0)
reset_recipient_states = lookup(var.escalation.repeat, "reset_recipient_states", true)
close_alert_after_all = lookup(var.escalation.repeat, "close_alert_after_all", true)
wait_interval = try(var.escalation.repeat.wait_interval, 5)
count = try(var.escalation.repeat.count, 0)
reset_recipient_states = try(var.escalation.repeat.reset_recipient_states, true)
close_alert_after_all = try(var.escalation.repeat.close_alert_after_all, true)
}
}
}