Skip to content

Commit

Permalink
Various fixes and improvements (#11)
Browse files Browse the repository at this point in the history
* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config

* Update config
  • Loading branch information
aknysh authored Oct 18, 2020
1 parent 11be84f commit 2e81a07
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 30 deletions.
4 changes: 3 additions & 1 deletion examples/config/resources/service_incident_rules.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
service_incident_rules:
- service_name: frontend
- name: frontend-service-incident-rule-1
service_name: frontend

incident_rule:
condition_match_type: match-all

Expand Down
21 changes: 16 additions & 5 deletions modules/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ users:
timezone: "America/New_York"
```


### `existing_users.yaml`

```yaml
existing_users:
- username: [email protected]
```


### `services.yaml`

```yaml
Expand All @@ -126,7 +135,8 @@ services:

```yaml
service_incident_rules:
- service_name: frontend
- name: frontend-service-incident-rule-1
service_name: frontend
incident_rule:
condition_match_type: match-all
Expand Down Expand Up @@ -170,9 +180,9 @@ module "opsgenie" {

## Inputs

| Name | Default | Description | Required |
|:-------------------------------|:---------------------------------:|:--------------------------------------------------------------------------------------------------------------------------------|:--------:|
| `opsgenie_resources` | `{}` | A map generated by sourcing YAML resource definitions (see above). | Yes |
| Name | Default | Description | Required |
|:-------------------------------|:----------------:|:-------------------------------------------------------------------------------|:--------:|
| `opsgenie_resources` | `{}` | A map generated by sourcing YAML resource definitions (see above). | Yes |


## Outputs
Expand All @@ -185,7 +195,8 @@ module "opsgenie" {
| `notification_policies` | `name` and `id` of each notification policy |
| `team_routing_rules` | `name` and `id` of each team routing rule |
| `teams` | `name` and `id` of each team |
| `users` | `name` and `id` of each user |
| `users` | `username` and `id` of each user |
| `existing_users` | `username` and `id` of each existing user |
| `services` | `name` and `id` of each service |
| `services` | `name` and `id` of each service |
| `service_incident_rule_ids` | `id` of each service incident rule |
18 changes: 15 additions & 3 deletions modules/config/alert_policies.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# https://docs.opsgenie.com/docs/alert-api

resource "opsgenie_alert_policy" "this" {
for_each = {
for policy in local.alert_policies : policy.name => policy
Expand All @@ -14,7 +16,7 @@ resource "opsgenie_alert_policy" "this" {

alias = try(each.value.alias, null)
entity = try(each.value.entity, null)
message = try(each.value.message, "{{ message }}")
message = try(each.value.message, "{{message}}")
priority = try(each.value.priority, null)
source = try(each.value.source, null)
tags = try(each.value.tags, null)
Expand All @@ -28,9 +30,19 @@ resource "opsgenie_alert_policy" "this" {
for_each = try(each.value.responders, [])

content {
id = try(responders.value.id, null)
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 == "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
)
)
)
)

name = try(responders.value.name, null)
type = responders.value.type
username = try(responders.value.username, 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 @@ -21,5 +21,5 @@ resource "opsgenie_api_integration" "this" {
suppress_notifications = try(each.value.suppress_notifications, false)

# Look up our team id by name
owner_team_id = opsgenie_team.this[each.value.owner_team_name].id
owner_team_id = try(opsgenie_team.this[each.value.owner_team_name].id, null)
}
21 changes: 19 additions & 2 deletions modules/config/escalations.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@ resource "opsgenie_escalation" "this" {
for_each = try(each.value.rule.recipients, [])

content {
# Look up our team id by name
id = opsgenie_team.this[recipient.value.team_name].id
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) : (
null
)
)
)
}
}
}

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

content {
wait_interval = repeat.value.wait_interval
count = repeat.value.count
reset_recipient_states = repeat.value.reset_recipient_states
close_alert_after_all = repeat.value.close_alert_after_all
}
}
}
7 changes: 7 additions & 0 deletions modules/config/existing_users.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "opsgenie_user" "this" {
for_each = {
for user in local.existing_users : user.username => user
}

username = each.value.username
}
19 changes: 10 additions & 9 deletions modules/config/main.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
locals {
alert_policies = try(var.opsgenie_resources.alert_policies, [])
api_integrations = try(var.opsgenie_resources.api_integrations, [])
escalations = try(var.opsgenie_resources.escalations, [])
notification_policies = try(var.opsgenie_resources.notification_policies, [])
team_routing_rules = try(var.opsgenie_resources.team_routing_rules, [])
teams = try(var.opsgenie_resources.teams, [])
users = try(var.opsgenie_resources.users, [])
services = try(var.opsgenie_resources.services, [])
service_incident_rules = try(var.opsgenie_resources.service_incident_rules, [])
alert_policies = lookup(var.opsgenie_resources, "alert_policies", [])
api_integrations = lookup(var.opsgenie_resources, "api_integrations", [])
escalations = lookup(var.opsgenie_resources, "escalations", [])
notification_policies = lookup(var.opsgenie_resources, "notification_policies", [])
team_routing_rules = lookup(var.opsgenie_resources, "team_routing_rules", [])
teams = lookup(var.opsgenie_resources, "teams", [])
users = lookup(var.opsgenie_resources, "users", [])
existing_users = lookup(var.opsgenie_resources, "existing_users", [])
services = lookup(var.opsgenie_resources, "services", [])
service_incident_rules = lookup(var.opsgenie_resources, "service_incident_rules", [])
}
2 changes: 1 addition & 1 deletion modules/config/notification_policies.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ resource "opsgenie_notification_policy" "this" {
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, null)
time_unit = try(each.value.auto_close_action.time_unit, "minutes")
}
}
}
7 changes: 7 additions & 0 deletions modules/config/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ output "users" {
description = "Users"
}

output "existing_users" {
value = {
for user in data.opsgenie_user.this : user.id => user.username
}
description = "Users that already exist in Opsgenie"
}

output "services" {
value = {
for service in opsgenie_service.this : service.id => service.name
Expand Down
4 changes: 3 additions & 1 deletion modules/config/service_incident_rules.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
resource "opsgenie_service_incident_rule" "this" {
for_each = local.service_incident_rules
for_each = {
for service_incident_rule in local.service_incident_rules : service_incident_rule.name => service_incident_rule
}

service_id = opsgenie_service.this[each.value.service_name].id

Expand Down
9 changes: 5 additions & 4 deletions modules/config/team_routing_rules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ resource "opsgenie_team_routing_rule" "this" {

name = each.value.name

# Look up our team id by name
team_id = opsgenie_team.this[each.value.owner_team_name].id
# Look up Team ID by name
team_id = opsgenie_team.this[each.value.owner_team_name].id

order = try(each.value.order, 0)
timezone = try(each.value.timezone, "America/Los_Angeles")

Expand All @@ -22,6 +23,7 @@ resource "opsgenie_team_routing_rule" "this" {
key = try(conditions.value.key, null)
not = try(conditions.value.not, null)
operation = try(conditions.value.operation, null)
order = try(conditions.value.order, null)
}
}
}
Expand All @@ -30,9 +32,8 @@ resource "opsgenie_team_routing_rule" "this" {
for_each = try(each.value.notify, [])

content {
# Look up our escalation id by name
id = opsgenie_escalation.this[notify.value.name].id
type = notify.value.type
id = opsgenie_escalation.this[notify.value.name].id
}
}
}
4 changes: 2 additions & 2 deletions modules/config/teams.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ resource "opsgenie_team" "this" {
for_each = try(each.value.members, [])

content {
id = opsgenie_user.this[member.value.username].id
role = member.value.role
id = try(opsgenie_user.this[member.value.username].id, data.opsgenie_user.this[member.value.username].id)
role = try(member.value.role, null)
}
}
}
3 changes: 2 additions & 1 deletion modules/team_routing_rule/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ resource "opsgenie_team_routing_rule" "this" {
key = try(conditions.value.key, null)
not = try(conditions.value.not, null)
operation = try(conditions.value.operation, null)
order = try(conditions.value.order, null)
}
}
}
Expand All @@ -24,10 +25,10 @@ resource "opsgenie_team_routing_rule" "this" {
for_each = try(var.team_routing_rule.notify, [])

content {
type = notify.value.type
# name and id parameters are mutually exclusive
id = try(notify.value.id, null)
name = try(notify.value.name, null)
type = notify.value.type
}
}
}

0 comments on commit 2e81a07

Please sign in to comment.