-
Notifications
You must be signed in to change notification settings - Fork 882
/
subnets.tf
293 lines (280 loc) · 10.6 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
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# tfdoc:file:description Subnet resources.
locals {
_factory_data_raw = {
for f in try(fileset(local._factory_path, "**/*.yaml"), []) :
trimsuffix(basename(f), ".yaml") => yamldecode(file("${local._factory_path}/${f}"))
}
_factory_data = {
for k, v in local._factory_data_raw : k => merge(v, { region_computed = lookup(var.factories_config.context.regions, v.region, v.region) })
}
_factory_path = try(pathexpand(var.factories_config.subnets_folder), null)
_factory_subnets = {
for k, v in local._factory_data :
"${v.region_computed}/${try(v.name, k)}" => {
active = try(v.active, true)
description = try(v.description, null)
enable_private_access = try(v.enable_private_access, true)
allow_subnet_cidr_routes_overlap = try(v.allow_subnet_cidr_routes_overlap, null)
flow_logs_config = can(v.flow_logs_config) ? {
aggregation_interval = try(v.flow_logs_config.aggregation_interval, null)
filter_expression = try(v.flow_logs_config.filter_expression, null)
flow_sampling = try(v.flow_logs_config.flow_sampling, null)
metadata = try(v.flow_logs_config.metadata, null)
metadata_fields = try(v.flow_logs_config.metadata_fields, null)
} : null
global = try(v.global, false)
ip_cidr_range = v.ip_cidr_range
ipv6 = !can(v.ipv6) ? null : {
access_type = try(v.ipv6.access_type, "INTERNAL")
}
name = try(v.name, k)
region = v.region_computed
secondary_ip_ranges = try(v.secondary_ip_ranges, null)
iam = try(v.iam, {})
iam_bindings = !can(v.iam_bindings) ? {} : {
for k2, v2 in v.iam_bindings :
k2 => {
role = v2.role
members = v2.members
condition = !can(v2.condition) ? null : {
expression = v2.condition.expression
title = v2.condition.title
description = try(v2.condition.description, null)
}
}
}
iam_bindings_additive = !can(v.iam_bindings_additive) ? {} : {
for k2, v2 in v.iam_bindings_additive :
k2 => {
member = v2.member
role = v2.role
condition = !can(v2.condition) ? null : {
expression = v2.condition.expression
title = v2.condition.title
description = try(v2.condition.description, null)
}
}
}
_is_regular = !try(v.psc == true, false) && !try(v.proxy_only == true, false)
_is_psc = try(v.psc == true, false)
_is_proxy_only = try(v.proxy_only == true, false)
}
}
all_subnets = merge(
{ for k, v in google_compute_subnetwork.subnetwork : k => v },
{ for k, v in google_compute_subnetwork.proxy_only : k => v },
{ for k, v in google_compute_subnetwork.psc : k => v }
)
subnet_iam = flatten(concat(
[
for s in concat(var.subnets, var.subnets_psc, var.subnets_proxy_only, values(local._factory_subnets)) : [
for role, members in s.iam :
{
role = role
members = members
subnet = "${s.region}/${s.name}"
}
]
],
))
subnet_iam_bindings = merge([
for s in concat(var.subnets, var.subnets_psc, var.subnets_proxy_only, values(local._factory_subnets)) : {
for key, data in s.iam_bindings :
key => {
role = data.role
subnet = "${s.region}/${s.name}"
members = data.members
condition = data.condition
}
}
]...)
# note: all additive bindings share a single namespace for the key.
# In other words, if you have multiple additive bindings with the
# same name, only one will be used
subnet_iam_bindings_additive = merge([
for s in concat(var.subnets, var.subnets_psc, var.subnets_proxy_only, values(local._factory_subnets)) : {
for key, data in s.iam_bindings_additive :
key => {
role = data.role
subnet = "${s.region}/${s.name}"
member = data.member
condition = data.condition
}
}
]...)
subnets = merge(
{ for s in var.subnets : "${s.region}/${s.name}" => s },
{ for k, v in local._factory_subnets : k => v if v._is_regular }
)
subnets_proxy_only = merge(
{ for s in var.subnets_proxy_only : "${s.region}/${s.name}" => s },
{ for k, v in local._factory_subnets : k => v if v._is_proxy_only },
)
subnets_private_nat = merge(
{ for s in var.subnets_private_nat : "${s.region}/${s.name}" => s },
# { for k, v in local._factory_subnets : k => v if v._is_proxy_only },
)
subnets_psc = merge(
{ for s in var.subnets_psc : "${s.region}/${s.name}" => s },
{ for k, v in local._factory_subnets : k => v if v._is_psc }
)
}
resource "google_compute_subnetwork" "subnetwork" {
provider = google-beta
for_each = local.subnets
project = var.project_id
network = local.network.name
name = each.value.name
region = each.value.region
ip_cidr_range = each.value.ip_cidr_range
allow_subnet_cidr_routes_overlap = each.value.allow_subnet_cidr_routes_overlap
description = (
each.value.description == null
? "Terraform-managed."
: each.value.description
)
private_ip_google_access = each.value.enable_private_access
stack_type = (
try(each.value.ipv6, null) != null ? "IPV4_IPV6" : null
)
ipv6_access_type = (
try(each.value.ipv6, null) != null ? each.value.ipv6.access_type : null
)
private_ipv6_google_access = try(each.value.ipv6.enable_private_access, null)
send_secondary_ip_range_if_empty = true
dynamic "secondary_ip_range" {
for_each = each.value.secondary_ip_ranges == null ? {} : each.value.secondary_ip_ranges
content {
range_name = secondary_ip_range.key
ip_cidr_range = secondary_ip_range.value
}
}
dynamic "log_config" {
for_each = each.value.flow_logs_config != null ? [""] : []
content {
aggregation_interval = each.value.flow_logs_config.aggregation_interval
filter_expr = each.value.flow_logs_config.filter_expression
flow_sampling = each.value.flow_logs_config.flow_sampling
metadata = each.value.flow_logs_config.metadata
metadata_fields = (
each.value.flow_logs_config.metadata == "CUSTOM_METADATA"
? each.value.flow_logs_config.metadata_fields
: null
)
}
}
}
resource "google_compute_subnetwork" "proxy_only" {
for_each = local.subnets_proxy_only
project = var.project_id
network = local.network.name
name = each.value.name
region = each.value.region
ip_cidr_range = each.value.ip_cidr_range
description = coalesce(
each.value.description,
"Terraform-managed proxy-only subnet for Regional HTTPS, Internal HTTPS or Cross-Regional HTTPS Internal LB."
)
purpose = each.value.global ? "GLOBAL_MANAGED_PROXY" : "REGIONAL_MANAGED_PROXY"
role = each.value.active ? "ACTIVE" : "BACKUP"
}
resource "google_compute_subnetwork" "private_nat" {
for_each = local.subnets_private_nat
project = var.project_id
network = local.network.name
name = each.value.name
region = each.value.region
ip_cidr_range = each.value.ip_cidr_range
description = coalesce(
each.value.description,
"Terraform-managed private NAT subnet."
)
purpose = "PRIVATE_NAT"
}
resource "google_compute_subnetwork" "psc" {
for_each = local.subnets_psc
project = var.project_id
network = local.network.name
name = each.value.name
region = each.value.region
ip_cidr_range = each.value.ip_cidr_range
description = coalesce(
each.value.description,
"Terraform-managed subnet for Private Service Connect (PSC NAT)."
)
purpose = "PRIVATE_SERVICE_CONNECT"
}
resource "google_compute_subnetwork_iam_binding" "authoritative" {
for_each = {
for binding in local.subnet_iam :
"${binding.subnet}.${binding.role}" => binding
}
project = var.project_id
subnetwork = local.all_subnets[each.value.subnet].name
region = local.all_subnets[each.value.subnet].region
role = each.value.role
members = each.value.members
}
resource "google_compute_subnetwork_iam_binding" "bindings" {
for_each = local.subnet_iam_bindings
project = var.project_id
subnetwork = local.all_subnets[each.value.subnet].name
region = local.all_subnets[each.value.subnet].region
role = each.value.role
members = each.value.members
dynamic "condition" {
for_each = each.value.condition == null ? [] : [""]
content {
expression = each.value.condition.expression
title = each.value.condition.title
description = each.value.condition.description
}
}
}
resource "google_compute_subnetwork_iam_member" "bindings" {
for_each = local.subnet_iam_bindings_additive
project = var.project_id
subnetwork = local.all_subnets[each.value.subnet].name
region = local.all_subnets[each.value.subnet].region
role = each.value.role
member = each.value.member
dynamic "condition" {
for_each = each.value.condition == null ? [] : [""]
content {
expression = each.value.condition.expression
title = each.value.condition.title
description = each.value.condition.description
}
}
}
resource "google_compute_network_attachment" "default" {
provider = google-beta
for_each = var.network_attachments
project = var.project_id
region = google_compute_subnetwork.subnetwork[each.value.subnet].region
name = each.key
description = each.value.description
connection_preference = (
each.value.automatic_connection ? "ACCEPT_AUTOMATIC" : "ACCEPT_MANUAL"
)
subnetworks = [
google_compute_subnetwork.subnetwork[each.value.subnet].self_link
]
producer_accept_lists = each.value.producer_accept_lists
producer_reject_lists = each.value.producer_reject_lists
}