From 141680f8b7fef67b8104849a734c01955fe32d61 Mon Sep 17 00:00:00 2001 From: Diego Rabatone Oliveira Date: Fri, 14 Jun 2024 17:56:18 -0300 Subject: [PATCH 1/2] [Existing Resources] Add Team and Schedule --- modules/config/README.md | 18 +++++++++++++++++ modules/config/existing_resources.tf | 30 ++++++++++++++++++++++++++++ modules/config/existing_users.tf | 5 ----- modules/config/main.tf | 2 ++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 modules/config/existing_resources.tf delete mode 100644 modules/config/existing_users.tf diff --git a/modules/config/README.md b/modules/config/README.md index 74892c7..7c76ccf 100644 --- a/modules/config/README.md +++ b/modules/config/README.md @@ -149,6 +149,22 @@ users: ``` +### `existing_schedules.yaml` + +```yaml +existing_schedules: +- acme.default +``` + + +### `existing_teams.yaml` + +```yaml +existing_teams: +- acme +``` + + ### `existing_users.yaml` ```yaml @@ -229,6 +245,8 @@ module "opsgenie" { | `alert_policies` | `name` and `id` of each alert policy | | `api_integrations` | `name` and `id` of each API integration | | `escalations` | `name` and `id` of each escalation | +| `existing_schedules` | `name` and `id` of each existing schedule | +| `existing_teams` | `name` and `id` of each existing team | | `existing_users` | `username` and `id` of each existing user | | `notification_policies` | `name` and `id` of each notification policy | | `schedules` | `name` and `id` of each schedules | diff --git a/modules/config/existing_resources.tf b/modules/config/existing_resources.tf new file mode 100644 index 0000000..381dc62 --- /dev/null +++ b/modules/config/existing_resources.tf @@ -0,0 +1,30 @@ +// local.existing users is a list of key pairs, in which all keys are "username". +// Let's remove duplicated items from the list based on the value of each item. +// Existing schedules and teams are just a plain list of their respective +// resource names. +locals { + distinct_existing_users = distinct([for user in local.existing_users : user.username]) + unique_existing_users = [for u in local.distinct_existing_users : { "username" = u }] + + unique_existing_schedules = distinct(local.existing_schedules) + + unique_existing_teams = distinct(local.existing_teams) +} + +data "opsgenie_user" "this" { + for_each = module.this.enabled ? { for user in local.unique_existing_users : user.username => user } : tomap() + + username = each.value.username +} + +data "opsgenie_schedule" "this" { + for_each = module.this.enabled ? toset(local.unique_existing_schedules) : toset() + + name = each.key +} + +data "opsgenie_team" "this" { + for_each = module.this.enabled ? toset(local.unique_existing_teams) : toset() + + name = each.key +} diff --git a/modules/config/existing_users.tf b/modules/config/existing_users.tf deleted file mode 100644 index 8423ce7..0000000 --- a/modules/config/existing_users.tf +++ /dev/null @@ -1,5 +0,0 @@ -data "opsgenie_user" "this" { - for_each = module.this.enabled ? { for user in local.existing_users : user.username => user } : tomap() - - username = each.value.username -} diff --git a/modules/config/main.tf b/modules/config/main.tf index ca2be44..db9faa6 100644 --- a/modules/config/main.tf +++ b/modules/config/main.tf @@ -5,9 +5,11 @@ locals { integration_actions = lookup(var.opsgenie_resources, "integration_actions", []) notification_policies = lookup(var.opsgenie_resources, "notification_policies", []) schedules = lookup(var.opsgenie_resources, "schedules", []) + existing_schedules = lookup(var.opsgenie_resources, "existing_schedules", []) schedule_rotations = lookup(var.opsgenie_resources, "schedule_rotations", []) team_routing_rules = lookup(var.opsgenie_resources, "team_routing_rules", []) teams = lookup(var.opsgenie_resources, "teams", []) + existing_teams = lookup(var.opsgenie_resources, "existing_teams", []) users = lookup(var.opsgenie_resources, "users", []) existing_users = lookup(var.opsgenie_resources, "existing_users", []) services = lookup(var.opsgenie_resources, "services", []) From eb7251e36d0d8e7ca4e254685bb5d9512b696432 Mon Sep 17 00:00:00 2001 From: Diego Rabatone Oliveira Date: Thu, 25 Jul 2024 15:31:31 -0300 Subject: [PATCH 2/2] Use data objects as fallback --- modules/config/alert_policies.tf | 4 ++-- modules/config/api_integrations.tf | 2 +- modules/config/escalations.tf | 3 +-- modules/config/notification_policies.tf | 2 +- modules/config/schedule_rotations.tf | 2 +- modules/config/schedules.tf | 2 +- modules/config/services.tf | 2 +- modules/config/team_routing_rules.tf | 2 +- 8 files changed, 9 insertions(+), 10 deletions(-) diff --git a/modules/config/alert_policies.tf b/modules/config/alert_policies.tf index bedb018..475d3b6 100644 --- a/modules/config/alert_policies.tf +++ b/modules/config/alert_policies.tf @@ -8,7 +8,7 @@ resource "opsgenie_alert_policy" "this" { alert_description = try(each.value.alert_description, each.value.name) # Look up our team id by name - team_id = try(opsgenie_team.this[each.value.owner_team_name].id, null) + team_id = try(opsgenie_team.this[each.value.owner_team_name].id, data.opsgenie_team.this[each.value.owner_team_name].id, null) enabled = try(each.value.enabled, true) continue_policy = try(each.value.continue, true) @@ -32,7 +32,7 @@ resource "opsgenie_alert_policy" "this" { type = responders.value.type id = lookup(responders.value, "id", null) != null ? responders.value.id : ( - responders.value.type == "team" ? opsgenie_team.this[responders.value.team_name].id : ( + responders.value.type == "team" ? try(opsgenie_team.this[responders.value.team_name].id, data.opsgenie_team.this[responders.value.team_name].id) : ( responders.value.type == "user" ? try(opsgenie_user.this[responders.value.user_name].id, data.opsgenie_user.this[responders.value.user_name].id) : ( responders.value.type == "escalation" ? opsgenie_escalation.this[responders.value.escalation_name].id : ( null diff --git a/modules/config/api_integrations.tf b/modules/config/api_integrations.tf index 8af93c2..0957b41 100644 --- a/modules/config/api_integrations.tf +++ b/modules/config/api_integrations.tf @@ -20,5 +20,5 @@ resource "opsgenie_api_integration" "this" { webhook_url = try(each.value.webhook_url, null) # Look up our team id by name - owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, null) + owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, data.opsgenie_team.this[each.value.owner_team_name].id, null) } diff --git a/modules/config/escalations.tf b/modules/config/escalations.tf index 3b84515..eaa6665 100644 --- a/modules/config/escalations.tf +++ b/modules/config/escalations.tf @@ -5,7 +5,7 @@ resource "opsgenie_escalation" "this" { description = try(each.value.description, each.value.name) # Look up our team id by name - owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, null) + owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, data.opsgenie_team.this[each.value.owner_team_name].id, null) dynamic "rules" { for_each = each.value.rules @@ -17,7 +17,6 @@ resource "opsgenie_escalation" "this" { recipient { type = rules.value.recipient.type - 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) : ( diff --git a/modules/config/notification_policies.tf b/modules/config/notification_policies.tf index 3dfff35..a72b9e4 100644 --- a/modules/config/notification_policies.tf +++ b/modules/config/notification_policies.tf @@ -5,7 +5,7 @@ resource "opsgenie_notification_policy" "this" { name = each.key # Look up our team id by name - team_id = opsgenie_team.this[each.value.team_name].id + team_id = try(opsgenie_team.this[each.value.team_name].id, data.opsgenie_team.this[each.value.team_name].id) policy_description = try(each.value.description, each.value.name) filter { diff --git a/modules/config/schedule_rotations.tf b/modules/config/schedule_rotations.tf index 1e68a23..8ea84e3 100644 --- a/modules/config/schedule_rotations.tf +++ b/modules/config/schedule_rotations.tf @@ -9,7 +9,7 @@ resource "opsgenie_schedule_rotation" "this" { length = try(each.value.length, null) # Look up our schedule id by name - schedule_id = try(opsgenie_schedule.this[each.value.schedule_name].id, null) + schedule_id = try(opsgenie_schedule.this[each.value.schedule_name].id, data.opsgenie_schedule.this[each.value.schedule_name].id, null) dynamic "participant" { for_each = try(each.value.participants, []) diff --git a/modules/config/schedules.tf b/modules/config/schedules.tf index 60593fa..5516887 100644 --- a/modules/config/schedules.tf +++ b/modules/config/schedules.tf @@ -8,5 +8,5 @@ resource "opsgenie_schedule" "this" { timezone = try(each.value.timezone, each.value.name) # Look up our team id by name - owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, null) + owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, data.opsgenie_team.this[each.value.owner_team_name].id, null) } diff --git a/modules/config/services.tf b/modules/config/services.tf index 73a5096..3501457 100644 --- a/modules/config/services.tf +++ b/modules/config/services.tf @@ -2,6 +2,6 @@ resource "opsgenie_service" "this" { for_each = module.this.enabled ? { for service in local.services : service.name => service } : tomap() name = each.value.name - team_id = opsgenie_team.this[each.value.team_name].id + team_id = try(opsgenie_team.this[each.value.team_name].id, data.opsgenie_team.this[each.value.team_name].id) description = try(each.value.description, null) } diff --git a/modules/config/team_routing_rules.tf b/modules/config/team_routing_rules.tf index 194f8ad..76a9e2e 100644 --- a/modules/config/team_routing_rules.tf +++ b/modules/config/team_routing_rules.tf @@ -6,7 +6,7 @@ resource "opsgenie_team_routing_rule" "this" { is_default = try(each.value.is_default, false) # Look up Team ID by name - team_id = opsgenie_team.this[each.value.owner_team_name].id + team_id = try(opsgenie_team.this[each.value.owner_team_name].id, data.opsgenie_team.this[each.value.owner_team_name].id) order = try(each.value.order, 0) timezone = try(each.value.timezone, "America/Los_Angeles")