-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
182 lines (161 loc) · 6.42 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
locals {
# create a flat list of objects for Cloudflare resources from A records
cf_a_records_list = flatten([
for zone_key, zone_data in var.zones: [
for record_key, record_data in zone_data.a_records: [
for ip in record_data.provider1_addresses: {
zone_id: zone_data.provider1_zone_id
name: record_data.name
ttl: record_data.ttl
proxied: record_data.provider1_proxied
ip: ip
unique_key: join("-", [ip, record_key, trimsuffix(zone_key, ".")])
}
]
]
])
# resource for_each requires a map. cf_a_records_list is a list of maps, so we must
# now project it into a single map where each key is unique. We will use the
# combined zone and record keys as a single unique key per instance.
cf_a_records_map = {
for record_data in local.cf_a_records_list: record_data.unique_key => record_data
}
# create a flat list of objects for Cloudflare resources from CNAME records
cf_cname_records_list = flatten([
for zone_key, zone_data in var.zones: [
for record_key, record_data in zone_data.cname_records: {
zone_id: zone_data.provider1_zone_id
name: record_data.name
value: record_data.provider1_cname
ttl: record_data.ttl
proxied: record_data.provider1_proxied
unique_key: join("-", [record_key, trimsuffix(zone_key, ".")])
}
]
])
# project to a map as required by for_each
cf_cname_records_map = {
for record_data in local.cf_cname_records_list: record_data.unique_key => record_data
}
# create a flat list of objects for Cloudflare resources from MX records
cf_mx_records_list = flatten([
for zone_key, zone_data in var.zones: [
for record_key, record_data in zone_data.mx_records: [
for mx_name, mx_data in record_data.provider1_mxs: {
zone_id: zone_data.provider1_zone_id
name: record_data.provider1_name
ttl: record_data.ttl
value: mx_data.exchange
priority: mx_data.preference
unique_key: join("-", [ mx_name, record_key, trimsuffix(zone_key, ".") ])
}
]
]
])
# project to a map as required by for_each
cf_mx_records_map = {
for record_data in local.cf_mx_records_list: record_data.unique_key => record_data
}
# create a flat list of objects for Bind resources from A records
b_a_records_set = flatten([
for zone_key, zone_data in var.zones: [
for record_key, record_data in zone_data.a_records: {
zone: zone_data.provider2_zone
name: record_data.name
ttl: record_data.ttl
addresses: record_data.provider2_addresses
unique_key: join("-", [ record_key, trimsuffix(zone_key, ".") ])
}
]
])
# project to a map as required by for_each
b_a_records_map = {
for record_data in local.b_a_records_set: record_data.unique_key => record_data
}
# create a flat list of objects for Bind resources from CNAME records
b_cname_records_list = flatten([
for zone_key, zone_data in var.zones: [
for record_key, record_data in zone_data.cname_records: {
zone: zone_data.provider2_zone
name: record_data.name
cname: record_data.provider2_cname
ttl: record_data.ttl
unique_key: join("-", [record_key, trimsuffix(zone_key, ".")])
}
]
])
# project to a map as required by for_each
b_cname_records_map = {
for record_data in local.b_cname_records_list: record_data.unique_key => record_data
}
# create a flat list of objects for Bind resources from MX records
b_mx_records_set = flatten([
for zone_key, zone_data in var.zones: [
for record_key, record_data in zone_data.mx_records: {
zone: zone_data.provider2_zone
name: record_data.provider2_name
ttl: record_data.ttl
mxs: record_data.provider2_mxs
unique_key: join("-", [ record_key, trimsuffix(zone_key, ".") ])
}
]
])
# project to a map as required by for_each
b_mx_records_map = {
for record_data in local.b_mx_records_set: record_data.unique_key => record_data
}
}
resource cloudflare_record "a_record" {
for_each = local.cf_a_records_map
zone_id = each.value.zone_id
name = each.value.name
value = each.value.ip
type = "A"
ttl = each.value.ttl
proxied = each.value.proxied
}
resource cloudflare_record "cname_record" {
for_each = local.cf_cname_records_map
zone_id = each.value.zone_id
name = each.value.name
value = each.value.value
type = "CNAME"
ttl = each.value.ttl
proxied = each.value.proxied
}
resource cloudflare_record "mx_record" {
for_each = local.cf_mx_records_map
zone_id = each.value.zone_id
name = each.value.name
value = each.value.value
priority = each.value.priority
type = "MX"
ttl = each.value.ttl
}
resource dns_a_record_set "a_record" {
for_each = local.b_a_records_map
zone = each.value.zone
name = each.value.name
addresses = each.value.addresses
ttl = each.value.ttl
}
resource dns_cname_record "cname_record" {
for_each = local.b_cname_records_map
zone = each.value.zone
name = each.value.name
cname = each.value.cname
ttl = each.value.ttl
}
resource dns_mx_record_set "mx_record" {
for_each = local.b_mx_records_map
zone = each.value.zone
name = each.value.name == "" ? null : each.value.name
ttl = each.value.ttl
dynamic "mx" {
for_each = each.value.mxs
content {
exchange = mx.value.exchange
preference = mx.value.preference
}
}
}