-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
127 lines (103 loc) · 5.5 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
locals {
// unique lists of all the endpoints used in test definitions
all_sources = distinct(flatten([for k, tests in var.test_groups : [for resource_id in tests.sources : resource_id]]))
all_destinations = distinct(flatten([for k, tests in var.test_groups : [for host in tests.destinations : host]]))
// Maps with endpoint names, first try aliases map provided by the user, if not, generate default
source_names = { for resource_id in local.all_sources : resource_id => try(var.endpoint_aliases[resource_id], "${element(split("/", resource_id), length(split("/", resource_id)) - 1)} (${element(split("/", resource_id), 4)})") }
destination_names = { for host in local.all_destinations : host => try(var.endpoint_aliases[host], host) }
// Actual endpoints to generate
azure_source_endpoints = { for resource_id in local.all_sources : local.source_names[resource_id] => resource_id }
external_destination_endpoints = { for host in local.all_destinations : local.destination_names[host] => host }
// Test configurations
all_tests = flatten([for k, tests in var.test_groups : [for t in tests.test_configurations : merge({ test_configuration_name = lower("${t.protocol}${t.port != null ? "/${t.port}" : ""} (${k})") }, t)]])
tcp_test_configurations = merge(
{ for k, t in var.test_configurations : k => t if t.protocol == "Tcp" }, // globally defined
{ for t in local.all_tests : t.test_configuration_name => t if t.protocol == "Tcp" }, // inline defined
)
http_test_configurations = merge(
{ for k, t in var.test_configurations : k => t if t.protocol == "Http" }, // globally defined
{ for t in local.all_tests : t.test_configuration_name => t if t.protocol == "Http" }, // inline defined
)
icmp_test_configurations = merge(
{ for k, t in var.test_configurations : k => t if t.protocol == "Icmp" }, // globally defined
{ for t in local.all_tests : t.test_configuration_name => t if t.protocol == "Icmp" }, // inline defined
)
}
resource "azurerm_network_connection_monitor" "monitor" {
name = var.name
network_watcher_id = var.network_watcher_id
location = var.location
tags = var.tags
dynamic "endpoint" {
for_each = local.azure_source_endpoints
content {
name = endpoint.key
target_resource_id = endpoint.value
target_resource_type = "AzureVM"
}
}
dynamic "endpoint" {
for_each = local.external_destination_endpoints
content {
name = endpoint.key
address = endpoint.value
}
}
dynamic "test_configuration" {
for_each = local.icmp_test_configurations
content {
name = test_configuration.key
protocol = test_configuration.value.protocol
test_frequency_in_seconds = test_configuration.value.test_frequency_in_seconds
icmp_configuration {
}
success_threshold {
checks_failed_percent = try(test_configuration.value.sucess_threshold.checks_failed_percent, var.default_checks_failed_percent)
round_trip_time_ms = try(test_configuration.value.sucess_threshold.round_trip_time_ms, var.default_round_trip_time_ms)
}
}
}
dynamic "test_configuration" {
for_each = local.tcp_test_configurations
content {
name = test_configuration.key
protocol = test_configuration.value.protocol
test_frequency_in_seconds = test_configuration.value.test_frequency_in_seconds
tcp_configuration {
port = test_configuration.value.port
}
success_threshold {
checks_failed_percent = try(test_configuration.value.sucess_threshold.checks_failed_percent, var.default_checks_failed_percent)
round_trip_time_ms = try(test_configuration.value.sucess_threshold.round_trip_time_ms, var.default_round_trip_time_ms)
}
}
}
dynamic "test_configuration" {
for_each = local.http_test_configurations
content {
name = test_configuration.key
protocol = test_configuration.value.protocol
test_frequency_in_seconds = try(test_configuration.value.test_frequency_in_seconds, 60)
http_configuration {
port = try(test_configuration.value.port, 80)
path = try(test_configuration.value.http_configuration.path, null)
method = try(test_configuration.value.http_configuration.method, null)
valid_status_code_ranges = try(test_configuration.value.http_configuration.valid_status_code_ranges, null)
}
success_threshold {
checks_failed_percent = try(test_configuration.value.sucess_threshold.checks_failed_percent, var.default_checks_failed_percent)
round_trip_time_ms = try(test_configuration.value.sucess_threshold.round_trip_time_ms, var.default_round_trip_time_ms)
}
}
}
dynamic "test_group" {
for_each = var.test_groups
content {
name = test_group.key
destination_endpoints = [for host in test_group.value.destinations : local.destination_names[host]]
source_endpoints = [for resource_id in test_group.value.sources : local.source_names[resource_id]]
test_configuration_names = concat(test_group.value.tests, [for t in test_group.value.test_configurations : lower("${t.protocol}${t.port != null ? "/${t.port}" : ""} (${test_group.key})")])
}
}
output_workspace_resource_ids = [var.log_analytics_workspace_id]
}