forked from Azure/terraform-azurerm-caf-enterprise-scale
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.policy_assignments.tf
58 lines (50 loc) · 2.82 KB
/
resources.policy_assignments.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
resource "azurerm_management_group_policy_assignment" "enterprise_scale" {
for_each = local.azurerm_management_group_policy_assignment_enterprise_scale
# Mandatory resource attributes
# The policy assignment name length must not exceed '24' characters, but Terraform plan is unable to validate this in the plan stage. The following logic forces an error during plan if an invalid name length is specified.
name = tonumber(length(each.value.template.name) > 24 ? "The policy assignment name '${each.value.template.name}' is invalid. The policy assignment name length must not exceed '24' characters." : length(each.value.template.name)) > 24 ? null : each.value.template.name
management_group_id = each.value.scope_id
policy_definition_id = each.value.template.properties.policyDefinitionId
# Optional resource attributes
location = try(each.value.template.location, null)
description = try(each.value.template.properties.description, "${each.value.template.name} Policy Assignment at scope ${each.value.scope_id}")
display_name = try(each.value.template.properties.displayName, each.value.template.name)
metadata = try(length(each.value.template.properties.metadata) > 0, false) ? jsonencode(each.value.template.properties.metadata) : null
parameters = try(length(each.value.parameters) > 0, false) ? jsonencode(each.value.parameters) : null
not_scopes = try(each.value.template.properties.notScopes, local.empty_list)
enforce = each.value.enforcement_mode
# Dynamic configuration blocks
# The identity block only supports a single value
# for type = "SystemAssigned" so the following logic
# ensures the block is only created when this value
# is specified in the source template
dynamic "identity" {
for_each = {
for ik, iv in try(each.value.template.identity, local.empty_map) :
ik => iv
if lower(iv) == "systemassigned"
}
content {
type = "SystemAssigned"
}
}
# Set explicit dependency on Management Group, Policy Definition and Policy Set Definition deployments
depends_on = [
time_sleep.after_azurerm_management_group,
time_sleep.after_azurerm_policy_definition,
time_sleep.after_azurerm_policy_set_definition,
]
}
resource "time_sleep" "after_azurerm_policy_assignment" {
depends_on = [
time_sleep.after_azurerm_management_group,
time_sleep.after_azurerm_policy_definition,
time_sleep.after_azurerm_policy_set_definition,
azurerm_management_group_policy_assignment.enterprise_scale,
]
triggers = {
"azurerm_management_group_policy_assignment_enterprise_scale" = jsonencode(keys(azurerm_management_group_policy_assignment.enterprise_scale))
}
create_duration = local.create_duration_delay["after_azurerm_policy_assignment"]
destroy_duration = local.destroy_duration_delay["after_azurerm_policy_assignment"]
}