-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
199 lines (187 loc) · 16.7 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Copyright (c) 2023 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
locals {
tenancy_root_key = "TENANCY-ROOT"
subscriptions = flatten([
for topic_key, topic in (var.events_configuration["topics"] != null ? var.events_configuration["topics"] : {}) : [
for subs in (topic["subscriptions"] != null ? topic["subscriptions"] : []) : [
for value in subs["values"] : {
key = "${topic_key}.${value}"
#compartment_id = topic.compartment_id != null ? (length(regexall("^ocid1.*$", topic.compartment_id)) > 0 ? topic.compartment_id : var.compartments_dependency[topic.compartment_id].id) : (length(regexall("^ocid1.*$", var.events_configuration.default_compartment_id)) > 0 ? var.events_configuration.default_compartment_id : var.compartments_dependency[var.events_configuration.default_compartment_id].id)
compartment_id = topic.compartment_id != null ? topic.compartment_id : var.events_configuration.default_compartment_id
protocol = upper(subs.protocol)
endpoint = value
topic_id = oci_ons_notification_topic.these[topic_key].id
defined_tags = subs.defined_tags != null ? subs.defined_tags : topic.defined_tags != null ? topic.defined_tags : var.events_configuration.default_defined_tags
freeform_tags = merge(local.cislz_module_tag, subs.freeform_tags != null ? subs.freeform_tags : topic.freeform_tags != null ? topic.freeform_tags : var.events_configuration.default_freeform_tags)
}
]
]
])
subscription_protocols = ["EMAIL","CUSTOM_HTTPS","SLACK","PAGERDUTY","ORACLE_FUNCTIONS","SMS"]
data_element_attributes = ["compartmentId","compartmentName","resourceName","resourceId","availabilityDomain"]
filters = {for key, rule in var.events_configuration["event_rules"] : key => merge(rule.tags_filter != null ? {for tag_filter in rule.tags_filter : "definedTags" => {(tag_filter.namespace) : { for tag in tag_filter.tags : (tag.name) => tag.value }}}:{},
rule.attributes_filter != null ? {for attr_filter in rule.attributes_filter : attr_filter.attr => attr_filter.value if contains(local.data_element_attributes,attr_filter.attr)}:{},
rule.attributes_filter != null ? {for attr_filter in rule.attributes_filter : "additionalDetails" => {(attr_filter.attr) : attr_filter.value} if !contains(local.data_element_attributes,attr_filter.attr)}:{})
}
#-- The filters local variable is for easing filters processing in oci_events_rule condition attribute.
#-- Here we are processing tags_filter and attributes_filter input attributes, by building a Terraform map of objects made by tags and attributes filters as expected by the Events service.
#-- It results in a structure like this:
#-- filters = {
# IDENTITY-SIGNON-EVENTS-KEY = {
# compartmentId = [
# "ocid1.compartment.oc1..aaaaaaaaw22engd4ipk52nvsfzxeroee4uskaryijy6l5omfh7syza2vf4ja",
# "ocid1.compartment.oc1..aaaaaaaaw22engd4ipk52nvsfzxeroee4uskaryijy6l5omfh7syza2vf4ja",
# ]
# compartmentName = [
# "cislz-network-cmp",
# ]
# definedTags = {
# OracleInternalReserved = {
# CostCenter = "2"
# OwnerEmail = "josh.hammer@oracle.com"
# }
# }
# }
# NETWORK-EVENTS-KEY = {
# compartmentId = [
# "ocid1.compartment.oc1..aaaaaaaaw22engd4ipk52nvsfzxeroee4uskaryijy6l5omfh7syza2vf4ja",
# "ocid1.compartment.oc1..aaaaaaaaw22engd4ipk52nvsfzxeroee4uskaryijy6l5omfh7syza2vf4ja",
# ]
# definedTags = {
# OracleInternalReserved = {
# CostCenter = "1"
# OwnerEmail = "andre.correa@oracle.com"
# }
# }
# }
# }
}
resource "oci_events_rule" "these" {
for_each = var.events_configuration["event_rules"]
lifecycle {
precondition {
#-- This precondition checks if values in preconfigured_events_categories attributes are valid.
condition = each.value.preconfigured_events_categories != null ? length(setintersection(keys(local.preconfigured_events),[for category in each.value.preconfigured_events_categories : lower(category)])) == length([for category in each.value.preconfigured_events_categories : lower(category)]) : true
error_message = "VALIDATION FAILURE in event \"${each.key}\": \"${each.value.preconfigured_events_categories != null ? join(",",setsubtract([for category in each.value.preconfigured_events_categories : lower(category)],keys(local.preconfigured_events))) : ""}\" value is invalid for \"preconfigured_events_categories\" attribute. Valid values are ${join(", ",keys(local.preconfigured_events))} (case insensitive)."
}
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id != null && upper(coalesce(each.value.compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in event \"${each.key}\": variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in attribute \"compartment_id\"."
}
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id == null && upper(coalesce(var.events_configuration.default_compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in event \"${each.key}\": as attribute \"compartment_id\" is absent, variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in \"events_configuration's\" \"default_compartment_id\" attribute."
}
}
compartment_id = each.value.compartment_id != null ? (length(regexall("^ocid1.*$", each.value.compartment_id)) > 0 ? each.value.compartment_id : (upper(each.value.compartment_id) == "TENANCY-ROOT" ? var.tenancy_ocid : var.compartments_dependency[each.value.compartment_id].id)) : (length(regexall("^ocid1.*$", var.events_configuration.default_compartment_id)) > 0 ? var.events_configuration.default_compartment_id : (upper(var.events_configuration.default_compartment_id) == "TENANCY-ROOT" ? var.tenancy_ocid : var.compartments_dependency[var.events_configuration.default_compartment_id].id))
display_name = each.value.event_display_name
description = each.value.event_description != null ? each.value.event_description : each.value.event_display_name
condition = each.value.supplied_events != null ? jsonencode({"eventType":each.value.supplied_events,"data":local.filters[each.key]}) : jsonencode({"eventType":flatten(concat([for category in each.value.preconfigured_events_categories : local.preconfigured_events[lower(category)].conditions])),"data":local.filters[each.key]})
is_enabled = each.value.is_enabled != null ? each.value.is_enabled : true
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : var.events_configuration.default_defined_tags
freeform_tags = merge(local.cislz_module_tag, each.value.freeform_tags != null ? each.value.freeform_tags : var.events_configuration.default_freeform_tags)
actions {
dynamic "actions" {
for_each = each.value.destination_topic_ids != null ? each.value.destination_topic_ids : []
content {
action_type = "ONS"
is_enabled = true
description = "Events are published into a topic."
topic_id = length(regexall("^ocid1.*$", actions.value)) > 0 ? actions.value : contains(keys(oci_ons_notification_topic.these),actions.value) ? oci_ons_notification_topic.these[actions.value].id : var.topics_dependency[actions.value].id
stream_id = null
function_id = null
}
}
dynamic "actions" {
for_each = each.value.destination_stream_ids != null ? each.value.destination_stream_ids : []
content {
action_type = "OSS"
is_enabled = true
description = "Events are sent to a stream."
topic_id = null
stream_id = length(regexall("^ocid1.*$", actions.value)) > 0 ? actions.value : contains(keys(oci_streaming_stream.these),actions.value) ? oci_streaming_stream.these[actions.value].id : var.streams_dependency[actions.value].id
function_id = null
}
}
dynamic "actions" {
for_each = each.value.destination_function_ids != null ? each.value.destination_function_ids : []
content {
action_type = "FAAS"
is_enabled = true
description = "Events are sent to a function."
topic_id = null
stream_id = null
function_id = length(regexall("^ocid1.*$", actions.value)) > 0 ? actions.value : var.functions_dependency[actions.value].id
}
}
}
}
resource "oci_ons_notification_topic" "these" {
for_each = var.events_configuration["topics"] != null ? var.events_configuration["topics"] : {}
lifecycle {
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id != null && upper(coalesce(each.value.compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in topic \"${each.key}\": variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in attribute \"compartment_id\"."
}
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id == null && upper(coalesce(var.events_configuration.default_compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in topic \"${each.key}\": as attribute \"compartment_id\" is absent, variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in \"events_configuration's\" \"default_compartment_id\" attribute."
}
}
#compartment_id = each.value.compartment_id != null ? (length(regexall("^ocid1.*$", each.value.compartment_id)) > 0 ? each.value.compartment_id : var.compartments_dependency[each.value.compartment_id].id) : (length(regexall("^ocid1.*$", var.events_configuration.default_compartment_id)) > 0 ? var.events_configuration.default_compartment_id : var.compartments_dependency[var.events_configuration.default_compartment_id].id)
compartment_id = each.value.compartment_id != null ? (length(regexall("^ocid1.*$", each.value.compartment_id)) > 0 ? each.value.compartment_id : (upper(each.value.compartment_id) == local.tenancy_root_key ? var.tenancy_ocid : var.compartments_dependency[each.value.compartment_id].id)) : (length(regexall("^ocid1.*$", var.events_configuration.default_compartment_id)) > 0 ? var.events_configuration.default_compartment_id : (upper(var.events_configuration.default_compartment_id) == local.tenancy_root_key ? var.tenancy_ocid : var.compartments_dependency[var.events_configuration.default_compartment_id].id))
name = each.value.name
description = each.value.description != null ? each.value.description : each.value.name
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : var.events_configuration.default_defined_tags
freeform_tags = merge(local.cislz_module_tag, each.value.freeform_tags != null ? each.value.freeform_tags : var.events_configuration.default_freeform_tags)
}
resource "oci_ons_subscription" "these" {
for_each = { for subs in local.subscriptions : subs.key => {compartment_id = subs.compartment_id,
protocol = subs.protocol,
endpoint = subs.endpoint,
topic_id = subs.topic_id,
defined_tags = subs.defined_tags,
freeform_tags = subs.freeform_tags}}
lifecycle {
precondition {
condition = contains(local.subscription_protocols,upper(each.value.protocol))
error_message = "VALIDATION FAILURE in topic subscription \"${each.key}\": \"${each.value.protocol}\" value is invalid for \"protocol\" attribute. Valid values are ${join(", ",local.subscription_protocols)} (case insensitive)."
}
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id != null && upper(coalesce(each.value.compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in topic subscription \"${each.key}\": variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in attribute \"compartment_id\"."
}
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id == null && upper(coalesce(var.events_configuration.default_compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in topic subscription \"${each.key}\": as attribute \"compartment_id\" is absent, variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in \"events_configuration's\" \"default_compartment_id\" attribute."
}
}
#compartment_id = each.value.compartment_id
compartment_id = length(regexall("^ocid1.*$", each.value.compartment_id)) > 0 ? each.value.compartment_id : (upper(each.value.compartment_id) == local.tenancy_root_key ? var.tenancy_ocid : var.compartments_dependency[each.value.compartment_id].id)
topic_id = each.value.topic_id
endpoint = each.value.endpoint
protocol = each.value.protocol
defined_tags = each.value.defined_tags
freeform_tags = each.value.freeform_tags
}
resource "oci_streaming_stream" "these" {
for_each = var.events_configuration["streams"] != null ? var.events_configuration["streams"] : {}
lifecycle {
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id != null && upper(coalesce(each.value.compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in stream \"${each.key}\": variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in attribute \"compartment_id\"."
}
precondition {
condition = var.tenancy_ocid == null && each.value.compartment_id == null && upper(coalesce(var.events_configuration.default_compartment_id,"__void__")) == local.tenancy_root_key ? false : true
error_message = "VALIDATION FAILURE in stream \"${each.key}\": as attribute \"compartment_id\" is absent, variable \"tenancy_ocid\" is required when the \"${local.tenancy_root_key}\" key word is used to reference the root compartment in \"events_configuration's\" \"default_compartment_id\" attribute."
}
}
#compartment_id = each.value.compartment_id != null ? (length(regexall("^ocid1.*$", each.value.compartment_id)) > 0 ? each.value.compartment_id : var.compartments_dependency[each.value.compartment_id].id) : (length(regexall("^ocid1.*$", var.events_configuration.default_compartment_id)) > 0 ? var.events_configuration.default_compartment_id : var.compartments_dependency[var.events_configuration.default_compartment_id].id)
compartment_id = each.value.compartment_id != null ? (length(regexall("^ocid1.*$", each.value.compartment_id)) > 0 ? each.value.compartment_id : (upper(each.value.compartment_id) == local.tenancy_root_key ? var.tenancy_ocid : var.compartments_dependency[each.value.compartment_id].id)) : (length(regexall("^ocid1.*$", var.events_configuration.default_compartment_id)) > 0 ? var.events_configuration.default_compartment_id : (upper(var.events_configuration.default_compartment_id) == local.tenancy_root_key ? var.tenancy_ocid : var.compartments_dependency[var.events_configuration.default_compartment_id].id))
name = each.value.name
partitions = each.value.num_partitions != null ? each.value.num_partitions : 1
retention_in_hours = each.value.log_retention_in_hours != null ? each.value.log_retention_in_hours : 24
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : var.events_configuration.default_defined_tags
freeform_tags = merge(local.cislz_module_tag, each.value.freeform_tags != null ? each.value.freeform_tags : var.events_configuration.default_freeform_tags)
}