-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
52 lines (46 loc) · 1.31 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
locals {
parsed_rules = flatten([
for rule in var.rules : [
for port in coalesce(rule.ports, [null]) : [
for cidr in coalesce(rule.cidrs, [""]) : {
ports = port
protocol = rule.protocol
cidrs = cidr
direction = rule.direction
}
]
]
])
}
resource "twc_firewall" "default" {
name = var.name
dynamic "link" {
for_each = coalesce(var.resources, [])
content {
id = link.value.id
type = link.value.type
}
}
}
resource "twc_firewall_rule" "default" {
for_each = { for key, rule in local.parsed_rules : key => rule }
firewall_id = resource.twc_firewall.default.id
direction = each.value.direction
port = each.value.ports
protocol = each.value.protocol
cidr = each.value.cidrs
lifecycle {
precondition {
condition = contains(["egress", "ingress"], each.value.direction)
error_message = "Error! Protocol does not support"
}
precondition {
condition = each.value.protocol == "icmp" || each.value.ports != null
error_message = "Error! If protocol is 'icmp', ports must be empty."
}
precondition {
condition = contains(["udp", "tcp", "icmp"], each.value.protocol)
error_message = "Error! Protocol does not support"
}
}
}