forked from Azure/terraform-azurerm-caf-enterprise-scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locals.management_groups.tf
397 lines (388 loc) · 18.5 KB
/
locals.management_groups.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
395
396
397
########################################################
# The locals defined within this file are used to generate
# the data model used to deploy the core Enterprise-scale
# Management Groups and any custom Management Groups
# specified via the custom_landing_zones variable.
########################################################
# The following locals are used to determine which archetype
# pattern to apply to the core Enterprise-scale Management
# Groups. To ensure a valid value is always provided, we
# provide a list of defaults in es_defaults which
# can be overridden using the es_overrides variable.
locals {
es_archetype_config_defaults = {
(local.root_id) = {
archetype_id = "es_root"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-decommissioned" = {
archetype_id = "es_decommissioned"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-sandboxes" = {
archetype_id = "es_sandboxes"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-landing-zones" = {
archetype_id = "es_landing_zones"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-platform" = {
archetype_id = "es_platform"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-connectivity" = {
archetype_id = "es_connectivity"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-management" = {
archetype_id = "es_management"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-identity" = {
archetype_id = "es_identity"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-corp" = {
archetype_id = "es_corp"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-online" = {
archetype_id = "es_online"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-sap" = {
archetype_id = "es_sap"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-demo-corp" = {
archetype_id = "es_corp"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-demo-online" = {
archetype_id = "es_online"
parameters = local.empty_map
access_control = local.empty_map
}
"${local.root_id}-demo-sap" = {
archetype_id = "es_sap"
parameters = local.empty_map
access_control = local.empty_map
}
}
archetype_config_overrides_map = {
for key, value in local.archetype_config_overrides :
key == "root" ? local.root_id : "${local.root_id}-${key}" => value
}
es_archetype_config_map = {
for key, value in local.es_archetype_config_defaults :
key => {
archetype_id = coalesce(try(local.archetype_config_overrides_map[key].archetype_id, ""), local.es_archetype_config_defaults[key].archetype_id)
parameters = merge(try(local.archetype_config_overrides_map[key].parameters, {}), local.es_archetype_config_defaults[key].parameters)
access_control = merge(try(local.archetype_config_overrides_map[key].access_control, {}), local.es_archetype_config_defaults[key].access_control)
enforcement_mode = try(local.archetype_config_overrides_map[key].enforcement_mode, local.empty_map)
}
}
}
# The following locals are used to determine which subscription_ids
# should be assigned to the core Enterprise-scale Management
# Groups. To ensure a valid value is always provided, we
# provide a list of defaults in es_subscription_ids_defaults which
# can be overridden using the subscription_id_overrides variable.
locals {
es_subscription_ids_defaults = {
(local.root_id) = local.empty_list
"${local.root_id}-decommissioned" = local.empty_list
"${local.root_id}-sandboxes" = local.empty_list
"${local.root_id}-landing-zones" = local.empty_list
"${local.root_id}-platform" = local.empty_list
"${local.root_id}-connectivity" = local.empty_list
"${local.root_id}-management" = local.empty_list
"${local.root_id}-identity" = local.empty_list
"${local.root_id}-corp" = local.empty_list
"${local.root_id}-online" = local.empty_list
"${local.root_id}-sap" = local.empty_list
"${local.root_id}-demo-corp" = local.empty_list
"${local.root_id}-demo-online" = local.empty_list
"${local.root_id}-demo-sap" = local.empty_list
}
subscription_id_overrides_map = {
for key, value in local.subscription_id_overrides :
key == "root" ? local.root_id : "${local.root_id}-${key}" => value
}
es_subscription_ids_map = merge(
local.es_subscription_ids_defaults,
local.subscription_id_overrides_map,
)
}
# The following locals are used to determine Management Group
# placement for the platform Subscriptions. Preference of
# placement is based on the following:
# 1. Management
# 2. Connectivity
# 3. Identity
# If a duplicate value is found in any of these scopes, the
# value will be discarded as per the described logic.
locals {
subscription_ids_management = distinct(compact(concat(
[local.subscription_id_management],
local.es_subscription_ids_map["${local.root_id}-management"],
)))
subscription_ids_connectivity = [
for id in distinct(compact(concat(
[local.subscription_id_connectivity],
local.es_subscription_ids_map["${local.root_id}-connectivity"],
))) :
id
if !contains(local.subscription_ids_management, id)
]
subscription_ids_identity = [
for id in distinct(compact(concat(
[local.subscription_id_identity],
local.es_subscription_ids_map["${local.root_id}-identity"],
))) :
id
if !contains(local.subscription_ids_management, id) &&
!contains(local.subscription_ids_connectivity, id)
]
}
# The following locals are used to define the core Enterprise
# -scale Management Groups deployed by the module and uses
# logic to determine the full Management Group deployment
# hierarchy.
locals {
# Mandatory core Enterprise-scale Management Groups
es_core_landing_zones = {
(local.root_id) = {
display_name = local.root_name
parent_management_group_id = local.root_parent_id
subscription_ids = local.es_subscription_ids_map[local.root_id]
archetype_config = local.es_archetype_config_map[local.root_id]
}
"${local.root_id}-decommissioned" = {
display_name = "Decommissioned"
parent_management_group_id = local.root_id
subscription_ids = local.es_subscription_ids_map["${local.root_id}-decommissioned"]
archetype_config = local.es_archetype_config_map["${local.root_id}-decommissioned"]
}
"${local.root_id}-sandboxes" = {
display_name = "Sandboxes"
parent_management_group_id = local.root_id
subscription_ids = local.es_subscription_ids_map["${local.root_id}-sandboxes"]
archetype_config = local.es_archetype_config_map["${local.root_id}-sandboxes"]
}
"${local.root_id}-landing-zones" = {
display_name = "Landing Zones"
parent_management_group_id = local.root_id
subscription_ids = local.es_subscription_ids_map["${local.root_id}-landing-zones"]
archetype_config = local.es_archetype_config_map["${local.root_id}-landing-zones"]
}
"${local.root_id}-platform" = {
display_name = "Platform"
parent_management_group_id = local.root_id
subscription_ids = local.es_subscription_ids_map["${local.root_id}-platform"]
archetype_config = local.es_archetype_config_map["${local.root_id}-platform"]
}
"${local.root_id}-connectivity" = {
display_name = "Connectivity"
parent_management_group_id = "${local.root_id}-platform"
subscription_ids = local.subscription_ids_connectivity
archetype_config = local.es_archetype_config_map["${local.root_id}-connectivity"]
}
"${local.root_id}-management" = {
display_name = "Management"
parent_management_group_id = "${local.root_id}-platform"
subscription_ids = local.subscription_ids_management
archetype_config = local.es_archetype_config_map["${local.root_id}-management"]
}
"${local.root_id}-identity" = {
display_name = "Identity"
parent_management_group_id = "${local.root_id}-platform"
subscription_ids = local.subscription_ids_identity
archetype_config = local.es_archetype_config_map["${local.root_id}-identity"]
}
}
# Optional "Landing Zone" Enterprise-scale Management Groups
es_corp_landing_zones = {
"${local.root_id}-corp" = {
display_name = "Corp"
parent_management_group_id = "${local.root_id}-landing-zones"
subscription_ids = local.es_subscription_ids_map["${local.root_id}-corp"]
archetype_config = local.es_archetype_config_map["${local.root_id}-corp"]
}
}
es_online_landing_zones = {
"${local.root_id}-online" = {
display_name = "Online"
parent_management_group_id = "${local.root_id}-landing-zones"
subscription_ids = local.es_subscription_ids_map["${local.root_id}-online"]
archetype_config = local.es_archetype_config_map["${local.root_id}-online"]
}
}
es_sap_landing_zones = {
"${local.root_id}-sap" = {
display_name = "SAP"
parent_management_group_id = "${local.root_id}-landing-zones"
subscription_ids = local.es_subscription_ids_map["${local.root_id}-sap"]
archetype_config = local.es_archetype_config_map["${local.root_id}-sap"]
}
}
# Optional demo "Landing Zone" Enterprise-scale Management Groups
es_demo_landing_zones = {
"${local.root_id}-demo-corp" = {
display_name = "Corp (Demo)"
parent_management_group_id = "${local.root_id}-landing-zones"
subscription_ids = local.es_subscription_ids_map["${local.root_id}-demo-corp"]
archetype_config = local.es_archetype_config_map["${local.root_id}-demo-corp"]
}
"${local.root_id}-demo-online" = {
display_name = "Online (Demo)"
parent_management_group_id = "${local.root_id}-landing-zones"
subscription_ids = local.es_subscription_ids_map["${local.root_id}-demo-online"]
archetype_config = local.es_archetype_config_map["${local.root_id}-demo-online"]
}
"${local.root_id}-demo-sap" = {
display_name = "SAP (Demo)"
parent_management_group_id = "${local.root_id}-landing-zones"
subscription_ids = local.es_subscription_ids_map["${local.root_id}-demo-sap"]
archetype_config = local.es_archetype_config_map["${local.root_id}-demo-sap"]
}
}
# Logic to determine whether to include the core Enterprise-scale
# Management Groups as part of the deployment
es_core_landing_zones_to_include = local.deploy_core_landing_zones ? local.es_core_landing_zones : null
es_corp_landing_zones_to_include = local.deploy_core_landing_zones && local.deploy_corp_landing_zones ? local.es_corp_landing_zones : null
es_online_landing_zones_to_include = local.deploy_core_landing_zones && local.deploy_online_landing_zones ? local.es_online_landing_zones : null
es_sap_landing_zones_to_include = local.deploy_core_landing_zones && local.deploy_sap_landing_zones ? local.es_sap_landing_zones : null
# Logic to determine whether to include the demo "Landing Zone"
# Enterprise-scale Management Groups as part of the deployment
es_demo_landing_zones_to_include = local.deploy_core_landing_zones && local.deploy_demo_landing_zones ? local.es_demo_landing_zones : null
# Local map containing all Management Groups to deploy
es_landing_zones_merge = merge(
local.es_core_landing_zones_to_include,
local.es_corp_landing_zones_to_include,
local.es_online_landing_zones_to_include,
local.es_sap_landing_zones_to_include,
local.es_demo_landing_zones_to_include,
local.custom_landing_zones,
)
# Logic to auto-generate values for Management Groups if needed
# Allows the user to specify the Management Group ID when working with existing
# Management Groups, or uses standard naming pattern if set to null
es_landing_zones_map = {
for key, value in local.es_landing_zones_merge :
"${local.provider_path.management_groups}${key}" => {
id = key
display_name = value.display_name
parent_management_group_id = coalesce(value.parent_management_group_id, local.root_parent_id)
subscription_ids = local.strict_subscription_association ? value.subscription_ids : null
archetype_config = {
archetype_id = value.archetype_config.archetype_id
access_control = value.archetype_config.access_control
parameters = {
# The following logic merges parameter values from the connectivity,
# identity and management sub-modules with the archetype defaults
# (including custom_landing_zones) and archetype_config_overrides.
# These values are then passed to the parameters input variable of the
# archetypes sub-module.
for policy_name in toset(keys(merge(
lookup(module.connectivity_resources.configuration.archetype_config_overrides, key, local.parameter_map_default).parameters,
lookup(module.identity_resources.configuration.archetype_config_overrides, key, local.parameter_map_default).parameters,
lookup(module.management_resources.configuration.archetype_config_overrides, key, local.parameter_map_default).parameters,
value.archetype_config.parameters,
))) :
policy_name => merge(
lookup(lookup(module.connectivity_resources.configuration.archetype_config_overrides, key, local.parameter_map_default).parameters, policy_name, null),
lookup(lookup(module.identity_resources.configuration.archetype_config_overrides, key, local.parameter_map_default).parameters, policy_name, null),
lookup(lookup(module.management_resources.configuration.archetype_config_overrides, key, local.parameter_map_default).parameters, policy_name, null),
lookup(value.archetype_config.parameters, policy_name, null),
)
}
enforcement_mode = merge(
lookup(module.connectivity_resources.configuration.archetype_config_overrides, key, local.enforcement_mode_default).enforcement_mode,
lookup(module.identity_resources.configuration.archetype_config_overrides, key, local.enforcement_mode_default).enforcement_mode,
lookup(module.management_resources.configuration.archetype_config_overrides, key, local.enforcement_mode_default).enforcement_mode,
lookup(value.archetype_config, "enforcement_mode", local.empty_map)
)
}
}
}
# Logic to determine which subscriptions to associate with Management Groups in relaxed mode.
# Empty unless strict_subscription_association is set to false.
mg_sub_association_list = flatten([
for key, value in local.es_landing_zones_merge : [
for sid in value.subscription_ids :
{
management_group_name = key
subscription_id = sid
}
]
if !local.strict_subscription_association
])
# azurerm_management_group_subscription_association_enterprise_scale is used as the
# for_each value to create azurerm_management_group_subscription_association
# resources in relaxed mode.
# Empty unless strict_subscription_association is set to false.
azurerm_management_group_subscription_association_enterprise_scale = { for item in local.mg_sub_association_list :
"${local.provider_path.management_groups}${item.management_group_name}/subscriptions/${item.subscription_id}" => {
management_group_id = "${local.provider_path.management_groups}${item.management_group_name}"
subscription_id = "/subscriptions/${item.subscription_id}"
}
}
}
# The following locals are used to build the map of Management
# Groups to deploy at each level of the hierarchy.
locals {
azurerm_management_group_level_1 = {
for key, value in local.es_landing_zones_map :
key => value
if value.parent_management_group_id == local.root_parent_id
}
azurerm_management_group_level_2 = {
for key, value in local.es_landing_zones_map :
key => value
if contains(keys(azurerm_management_group.level_1), try(length(value.parent_management_group_id) > 0, false) ? "${local.provider_path.management_groups}${value.parent_management_group_id}" : local.empty_string)
}
azurerm_management_group_level_3 = {
for key, value in local.es_landing_zones_map :
key => value
if contains(keys(azurerm_management_group.level_2), try(length(value.parent_management_group_id) > 0, false) ? "${local.provider_path.management_groups}${value.parent_management_group_id}" : local.empty_string)
}
azurerm_management_group_level_4 = {
for key, value in local.es_landing_zones_map :
key => value
if contains(keys(azurerm_management_group.level_3), try(length(value.parent_management_group_id) > 0, false) ? "${local.provider_path.management_groups}${value.parent_management_group_id}" : local.empty_string)
}
azurerm_management_group_level_5 = {
for key, value in local.es_landing_zones_map :
key => value
if contains(keys(azurerm_management_group.level_4), try(length(value.parent_management_group_id) > 0, false) ? "${local.provider_path.management_groups}${value.parent_management_group_id}" : local.empty_string)
}
azurerm_management_group_level_6 = {
for key, value in local.es_landing_zones_map :
key => value
if contains(keys(azurerm_management_group.level_5), try(length(value.parent_management_group_id) > 0, false) ? "${local.provider_path.management_groups}${value.parent_management_group_id}" : local.empty_string)
}
}
# The following local is used to build the list of Management Groups
# that will have Diagnostic Settings deployed, based on boolean variable
# deploy_diagnostics_for_mg
locals {
azapi_mg_diagnostics = {
for mg_id in keys(local.es_landing_zones_map) :
mg_id => ""
if local.deploy_diagnostics_for_mg
}
}