From c1386f3b94cb68b58c3d828e29daf51df354d595 Mon Sep 17 00:00:00 2001 From: Anderson Deluiz Date: Tue, 23 Apr 2024 17:10:13 -0300 Subject: [PATCH] Module alarm modifications * Removed default values for variables in optional() * Added support for overrides argument for resource type oci_monitoring_alarm * Added support for different prefix names for alarms and topics * Fixed display name for topics --- modules/alarm/README.md | 11 +++++----- modules/alarm/main.tf | 42 +++++++++++++++++++++++++++++++------- modules/alarm/variables.tf | 41 +++++++++++++++++++++++++------------ modules/alarm/versions.tf | 5 ++++- 4 files changed, 72 insertions(+), 27 deletions(-) diff --git a/modules/alarm/README.md b/modules/alarm/README.md index b74902e..abe38c5 100644 --- a/modules/alarm/README.md +++ b/modules/alarm/README.md @@ -1,9 +1,8 @@ - ## Requirements | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.3.0 | +| [terraform](#requirement\_terraform) | >= 1.2.0 | | [oci](#requirement\_oci) | >= 4.67.3 | ## Providers @@ -29,14 +28,14 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [alarm\_def](#input\_alarm\_def) | OCI Alarm definition |
map(object({
destination = string
display_name = string
severity = optional(string, "CRITICAL")
query = string
is_enabled = optional(bool, true)
namespace = string
metric_compartment_id = optional(string)
repeat_notification_duration = optional(string, "PT5M")
trigger = optional(string, "PT5M")
suppression_from_time = optional(string)
suppression_till_time = optional(string)
message_format = optional(string, "RAW")
body = optional(string, null)
freeform_tags = optional(map(string))
defined_tags = optional(map(string))
resolution = optional(string, "1m")
resource_group = optional(string, null)
split_notification = optional(bool, false)
}))
| n/a | yes | +| [alarm\_def](#input\_alarm\_def) | OCI Alarm definition |
map(object({
destination = string
display_name = optional(string)
severity = optional(string)
query = string
is_enabled = optional(bool)
rule_name = optional(string)
namespace = string
metric_compartment_id = optional(string)
repeat_notification_duration = optional(string)
trigger = optional(string)
suppression_from_time = optional(string)
suppression_till_time = optional(string)
message_format = optional(string)
body = optional(string)
freeform_tags = optional(map(string))
defined_tags = optional(map(string))
resolution = optional(string)
resource_group = optional(string)
split_notification = optional(bool)
has_overrides = optional(bool)
overrides = optional(map(object({
body = optional(string)
trigger = optional(string)
query = optional(string)
rule_name = optional(string)
severity = optional(string)
})))
}))
| n/a | yes | +| [alarm\_name\_prefix](#input\_alarm\_name\_prefix) | Prefix to be added to alarm resources | `string` | `"none"` | no | | [compartment\_ocid](#input\_compartment\_ocid) | Compartment OCID | `string` | n/a | yes | -| [label\_prefix](#input\_label\_prefix) | Prefix to be added to the resources | `string` | `"none"` | no | -| [notification](#input\_notification) | Notification Topic and Subscription |
map(object({
description = optional(string)
create_topic = optional(bool, true)
defined_tags = optional(map(string))
freeform_tags = optional(map(string))
subscription = optional(map(object({
endpoint = string
protocol = string
})))
}))
| n/a | yes | +| [notification](#input\_notification) | Notification Topic and Subscription |
map(object({
description = optional(string)
create_topic = optional(bool)
defined_tags = optional(map(string))
freeform_tags = optional(map(string))
subscription = optional(map(object({
endpoint = string
protocol = string
})))
}))
| n/a | yes | +| [topic\_name\_prefix](#input\_topic\_name\_prefix) | Prefix to be added to topic resources | `string` | `"none"` | no | ## Outputs | Name | Description | |------|-------------| | [topic\_ids](#output\_topic\_ids) | Notification Topic OCID | - \ No newline at end of file diff --git a/modules/alarm/main.tf b/modules/alarm/main.tf index d6f3a81..70ab313 100644 --- a/modules/alarm/main.tf +++ b/modules/alarm/main.tf @@ -4,12 +4,17 @@ resource "oci_ons_notification_topic" "this" { for_each = { for k, v in var.notification : k => v if v.create_topic == true } compartment_id = var.compartment_ocid - name = var.label_prefix == "none" ? each.key : format("%s_%s", var.label_prefix, each.key) - + name = var.topic_name_prefix == "none" ? each.key : format("%s-%s", var.topic_name_prefix, each.key) + description = each.value.description == null ? format("%s%s", each.key, " topic created by Terraform") : each.value.description defined_tags = each.value.defined_tags freeform_tags = each.value.freeform_tags - + lifecycle { + ignore_changes = [ + defined_tags["Oracle-Tags.CreatedBy"], + defined_tags["Oracle-Tags.CreatedOn"] + ] + } } data "oci_ons_notification_topics" "existing_topic" { @@ -31,16 +36,22 @@ resource "oci_ons_subscription" "this" { defined_tags = each.value.defined_tags freeform_tags = each.value.freeform_tags + lifecycle { + ignore_changes = [ + defined_tags["Oracle-Tags.CreatedBy"], + defined_tags["Oracle-Tags.CreatedOn"] + ] + } } resource "oci_monitoring_alarm" "this" { for_each = length(var.alarm_def) > 0 ? var.alarm_def : {} compartment_id = var.compartment_ocid 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)] - display_name = var.label_prefix == "none" ? each.value.display_name : format("%s_%s", var.label_prefix, each.value.display_name) + display_name = var.alarm_name_prefix == "none" ? each.key : format("%s-%s", var.alarm_name_prefix, each.key) is_enabled = each.value.is_enabled is_notifications_per_metric_dimension_enabled = each.value.split_notification - metric_compartment_id = each.value.metric_compartment_id == null ? var.compartment_ocid : each.value.metric_compartment + metric_compartment_id = each.value.metric_compartment_id == null ? var.compartment_ocid : each.value.metric_compartment_id namespace = each.value.namespace query = each.value.query severity = each.value.severity @@ -48,10 +59,22 @@ resource "oci_monitoring_alarm" "this" { repeat_notification_duration = each.value.repeat_notification_duration resource_group = each.value.resource_group resolution = each.value.resolution + rule_name = each.value.rule_name pending_duration = each.value.trigger body = each.value.body defined_tags = each.value.defined_tags freeform_tags = each.value.freeform_tags + dynamic "overrides" { + for_each = { for k ,v in coalesce(each.value.overrides, {}) : k => v if each.value.has_overrides } + + content { + body = overrides.value.body + pending_duration = overrides.value.trigger + query = overrides.value.query + rule_name = overrides.value.rule_name + severity = overrides.value.severity + } + } dynamic "suppression" { for_each = (each.value.suppression_from_time != null && each.value.suppression_till_time != null) ? [1] : [] content { @@ -59,7 +82,12 @@ resource "oci_monitoring_alarm" "this" { time_suppress_until = each.value.suppression_till_time } } - + lifecycle { + ignore_changes = [ + defined_tags["Oracle-Tags.CreatedBy"], + defined_tags["Oracle-Tags.CreatedOn"] + ] + } } @@ -70,7 +98,7 @@ locals { 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 protocol = subscription_value.protocol endpoint = subscription_value.endpoint - subscription = format("%s_%s", topic_key, subscription_key) + subscription = format("%s-%s", topic_key, subscription_key) defined_tags = topic_value.defined_tags freeform_tags = topic_value.freeform_tags } diff --git a/modules/alarm/variables.tf b/modules/alarm/variables.tf index 797f04b..56446cd 100644 --- a/modules/alarm/variables.tf +++ b/modules/alarm/variables.tf @@ -11,23 +11,32 @@ variable "alarm_def" { description = "OCI Alarm definition" type = map(object({ destination = string - display_name = string - severity = optional(string, "CRITICAL") + display_name = optional(string) + severity = optional(string) query = string - is_enabled = optional(bool, true) + is_enabled = optional(bool) + rule_name = optional(string) namespace = string metric_compartment_id = optional(string) - repeat_notification_duration = optional(string, "PT5M") - trigger = optional(string, "PT5M") + repeat_notification_duration = optional(string) + trigger = optional(string) suppression_from_time = optional(string) suppression_till_time = optional(string) - message_format = optional(string, "RAW") - body = optional(string, null) + message_format = optional(string) + body = optional(string) freeform_tags = optional(map(string)) defined_tags = optional(map(string)) - resolution = optional(string, "1m") - resource_group = optional(string, null) - split_notification = optional(bool, false) + resolution = optional(string) + resource_group = optional(string) + split_notification = optional(bool) + has_overrides = optional(bool) + overrides = optional(map(object({ + body = optional(string) + trigger = optional(string) + query = optional(string) + rule_name = optional(string) + severity = optional(string) + }))) })) } @@ -35,7 +44,7 @@ variable "notification" { description = "Notification Topic and Subscription" type = map(object({ description = optional(string) - create_topic = optional(bool, true) + create_topic = optional(bool) defined_tags = optional(map(string)) freeform_tags = optional(map(string)) subscription = optional(map(object({ @@ -45,8 +54,14 @@ variable "notification" { })) } -variable "label_prefix" { +variable "alarm_name_prefix" { default = "none" - description = "Prefix to be added to the resources" + description = "Prefix to be added to alarm resources" type = string } + +variable "topic_name_prefix" { + default = "none" + description = "Prefix to be added to topic resources" + type = string +} \ No newline at end of file diff --git a/modules/alarm/versions.tf b/modules/alarm/versions.tf index 5afc4d5..f07f8eb 100644 --- a/modules/alarm/versions.tf +++ b/modules/alarm/versions.tf @@ -8,5 +8,8 @@ terraform { version = ">= 4.67.3" } } - required_version = ">= 1.3.0" + # required_version = ">= 1.3.0" + required_version = ">= 1.2.0" + # Only use below statment if terraform version <= 1.2.x + experiments = [ module_variable_optional_attrs ] }