|
| 1 | +#Copyright (c) 2023 Oracle Corporation and/or its affiliates. |
| 2 | +#Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl |
| 3 | + |
| 4 | +resource "oci_ons_notification_topic" "this" { |
| 5 | + for_each = { for k, v in var.notification : k => v if v.create_topic == true } |
| 6 | + compartment_id = var.compartment_ocid |
| 7 | + name = var.label_prefix == "none" ? each.key : format("%s_%s", var.label_prefix, each.key) |
| 8 | + |
| 9 | + description = each.value.description == null ? format("%s%s", each.key, " topic created by Terraform") : each.value.description |
| 10 | + defined_tags = each.value.defined_tags |
| 11 | + freeform_tags = each.value.freeform_tags |
| 12 | + |
| 13 | +} |
| 14 | + |
| 15 | +data "oci_ons_notification_topics" "existing_topic" { |
| 16 | + for_each = { for k, v in var.notification : k => v if v.create_topic == false } |
| 17 | + |
| 18 | + compartment_id = var.compartment_ocid |
| 19 | + |
| 20 | + name = each.key |
| 21 | + state = "ACTIVE" |
| 22 | +} |
| 23 | + |
| 24 | +resource "oci_ons_subscription" "this" { |
| 25 | + for_each = { for v in local.notification_subscription : v.subscription => v } |
| 26 | + compartment_id = var.compartment_ocid |
| 27 | + endpoint = each.value.endpoint |
| 28 | + protocol = each.value.protocol |
| 29 | + topic_id = each.value.topic_id |
| 30 | + |
| 31 | + |
| 32 | + defined_tags = each.value.defined_tags |
| 33 | + freeform_tags = each.value.freeform_tags |
| 34 | +} |
| 35 | + |
| 36 | +resource "oci_monitoring_alarm" "this" { |
| 37 | + for_each = length(var.alarm_def) > 0 ? var.alarm_def : {} |
| 38 | + compartment_id = var.compartment_ocid |
| 39 | + destinations = [try(oci_ons_notification_topic.this[each.value.destination].id, data.oci_ons_notification_topics.existing_topic[each.value.destination].notification_topics[0].topic_id, each.value.destination)] |
| 40 | + display_name = var.label_prefix == "none" ? each.key : format("%s_%s", var.label_prefix, each.key) |
| 41 | + is_enabled = each.value.is_enabled |
| 42 | + metric_compartment_id = each.value.metric_compartment_id == null ? var.compartment_ocid : each.value.metric_compartment |
| 43 | + namespace = each.value.namespace |
| 44 | + query = each.value.query |
| 45 | + severity = each.value.severity |
| 46 | + message_format = each.value.message_format |
| 47 | + repeat_notification_duration = each.value.repeat_notification_duration |
| 48 | + pending_duration = each.value.trigger |
| 49 | + body = each.value.body |
| 50 | + defined_tags = each.value.defined_tags |
| 51 | + freeform_tags = each.value.freeform_tags |
| 52 | + dynamic "suppression" { |
| 53 | + for_each = (each.value.suppression_from_time != null && each.value.suppression_till_time != null) ? [1] : [] |
| 54 | + content { |
| 55 | + time_suppress_from = each.value.suppression_from_time |
| 56 | + time_suppress_until = each.value.suppression_till_time |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +locals { |
| 64 | + notification_subscription = flatten([ |
| 65 | + for topic_key, topic_value in var.notification : [ |
| 66 | + for subscription_key, subscription_value in topic_value.subscription : { |
| 67 | + topic_id = topic_value.create_topic ? oci_ons_notification_topic.this[topic_key].id : data.oci_ons_notification_topics.existing_topic[topic_key].notification_topics[0].topic_id |
| 68 | + protocol = subscription_value.protocol |
| 69 | + endpoint = subscription_value.endpoint |
| 70 | + subscription = format("%s_%s", topic_key, subscription_key) |
| 71 | + defined_tags = topic_value.defined_tags |
| 72 | + freeform_tags = topic_value.freeform_tags |
| 73 | + } |
| 74 | + ] |
| 75 | + ]) |
| 76 | +} |
0 commit comments