forked from Azure/terraform-azurerm-caf-enterprise-scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locals.management_groups.tf
351 lines (345 loc) · 15.3 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
########################################################
# 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 = merge(
local.es_archetype_config_defaults,
local.archetype_config_overrides_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 = value.subscription_ids
archetype_config = {
archetype_id = value.archetype_config.archetype_id
access_control = value.archetype_config.access_control
parameters = merge(
try(module.connectivity_resources.configuration.archetype_config_overrides[key].parameters, null),
try(module.identity_resources.configuration.archetype_config_overrides[key].parameters, null),
try(module.management_resources.configuration.archetype_config_overrides[key].parameters, null),
value.archetype_config.parameters,
)
}
}
}
}
# 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 merge the Management Group
# configuration from each level back into a single data
# object to return in the module outputs.
locals {
es_management_group_output = merge(
azurerm_management_group.level_1,
azurerm_management_group.level_2,
azurerm_management_group.level_3,
azurerm_management_group.level_4,
azurerm_management_group.level_5,
azurerm_management_group.level_6,
)
}