-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.tf
177 lines (146 loc) · 5.25 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
terraform {
required_version = ">= 0.13"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.53.0"
}
}
}
provider azurerm {
features {}
}
locals {
consumers = { for hc in flatten([for h in var.hubs :
[for c in h.consumers : {
hub = h.name
name = c
}]]) : format("%s.%s", hc.hub, hc.name) => hc }
keys = { for hk in flatten([for h in var.hubs :
[for k in h.keys : {
hub = h.name
key = k
}]]) : format("%s.%s", hk.hub, hk.key.name) => hk }
hubs = { for h in var.hubs : h.name => h }
authorization_rules = { for a in var.authorization_rules : a.name => a }
diag_resource_list = var.diagnostics != null ? split("/", var.diagnostics.destination) : []
parsed_diag = var.diagnostics != null ? {
log_analytics_id = contains(local.diag_resource_list, "Microsoft.OperationalInsights") ? var.diagnostics.destination : null
storage_account_id = contains(local.diag_resource_list, "Microsoft.Storage") ? var.diagnostics.destination : null
event_hub_auth_id = contains(local.diag_resource_list, "Microsoft.EventHub") ? var.diagnostics.destination : null
metric = var.diagnostics.metrics
log = var.diagnostics.logs
} : {
log_analytics_id = null
storage_account_id = null
event_hub_auth_id = null
metric = []
log = []
}
}
resource "azurerm_resource_group" "events" {
name = var.resource_group_name
location = var.location
tags = var.tags
}
resource "azurerm_eventhub_namespace" "events" {
name = "${var.name}-ns"
location = azurerm_resource_group.events.location
resource_group_name = azurerm_resource_group.events.name
sku = var.sku
capacity = var.capacity
auto_inflate_enabled = var.auto_inflate != null ? var.auto_inflate.enabled : null
maximum_throughput_units = var.auto_inflate != null ? var.auto_inflate.maximum_throughput_units : null
dynamic "network_rulesets" {
for_each = var.network_rules != null ? ["true"] : []
content {
default_action = "Deny"
dynamic "ip_rule" {
for_each = var.network_rules.ip_rules
iterator = iprule
content {
ip_mask = iprule.value
}
}
dynamic "virtual_network_rule" {
for_each = var.network_rules.subnet_ids
iterator = subnet
content {
subnet_id = subnet.value
}
}
}
}
tags = var.tags
}
resource "azurerm_eventhub_namespace_authorization_rule" "events" {
for_each = local.authorization_rules
name = each.key
namespace_name = azurerm_eventhub_namespace.events.name
resource_group_name = azurerm_resource_group.events.name
listen = each.value.listen
send = each.value.send
manage = each.value.manage
}
resource "azurerm_eventhub" "events" {
for_each = local.hubs
name = each.key
namespace_name = azurerm_eventhub_namespace.events.name
resource_group_name = azurerm_resource_group.events.name
partition_count = each.value.partitions
message_retention = each.value.message_retention
}
resource "azurerm_eventhub_consumer_group" "events" {
for_each = local.consumers
name = each.value.name
namespace_name = azurerm_eventhub_namespace.events.name
eventhub_name = each.value.hub
resource_group_name = azurerm_resource_group.events.name
user_metadata = "terraform"
depends_on = [azurerm_eventhub.events]
}
resource "azurerm_eventhub_authorization_rule" "events" {
for_each = local.keys
name = each.value.key.name
namespace_name = azurerm_eventhub_namespace.events.name
eventhub_name = each.value.hub
resource_group_name = azurerm_resource_group.events.name
listen = each.value.key.listen
send = each.value.key.send
manage = false
depends_on = [azurerm_eventhub.events]
}
data "azurerm_monitor_diagnostic_categories" "default" {
resource_id = azurerm_eventhub_namespace.events.id
}
resource "azurerm_monitor_diagnostic_setting" "namespace" {
count = var.diagnostics != null ? 1 : 0
name = "${var.name}-ns-diag"
target_resource_id = azurerm_eventhub_namespace.events.id
log_analytics_workspace_id = local.parsed_diag.log_analytics_id
eventhub_authorization_rule_id = local.parsed_diag.event_hub_auth_id
eventhub_name = local.parsed_diag.event_hub_auth_id != null ? var.diagnostics.eventhub_name : null
storage_account_id = local.parsed_diag.storage_account_id
dynamic "log" {
for_each = data.azurerm_monitor_diagnostic_categories.default.logs
content {
category = log.value
enabled = contains(local.parsed_diag.log, "all") || contains(local.parsed_diag.log, log.value)
retention_policy {
days = 0
enabled = false
}
}
}
dynamic "metric" {
for_each = data.azurerm_monitor_diagnostic_categories.default.metrics
content {
category = metric.value
enabled = contains(local.parsed_diag.metric, "all") || contains(local.parsed_diag.metric, metric.value)
retention_policy {
days = 0
enabled = false
}
}
}
}