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

Module alarm modifications #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions modules/alarm/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.2.0 |
| <a name="requirement_oci"></a> [oci](#requirement\_oci) | >= 4.67.3 |

## Providers
Expand All @@ -29,14 +28,14 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_alarm_def"></a> [alarm\_def](#input\_alarm\_def) | OCI Alarm definition | <pre>map(object({<br> destination = string<br> display_name = string<br> severity = optional(string, "CRITICAL")<br> query = string<br> is_enabled = optional(bool, true)<br> namespace = string<br> metric_compartment_id = optional(string)<br> repeat_notification_duration = optional(string, "PT5M")<br> trigger = optional(string, "PT5M")<br> suppression_from_time = optional(string)<br> suppression_till_time = optional(string)<br> message_format = optional(string, "RAW")<br> body = optional(string, null)<br> freeform_tags = optional(map(string))<br> defined_tags = optional(map(string))<br> resolution = optional(string, "1m")<br> resource_group = optional(string, null)<br> split_notification = optional(bool, false)<br> }))</pre> | n/a | yes |
| <a name="input_alarm_def"></a> [alarm\_def](#input\_alarm\_def) | OCI Alarm definition | <pre>map(object({<br> destination = string<br> display_name = optional(string)<br> severity = optional(string)<br> query = string<br> is_enabled = optional(bool)<br> rule_name = optional(string)<br> namespace = string<br> metric_compartment_id = optional(string)<br> repeat_notification_duration = optional(string)<br> trigger = optional(string)<br> suppression_from_time = optional(string)<br> suppression_till_time = optional(string)<br> message_format = optional(string)<br> body = optional(string)<br> freeform_tags = optional(map(string))<br> defined_tags = optional(map(string))<br> resolution = optional(string)<br> resource_group = optional(string)<br> split_notification = optional(bool)<br> has_overrides = optional(bool)<br> overrides = optional(map(object({<br> body = optional(string)<br> trigger = optional(string)<br> query = optional(string)<br> rule_name = optional(string)<br> severity = optional(string)<br> })))<br> }))</pre> | n/a | yes |
| <a name="input_alarm_name_prefix"></a> [alarm\_name\_prefix](#input\_alarm\_name\_prefix) | Prefix to be added to alarm resources | `string` | `"none"` | no |
| <a name="input_compartment_ocid"></a> [compartment\_ocid](#input\_compartment\_ocid) | Compartment OCID | `string` | n/a | yes |
| <a name="input_label_prefix"></a> [label\_prefix](#input\_label\_prefix) | Prefix to be added to the resources | `string` | `"none"` | no |
| <a name="input_notification"></a> [notification](#input\_notification) | Notification Topic and Subscription | <pre>map(object({<br> description = optional(string)<br> create_topic = optional(bool, true)<br> defined_tags = optional(map(string))<br> freeform_tags = optional(map(string))<br> subscription = optional(map(object({<br> endpoint = string<br> protocol = string<br> })))<br> }))</pre> | n/a | yes |
| <a name="input_notification"></a> [notification](#input\_notification) | Notification Topic and Subscription | <pre>map(object({<br> description = optional(string)<br> create_topic = optional(bool)<br> defined_tags = optional(map(string))<br> freeform_tags = optional(map(string))<br> subscription = optional(map(object({<br> endpoint = string<br> protocol = string<br> })))<br> }))</pre> | n/a | yes |
| <a name="input_topic_name_prefix"></a> [topic\_name\_prefix](#input\_topic\_name\_prefix) | Prefix to be added to topic resources | `string` | `"none"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_topic_ids"></a> [topic\_ids](#output\_topic\_ids) | Notification Topic OCID |
<!-- END_TF_DOCS -->
42 changes: 35 additions & 7 deletions modules/alarm/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -31,35 +36,58 @@ 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
message_format = each.value.message_format
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 {
time_suppress_from = each.value.suppression_from_time
time_suppress_until = each.value.suppression_till_time
}
}

lifecycle {
ignore_changes = [
defined_tags["Oracle-Tags.CreatedBy"],
defined_tags["Oracle-Tags.CreatedOn"]
]
}
}


Expand All @@ -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
}
Expand Down
41 changes: 28 additions & 13 deletions modules/alarm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,40 @@ 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)
})))
}))
}

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({
Expand All @@ -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
}
5 changes: 4 additions & 1 deletion modules/alarm/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
}