generated from oracle-quickstart/oci-quickstart-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
subnets.tf
394 lines (377 loc) · 23.7 KB
/
subnets.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# ####################################################################################################### #
# Copyright (c) 2023 Oracle and/or its affiliates, All rights reserved. #
# Licensed under the Universal Permissive License v 1.0 as shown at https: //oss.oracle.com/licenses/upl. #
# Author: Cosmin Tudor #
# Author email: cosmin.tudor@oracle.com #
# Last Modified: Wed Nov 15 2023 #
# Modified by: Cosmin Tudor, email: cosmin.tudor@oracle.com #
# ####################################################################################################### #
locals {
one_dimension_processed_subnets = local.one_dimension_processed_vcns != null ? {
for flat_subnet in flatten([
for vcn_key, vcn_value in local.one_dimension_processed_vcns : vcn_value.subnets != null ? length(vcn_value.subnets) > 0 ? [
for subnet_key, subnet_value in vcn_value.subnets : {
cidr_block = subnet_value.cidr_block
compartment_id = subnet_value.compartment_id != null ? subnet_value.compartment_id : vcn_value.category_compartment_id != null ? vcn_value.category_compartment_id : vcn_value.default_compartment_id != null ? vcn_value.default_compartment_id : null
default_compartment_id = vcn_value.default_compartment_id
category_compartment_id = vcn_value.category_compartment_id
defined_tags = merge(subnet_value.defined_tags, vcn_value.category_defined_tags, vcn_value.default_defined_tags)
default_defined_tags = vcn_value.default_defined_tags
category_defined_tags = vcn_value.category_defined_tags
availability_domain = subnet_value.availability_domain
dhcp_options_key = subnet_value.dhcp_options_key
dhcp_options_id = subnet_value.dhcp_options_key != null ? merge(
{
for dhcp_opt_key, dhcp_opt_value in oci_core_dhcp_options.these : dhcp_opt_key => {
id = dhcp_opt_value.id
}
},
{
"default_dhcp_options" = {
id = oci_core_vcn.these[vcn_key].default_dhcp_options_id
}
}
)[subnet_value.dhcp_options_key].id : oci_core_vcn.these[vcn_key].default_dhcp_options_id
display_name = subnet_value.display_name
dns_label = subnet_value.dns_label
freeform_tags = merge(subnet_value.freeform_tags, vcn_value.category_freeform_tags, vcn_value.default_freeform_tags)
category_freeform_tags = vcn_value.category_freeform_tags
default_freeform_tags = vcn_value.default_freeform_tags
ipv6cidr_block = subnet_value.ipv6cidr_block
ipv6cidr_blocks = subnet_value.ipv6cidr_blocks
prohibit_internet_ingress = subnet_value.prohibit_internet_ingress
prohibit_public_ip_on_vnic = subnet_value.prohibit_public_ip_on_vnic
route_table_key = subnet_value.route_table_key
route_table_id = subnet_value.route_table_key != null ? merge(
{
for rt_key, rt_value in merge(
local.provisioned_non_gw_specific_remaining_route_tables,
local.provisioned_drga_specific_route_tables,
local.provisioned_lpg_specific_route_tables,
local.provisioned_sgw_specific_route_tables,
local.provisioned_igw_natgw_specific_route_tables) : rt_key => {
id = rt_value.id
}
},
{
"default_route_table" = {
id = oci_core_vcn.these[vcn_key].default_route_table_id
}
}
)[subnet_value.route_table_key].id : null
security_list_keys = subnet_value.security_list_keys
security_list_ids = subnet_value.security_list_keys != null ? length(subnet_value.security_list_keys) > 0 ? [
for seclistname in subnet_value.security_list_keys : merge(
{
for sec_list_key, sec_list_value in oci_core_security_list.these : sec_list_key => {
sec_list_key = sec_list_key,
display_name = sec_list_value.display_name,
id = sec_list_value.id
}
},
{
"default_security_list" = {
sec_list_key = "default_security_list",
display_name = "default_security_list",
id = oci_core_vcn.these[vcn_key].default_security_list_id
}
})[seclistname].id
] : [oci_core_vcn.these[vcn_key].default_security_list_id] : [oci_core_vcn.these[vcn_key].default_security_list_id]
network_configuration_category = vcn_value.network_configuration_category
vcn_key = vcn_key
vcn_name = vcn_value.display_name
subnet_key = subnet_key
vcn_id = oci_core_vcn.these[vcn_key].id
}
] : [] : []
]) : flat_subnet.subnet_key => flat_subnet
} : null
one_dimension_processed_injected_subnets = local.one_dimension_processed_existing_vcns != null ? {
for flat_subnet in flatten([
for vcn_key, vcn_value in local.one_dimension_processed_existing_vcns : vcn_value.subnets != null ? length(vcn_value.subnets) > 0 ? [
for subnet_key, subnet_value in vcn_value.subnets : {
cidr_block = subnet_value.cidr_block
compartment_id = subnet_value.compartment_id != null ? subnet_value.compartment_id : vcn_value.category_compartment_id != null ? vcn_value.category_compartment_id : vcn_value.default_compartment_id != null ? vcn_value.default_compartment_id : null
default_compartment_id = vcn_value.default_compartment_id
category_compartment_id = vcn_value.category_compartment_id
defined_tags = merge(subnet_value.defined_tags, vcn_value.category_defined_tags, vcn_value.default_defined_tags)
default_defined_tags = vcn_value.default_defined_tags
category_defined_tags = vcn_value.category_defined_tags
availability_domain = subnet_value.availability_domain
dhcp_options_key = subnet_value.dhcp_options_key
dhcp_options_id = subnet_value.dhcp_options_id != null ? subnet_value.dhcp_options_id : subnet_value.dhcp_options_key != null ? merge(
{
for dhcp_opt_key, dhcp_opt_value in oci_core_dhcp_options.these : dhcp_opt_key => {
id = dhcp_opt_value.id
}
},
{
"default_dhcp_options" = {
id = vcn_value.default_dhcp_options_id
}
}
)[subnet_value.dhcp_options_key].id : vcn_value.default_dhcp_options_id
display_name = subnet_value.display_name
dns_label = subnet_value.dns_label
freeform_tags = merge(subnet_value.freeform_tags, vcn_value.category_freeform_tags, vcn_value.default_freeform_tags)
category_freeform_tags = vcn_value.category_freeform_tags
default_freeform_tags = vcn_value.default_freeform_tags
ipv6cidr_block = subnet_value.ipv6cidr_block
ipv6cidr_blocks = subnet_value.ipv6cidr_blocks
prohibit_internet_ingress = subnet_value.prohibit_internet_ingress
prohibit_public_ip_on_vnic = subnet_value.prohibit_public_ip_on_vnic
route_table_key = subnet_value.route_table_key
route_table_id = subnet_value.route_table_key != null ? merge(
{
for rt_key, rt_value in merge(
local.provisioned_non_gw_specific_remaining_route_tables,
local.provisioned_drga_specific_route_tables,
local.provisioned_lpg_specific_route_tables,
local.provisioned_sgw_specific_route_tables,
local.provisioned_igw_natgw_specific_route_tables) : rt_key => {
id = rt_value.id
}
},
{
"default_route_table" = {
id = vcn_value.default_route_table_id
}
}
)[subnet_value.route_table_key].id : null
security_list_keys = subnet_value.security_list_keys
security_list_ids = concat(
subnet_value.security_list_keys != null ? [
for seclistname in subnet_value.security_list_keys : merge(
{
for sec_list_key, sec_list_value in oci_core_security_list.these : sec_list_key => {
sec_list_key = sec_list_key,
display_name = sec_list_value.display_name,
id = sec_list_value.id
}
},
{
"default_security_list" = {
sec_list_key = "default_security_list",
display_name = "default_security_list",
id = vcn_value.default_security_list_id
}
})[seclistname].id
] : [],
subnet_value.security_list_ids != null ? subnet_value.security_list_ids : []
) != null ? length(
concat(
subnet_value.security_list_keys != null ? [
for seclistname in subnet_value.security_list_keys : merge(
{
for sec_list_key, sec_list_value in oci_core_security_list.these : sec_list_key => {
sec_list_key = sec_list_key,
display_name = sec_list_value.display_name,
id = sec_list_value.id
}
},
{
"default_security_list" = {
sec_list_key = "default_security_list",
display_name = "default_security_list",
id = vcn_value.default_security_list_id
}
})[seclistname].id
] : [],
subnet_value.security_list_ids != null ? subnet_value.security_list_ids : []
)
) > 0 ? concat(
subnet_value.security_list_keys != null ? [
for seclistname in subnet_value.security_list_keys : merge(
{
for sec_list_key, sec_list_value in oci_core_security_list.these : sec_list_key => {
sec_list_key = sec_list_key,
display_name = sec_list_value.display_name,
id = sec_list_value.id
}
},
{
"default_security_list" = {
sec_list_key = "default_security_list",
display_name = "default_security_list",
id = vcn_value.default_security_list_id
}
})[seclistname].id
] : [],
subnet_value.security_list_ids != null ? subnet_value.security_list_ids : []
) : [] : []
network_configuration_category = vcn_value.network_configuration_category
vcn_key = vcn_key
vcn_name = vcn_value.vcn_name
subnet_key = subnet_key
vcn_id = vcn_value.vcn_id
}
] : [] : []
]) : flat_subnet.subnet_key => flat_subnet
} : null
//merging new VCNs defined subnets with existing VCNs defined subnets into a single map
merged_one_dimension_processed_subnets = merge(local.one_dimension_processed_subnets, local.one_dimension_processed_injected_subnets)
aux_provisioned_subnets = {
for subnet_key, subnet_value in oci_core_subnet.these : subnet_key => {
availability_domain = subnet_value.availability_domain
cidr_block = subnet_value.cidr_block
compartment_id = subnet_value.compartment_id
defined_tags = subnet_value.defined_tags
dhcp_options_id = subnet_value.dhcp_options_id
dhcp_options_key = local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_id != null ? local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key != null ? local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key : "NOT DETERMINED AS NOT CREATED BY THIS AUTOMATION" : "default_dhcp_options"
display_name = subnet_value.display_name
dns_label = subnet_value.dns_label
freeform_tags = subnet_value.freeform_tags
id = subnet_value.id
ipv6cidr_block = subnet_value.ipv6cidr_block
ipv6cidr_blocks = subnet_value.ipv6cidr_blocks
ipv6virtual_router_ip = subnet_value.ipv6virtual_router_ip
prohibit_internet_ingress = subnet_value.prohibit_internet_ingress
prohibit_public_ip_on_vnic = subnet_value.prohibit_public_ip_on_vnic
route_table_id = subnet_value.route_table_id
route_table_key = local.merged_one_dimension_processed_subnets[subnet_key].route_table_id != null ? local.merged_one_dimension_processed_subnets[subnet_key].route_table_key != null ? local.merged_one_dimension_processed_subnets[subnet_key].route_table_key : "NOT DETERMINED AS NOT CREATED BY THIS AUTOMATION" : "default_route_table"
security_lists = flatten(subnet_value.security_list_ids != null ? [
for sec_list_id in subnet_value.security_list_ids : contains([
for sec_list_key, sec_list_value in merge(
{
for sec_list_key, sec_list_value in oci_core_security_list.these : sec_list_key => {
sec_list_key = sec_list_key,
display_name = sec_list_value.display_name,
id = sec_list_value.id
}
},
{
"default_security_list" = {
sec_list_key = "default_security_list",
display_name = "default_security_list",
id = contains(keys(oci_core_vcn.these),local.merged_one_dimension_processed_subnets[subnet_key].vcn_key) ? oci_core_vcn.these[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_security_list_id : data.oci_core_vcn.existing_vcns[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_security_list_id
#id = oci_core_vcn.these[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_security_list_id
}
}) : sec_list_value.id
],
sec_list_id) ? [
for sec_list_key, sec_list_value in merge(
{
for sec_list_key, sec_list_value in oci_core_security_list.these : sec_list_key => {
sec_list_key = sec_list_key,
display_name = sec_list_value.display_name,
id = sec_list_value.id
}
},
{
"default_security_list" = {
sec_list_key = "default_security_list",
display_name = "default_security_list",
id = contains(keys(oci_core_vcn.these),local.merged_one_dimension_processed_subnets[subnet_key].vcn_key) ? oci_core_vcn.these[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_security_list_id : data.oci_core_vcn.existing_vcns[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_security_list_id
#id = oci_core_vcn.these[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_security_list_id
}
}) : {
display_name = sec_list_value.display_name
sec_list_key = sec_list_key
sec_list_id = sec_list_id
} if sec_list_value.id == sec_list_id] : [
{
display_name = "NOT DETERMINED AS NOT CREATED BY THIS AUTOMATION"
sec_list_key = "NOT DETERMINED AS NOT CREATED BY THIS AUTOMATION"
sec_list_id = sec_list_id
}
]
] : [])
state = subnet_value.state
subnet_domain_name = subnet_value.subnet_domain_name
time_created = subnet_value.time_created
timeouts = subnet_value.timeouts
vcn_id = subnet_value.vcn_id
vcn_key = local.merged_one_dimension_processed_subnets[subnet_key].vcn_key
vcn_name = local.merged_one_dimension_processed_subnets[subnet_key].vcn_name
network_configuration_category = local.merged_one_dimension_processed_subnets[subnet_key].network_configuration_category
virtual_router_ip = subnet_value.virtual_router_ip
virtual_router_mac = subnet_value.virtual_router_mac
subnet_key = subnet_key
}
}
provisioned_subnets = {
for subnet_key, subnet_value in local.aux_provisioned_subnets : subnet_key => {
availability_domain = subnet_value.availability_domain
cidr_block = subnet_value.cidr_block
compartment_id = subnet_value.compartment_id
defined_tags = subnet_value.defined_tags
dhcp_options_id = subnet_value.dhcp_options_id
dhcp_options_key = local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_id != null ? local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key != null ? local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key : "default_dhcp_options" : "default_dhcp_options"
dhcp_options_name = local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_id != null ? local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key != null ? local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key == "default_dhcp_options" ? "default_dhcp_options" : can(oci_core_dhcp_options.these[local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key].display_name) ? oci_core_dhcp_options.these[local.merged_one_dimension_processed_subnets[subnet_key].dhcp_options_key].display_name : "CANNOT BE DETERMINED AS NOT CREATED BY THIS AUTOMATION" : "default_dhcp_options" : "default_dhcp_options"
display_name = subnet_value.display_name
dns_label = subnet_value.dns_label
freeform_tags = subnet_value.freeform_tags
id = subnet_value.id
ipv6cidr_block = subnet_value.ipv6cidr_block
ipv6cidr_blocks = subnet_value.ipv6cidr_blocks
ipv6virtual_router_ip = subnet_value.ipv6virtual_router_ip
prohibit_internet_ingress = subnet_value.prohibit_internet_ingress
prohibit_public_ip_on_vnic = subnet_value.prohibit_public_ip_on_vnic
route_table_id = subnet_value.route_table_id
route_table_key = subnet_value.route_table_id == merge(
local.provisioned_vcns,
local.one_dimension_processed_existing_vcns
)[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_route_table_id ? "default_route_table" : local.merged_one_dimension_processed_subnets[subnet_key].route_table_key == null ? local.merged_one_dimension_processed_subnets[subnet_key].route_table_id != null ? "CANNOT BE DETERMINED AS NOT CREATED BY THIS AUTOMATION" : null : local.merged_one_dimension_processed_subnets[subnet_key].route_table_key
route_table_name = subnet_value.route_table_id == merge(
local.provisioned_vcns,
local.one_dimension_processed_existing_vcns
)[local.merged_one_dimension_processed_subnets[subnet_key].vcn_key].default_route_table_id ? "default_route_table" : local.merged_one_dimension_processed_subnets[subnet_key].route_table_key == null ? local.merged_one_dimension_processed_subnets[subnet_key].route_table_id != null ? "CANNOT BE DETERMINED AS NOT CREATED BY THIS AUTOMATION" : null : can(
merge(
local.provisioned_non_gw_specific_remaining_route_tables,
local.provisioned_drga_specific_route_tables,
local.provisioned_lpg_specific_route_tables,
local.provisioned_sgw_specific_route_tables,
local.provisioned_igw_natgw_specific_route_tables
)[local.merged_one_dimension_processed_subnets[subnet_key].route_table_key].display_name) ? merge(
local.provisioned_non_gw_specific_remaining_route_tables,
local.provisioned_drga_specific_route_tables,
local.provisioned_lpg_specific_route_tables,
local.provisioned_sgw_specific_route_tables,
local.provisioned_igw_natgw_specific_route_tables
)[local.merged_one_dimension_processed_subnets[subnet_key].route_table_key].display_name : "CANNOT BE DETERMINED AS NOT CREATED BY THIS AUTOMATION"
security_lists = {
for sec_list in subnet_value.security_lists : sec_list.sec_list_id => {
display_name = sec_list.display_name
sec_list_key = sec_list.sec_list_key
}
}
state = subnet_value.state
subnet_domain_name = subnet_value.subnet_domain_name
time_created = subnet_value.time_created
timeouts = subnet_value.timeouts
vcn_id = subnet_value.vcn_id
vcn_key = local.merged_one_dimension_processed_subnets[subnet_key].vcn_key
vcn_name = local.merged_one_dimension_processed_subnets[subnet_key].vcn_name
network_configuration_category = local.merged_one_dimension_processed_subnets[subnet_key].network_configuration_category
virtual_router_ip = subnet_value.virtual_router_ip
virtual_router_mac = subnet_value.virtual_router_mac
subnet_key = subnet_key
}
}
}
resource "oci_core_subnet" "these" {
for_each = local.merged_one_dimension_processed_subnets
#Required
cidr_block = each.value.cidr_block
compartment_id = each.value.compartment_id != null ? (length(regexall("^ocid1.*$", each.value.compartment_id)) > 0 ? each.value.compartment_id : var.compartments_dependency[each.value.compartment_id].id) : null
vcn_id = each.value.vcn_id
#Optional
availability_domain = each.value.availability_domain
defined_tags = each.value.defined_tags
dhcp_options_id = each.value.dhcp_options_id
display_name = each.value.display_name
dns_label = each.value.dns_label
freeform_tags = merge(local.cislz_module_tag, each.value.freeform_tags)
ipv6cidr_block = each.value.ipv6cidr_block
ipv6cidr_blocks = each.value.ipv6cidr_blocks
prohibit_internet_ingress = each.value.prohibit_internet_ingress
prohibit_public_ip_on_vnic = each.value.prohibit_public_ip_on_vnic
route_table_id = each.value.route_table_id != null ? each.value.route_table_id : each.value.route_table_key != null ? merge(
local.provisioned_non_gw_specific_remaining_route_tables,
local.provisioned_drga_specific_route_tables,
local.provisioned_lpg_specific_route_tables,
local.provisioned_sgw_specific_route_tables,
local.provisioned_igw_natgw_specific_route_tables
)[each.value.route_table_key].id : null
security_list_ids = each.value.security_list_ids
}