-
Notifications
You must be signed in to change notification settings - Fork 5
/
foundation_subnetworks.tf
139 lines (118 loc) · 6.09 KB
/
foundation_subnetworks.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
## Useful Links
// Terraform - https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_subnetwork
// GCLOUD - https://cloud.google.com/sdk/gcloud/reference/compute/networks/subnets/create
## Known Issues
// https://github.com/hashicorp/terraform-provider-google/issues/2570 - Error updating secondary IP ranges in Google_compute_subnetwork
//// Cannot add and remove secondary IP ranges in the same request.
locals {
subnetwork_defaults = {
purpose = "PRIVATE"
secondary_ranges = []
role = "ACTIVE"
log_config = {
enabled = false
aggregation_interval = "INTERVAL_5_SEC"
flow_sampling = 50
metadata = "INCLUDE_ALL_METADATA"
metadata_fields = []
}
}
subnetworks = merge([
for key, network in
{ for x in var.network_configs : templatefile("${path.module}/templates/network.tftpl", {
attributes = {
name = try(x.name, null),
label = x.label,
prefix = try(x.prefix, var.prefix, null),
environment = try(x.environment, var.environment, null),
} }) => x } : { for primary_subnetwork in network.subnetworks : "${key}-${lower(primary_subnetwork.region)}-${try(primary_subnetwork.name, primary_subnetwork.ip_cidr_range)}" => {
name = templatefile("${path.module}/templates/subnetwork_primary.tftpl", {
attributes = {
name = try(primary_subnetwork.name, null),
label = network.label,
prefix = try(network.prefix, var.prefix, null),
environment = try(network.environment, var.environment, null)
region = module.gcp_utils.region_short_name_map[lower(primary_subnetwork.region)],
cidr = try(primary_subnetwork.ip_cidr_range, null),
} })
project_id = try(network.project_id, var.project_id)
network = key
purpose = try(primary_subnetwork.purpose, "PRIVATE")
// First Check
//// Is primary_subnetwork.purpose either OneOf["INTERNAL_HTTPS_LOAD_BALANCER"], if so primary_subnetwork.role is set to value from JSON
// else
//// primary_subnetwork.role is set to null
role = (contains(["INTERNAL_HTTPS_LOAD_BALANCER"], try(primary_subnetwork.purpose, "PRIVATE"))) ? try(primary_subnetwork.role, "ACTIVE") : null
// First Check
//// Is primary_subnetwork.purpose either OneOf["INTERNAL_HTTPS_LOAD_BALANCER","PRIVATE_SERVICE_CONNECT"], if so primary_subnetwork.private_ip_google_access == false
// Second Check
//// Is primary_subnetwork.private_ip_google_access set to DISABLED, if so primary_subnetwork.private_ip_google_access == false
// else
//// primary_subnetwork.private_ip_google_access == true
private_ip_google_access = (contains(["INTERNAL_HTTPS_LOAD_BALANCER", "PRIVATE_SERVICE_CONNECT"], try(primary_subnetwork.purpose, "PRIVATE"))) ? false : (contains(["DISABLED"], try(primary_subnetwork.private_ip_google_access, "ENABLED"))) ? false : true
region = lower(primary_subnetwork.region)
ip_cidr_range = primary_subnetwork.ip_cidr_range
secondary_subnetworks = try(
[for secondary_subnetwork in primary_subnetwork.secondary_subnetworks : {
range_name = templatefile("${path.module}/templates/subnetwork_secondary.tftpl", {
attributes = {
name = try(secondary_subnetwork.name, null),
label = network.label,
region = module.gcp_utils.region_short_name_map[lower(primary_subnetwork.region)],
cidr = try(secondary_subnetwork.ip_cidr_range, null)
} })
ip_cidr_range = secondary_subnetwork.ip_cidr_range
}], [])
log_config = {
enabled = (try(primary_subnetwork.log_config.enabled, false) && try(primary_subnetwork.purpose, "PRIVATE") == "PRIVATE") ? true : false
aggregation_interval = try(primary_subnetwork.log_config.aggregation_interval, "INTERVAL_5_SEC")
flow_sampling = try(primary_subnetwork.log_config.flow_sampling, 50) / 100
metadata = try(primary_subnetwork.log_config.metadata, local.subnetwork_defaults.log_config.metadata)
metadata_fields = (try(primary_subnetwork.log_config.metadata, local.subnetwork_defaults.log_config.metadata) == "CUSTOM_METADATA") ? try(primary_subnetwork.log_config.metadata_fields, local.subnetwork_defaults.log_config.metadata_fields) : []
}
}
} if can(network.subnetworks)
]...)
}
# Creates all subnetwork types except those with role BACKUP
resource "google_compute_subnetwork" "subnetworks" {
provider = google-beta
for_each = { for key, value in local.subnetworks : key => value if value.role != "BACKUP" }
name = each.value.name
project = each.value.project_id
region = each.value.region
network = each.value.network
ip_cidr_range = each.value.ip_cidr_range
private_ip_google_access = each.value.private_ip_google_access
purpose = each.value.purpose
role = each.value.role
secondary_ip_range = each.value.secondary_subnetworks
dynamic "log_config" {
for_each = each.value.log_config.enabled ? [1] : []
content {
aggregation_interval = each.value.log_config.aggregation_interval
flow_sampling = each.value.log_config.flow_sampling
metadata = each.value.log_config.metadata
metadata_fields = each.value.log_config.metadata_fields
}
}
depends_on = [
google_compute_network.networks,
]
}
# Backup Networks Need to be created After the primary network is setup
resource "google_compute_subnetwork" "subnetworks_backup" {
provider = google-beta
for_each = { for key, value in local.subnetworks : key => value if value.role == "BACKUP" }
name = each.value.name
project = each.value.project_id
region = each.value.region
network = each.value.network
ip_cidr_range = each.value.ip_cidr_range
purpose = each.value.purpose
role = each.value.role
depends_on = [
google_compute_network.networks,
google_compute_subnetwork.subnetworks,
]
}