-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
78 lines (67 loc) · 6.72 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
# Copyright (c) 2020 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
# default values
locals {
subnet_defaults = {
display_name = null
compartment_id = null
defined_tags = {}
freeform_tags = {}
dynamic_cidr = false
cidr = null
cidr_len = 28
cidr_num = null
enable_dns = true
dns_label = "subnet"
private = true
ad = null
dhcp_options_id = null
route_table_id = null
security_list_ids = null
}
keys = keys(var.subnets)
}
# resource definition
resource "oci_core_subnet" "this" {
count = length(local.keys)
vcn_id = var.vcn_id
cidr_block = var.subnets[local.keys[count.index]].dynamic_cidr != true ? (local.subnet_defaults.dynamic_cidr == false ? var.subnets[local.keys[count.index]].cidr : (cidrsubnet((var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr), ((var.subnets[local.keys[count.index]].cidr_len != null ? var.subnets[local.keys[count.index]].cidr_len : local.subnet_defaults.cidr_len) - split("/", (var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr))[1]), (var.subnets[local.keys[count.index]].cidr_num != null ? var.subnets[local.keys[count.index]].cidr_num : count.index)))) : cidrsubnet((var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr), ((var.subnets[local.keys[count.index]].cidr_len != null ? var.subnets[local.keys[count.index]].cidr_len : local.subnet_defaults.cidr_len) - split("/", (var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr))[1]), (var.subnets[local.keys[count.index]].cidr_num != null ? var.subnets[local.keys[count.index]].cidr_num : count.index))
compartment_id = var.subnets[local.keys[count.index]].compartment_id != null ? var.subnets[local.keys[count.index]].compartment_id : var.default_compartment_id
defined_tags = var.subnets[local.keys[count.index]].defined_tags != null ? var.subnets[local.keys[count.index]].defined_tags : local.subnet_defaults.defined_tags
freeform_tags = var.subnets[local.keys[count.index]].freeform_tags != null ? var.subnets[local.keys[count.index]].freeform_tags : local.subnet_defaults.freeform_tags
display_name = local.keys[count.index] != null ? local.keys[count.index] : "${local.subnet_defaults.display_name}-${count.index}"
prohibit_public_ip_on_vnic = var.subnets[local.keys[count.index]].private != null ? var.subnets[local.keys[count.index]].private : local.subnet_defaults.private
dns_label = var.subnets[local.keys[count.index]].enable_dns != false ? (var.subnets[local.keys[count.index]].dns_label != null ? var.subnets[local.keys[count.index]].dns_label : "${local.subnet_defaults.dns_label}${count.index}" ) : null
availability_domain = var.subnets[local.keys[count.index]].ad != null ? lookup(data.oci_identity_availability_domains.this.availability_domains[var.subnets[local.keys[count.index]].ad], "name") : local.subnet_defaults.ad
dhcp_options_id = var.subnets[local.keys[count.index]].dhcp_options_id != null ? var.subnets[local.keys[count.index]].dhcp_options_id : local.subnet_defaults.dhcp_options_id
route_table_id = var.subnets[local.keys[count.index]].route_table_id != null ? var.subnets[local.keys[count.index]].route_table_id : local.subnet_defaults.route_table_id
security_list_ids = var.subnets[local.keys[count.index]].security_list_ids != null ? var.subnets[local.keys[count.index]].security_list_ids : local.subnet_defaults.security_list_ids
}
/*
Idea behind above code :
--------------
-- cidr_block
--------------
If the single line is broken out, here's what it looks like (verbatim):
var.subnets[local.keys[count.index]].dynamic_cidr != true ?
(
local.subnet_defaults.dynamic_cidr == false ?
var.subnets[local.keys[count.index]].cidr
: (
cidrsubnet(
(var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr),
((var.subnets[local.keys[count.index]].cidr_len != null ? var.subnets[local.keys[count.index]].cidr_len : local.subnet_defaults.cidr_len) - split("/", (var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr))[1]),
(var.subnets[local.keys[count.index]].cidr_num != null ? var.subnets[local.keys[count.index]].cidr_num : count.index)
)
)
)
:
cidrsubnet(
(var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr),
((var.subnets[local.keys[count.index]].cidr_len != null ? var.subnets[local.keys[count.index]].cidr_len : local.subnet_defaults.cidr_len) - split("/", (var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr))[1]),
(var.subnets[local.keys[count.index]].cidr_num != null ? var.subnets[local.keys[count.index]].cidr_num : count.index)
)
All of this is collapsed into this single line:
var.subnets[local.keys[count.index]].dynamic_cidr != true ? (local.subnet_defaults.dynamic_cidr == false ? var.subnets[local.keys[count.index]].cidr : (cidrsubnet((var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr), ((var.subnets[local.keys[count.index]].cidr_len != null ? var.subnets[local.keys[count.index]].cidr_len : local.subnet_defaults.cidr_len) - split("/", (var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr))[1]), (var.subnets[local.keys[count.index]].cidr_num != null ? var.subnets[local.keys[count.index]].cidr_num : count.index)))) : cidrsubnet((var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr), ((var.subnets[local.keys[count.index]].cidr_len != null ? var.subnets[local.keys[count.index]].cidr_len : local.subnet_defaults.cidr_len) - split("/", (var.subnets[local.keys[count.index]].cidr != null ? var.subnets[local.keys[count.index]].cidr : var.vcn_cidr))[1]), (var.subnets[local.keys[count.index]].cidr_num != null ? var.subnets[local.keys[count.index]].cidr_num : count.index))
Unfortunately, unless using HEREDOC expression (works for strings), you cannot span multiple lines in TF. The single line is barely discernable (without breaking it back apart as I've presented above), so hopefully the multi-line representation helps to understand the logic of what's happening.
*/