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

[Existing Resources] Add Team and Schedule #60

Merged
merged 2 commits into from
Jul 26, 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
18 changes: 18 additions & 0 deletions modules/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ users:
```


### `existing_schedules.yaml`

```yaml
existing_schedules:
- acme.default
```


### `existing_teams.yaml`

```yaml
existing_teams:
- acme
```


### `existing_users.yaml`

```yaml
Expand Down Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions modules/config/alert_policies.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/config/api_integrations.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions modules/config/escalations.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) : (
Expand Down
30 changes: 30 additions & 0 deletions modules/config/existing_resources.tf
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 0 additions & 5 deletions modules/config/existing_users.tf

This file was deleted.

2 changes: 2 additions & 0 deletions modules/config/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
Expand Down
2 changes: 1 addition & 1 deletion modules/config/notification_policies.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/config/schedule_rotations.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
Expand Down
2 changes: 1 addition & 1 deletion modules/config/schedules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion modules/config/services.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion modules/config/team_routing_rules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down