forked from marceloalmeida/terraform-cidr-subnets
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
112 lines (102 loc) · 3.82 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
locals {
addrs_by_idx = cidrsubnets(var.base_cidr_block, var.networks[*].new_bits...)
addrs_by_service_role_az_index = {
for k, v in var.networks : join(
var.separator_char, [
v.service != null ? v.service : "null",
v.role != null ? v.role : "null",
v.availability_zone != null ? v.availability_zone : "null",
k,
]) => local.addrs_by_idx[k] if(v.service != null && v.role != null && v.availability_zone != null) }
network_objs = [
for k, v in var.networks : {
availability_zone = v.availability_zone
cidr_block = (v.service != null && v.role != null && v.availability_zone != null) ? local.addrs_by_idx[k] : tostring(null)
new_bits = v.new_bits
map_public_ip_on_launch = v.map_public_ip_on_launch
role = v.role
service = v.service
}
]
network_objs_map = {
for k, v in var.networks : join(
var.separator_char, [
format("%010d", k),
v.service != null ? v.service : "null",
v.role != null ? v.role : "null",
v.availability_zone != null ? v.availability_zone : "null",
]) => {
availability_zone = v.availability_zone
cidr_block = (v.service != null && v.role != null && v.availability_zone != null) ? local.addrs_by_idx[k] : tostring(null)
map_public_ip_on_launch = v.map_public_ip_on_launch == null ? false : v.map_public_ip_on_launch
new_bits = v.new_bits
role = v.role
service = v.service
}
}
network_objs_map_2 = {
for k, v in local.network_objs_map : join(
var.separator_char, [
k,
split(var.separator_char, k)[0],
split(var.separator_char, k)[1],
split(var.separator_char, k)[2],
]) => {
join(
var.separator_char, [
split(var.separator_char, k)[0],
split(var.separator_char, k)[1],
split(var.separator_char, k)[2]
]
) : v
}
}
network_objs_map_3 = [
for k, v in local.network_objs_map_2 : {
join(
var.separator_char, [
split(var.separator_char, k)[1],
split(var.separator_char, k)[2]
]) : lookup(
v,
"${split(var.separator_char, k)[0]}${var.separator_char}${split(var.separator_char, k)[1]}${var.separator_char}${split(var.separator_char, k)[2]}",
{}
)
}
]
service_role = distinct(flatten([for k, v in local.network_objs_map_3 : keys(v)]))
networks_service_role_list = {
for k, v in local.service_role : v => [
for i, j in local.network_objs_map_3 : merge(lookup(j, v, null), { "index" : i }) if v == try(flatten(keys(j))[0], null)
]
}
networks_service_role_map = {
for k, v in local.networks_service_role_list : k => {
for i, j in v : i => j
}
}
network_objs_complex_map = {
for k, v in local.network_objs_map : k => {
split(var.separator_char, k)[0] = {
split(var.separator_char, k)[1] = {
split(var.separator_char, k)[2] = {
availability_zone = v.availability_zone
cidr_block = v.cidr_block
map_public_ip_on_launch = v.map_public_ip_on_launch
new_bits = v.new_bits
role = v.role
service = v.service
}
}
}
}
}
groups = {
network_cidr_blocks = tomap(local.addrs_by_service_role_az_index)
networks = tolist(local.network_objs)
networks_map = tomap(local.network_objs_map)
networks_service_role_list = local.networks_service_role_list
networks_complex_map = tomap(local.network_objs_complex_map)
networks_service_role_map = local.networks_service_role_map
}
}