main.tf (119 lines of code) (raw):
resource "azurerm_container_registry" "this" {
location = var.location
name = var.name
resource_group_name = var.resource_group_name
sku = var.sku
admin_enabled = var.admin_enabled
anonymous_pull_enabled = var.anonymous_pull_enabled
data_endpoint_enabled = var.data_endpoint_enabled
export_policy_enabled = var.export_policy_enabled
network_rule_bypass_option = var.network_rule_bypass_option
public_network_access_enabled = var.public_network_access_enabled
quarantine_policy_enabled = var.quarantine_policy_enabled
retention_policy_in_days = var.sku == "Premium" ? var.retention_policy_in_days : null
tags = var.tags
trust_policy_enabled = var.enable_trust_policy
zone_redundancy_enabled = var.sku == "Premium" ? var.zone_redundancy_enabled : false
dynamic "encryption" {
for_each = var.customer_managed_key != null ? { this = var.customer_managed_key } : {}
content {
identity_client_id = data.azurerm_user_assigned_identity.this[0].client_id
key_vault_key_id = data.azurerm_key_vault_key.this[0].id
}
}
dynamic "georeplications" {
for_each = local.ordered_geo_replications
content {
location = georeplications.value.location
regional_endpoint_enabled = georeplications.value.regional_endpoint_enabled
tags = georeplications.value.tags
zone_redundancy_enabled = georeplications.value.zone_redundancy_enabled
}
}
dynamic "identity" {
for_each = local.managed_identities.system_assigned_user_assigned
content {
type = identity.value.type
identity_ids = identity.value.user_assigned_resource_ids
}
}
# Only one network_rule_set block is allowed.
# Create it if the variable is not null.
dynamic "network_rule_set" {
for_each = var.network_rule_set != null ? { this = var.network_rule_set } : {}
content {
default_action = network_rule_set.value.default_action
dynamic "ip_rule" {
for_each = network_rule_set.value.ip_rule
content {
action = ip_rule.value.action
ip_range = ip_rule.value.ip_range
}
}
}
}
lifecycle {
precondition {
condition = var.zone_redundancy_enabled && var.sku == "Premium" || !var.zone_redundancy_enabled
error_message = "The Premium SKU is required if zone redundancy is enabled."
}
precondition {
condition = var.network_rule_set != null && var.sku == "Premium" || var.network_rule_set == null
error_message = "The Premium SKU is required if a network rule set is defined."
}
precondition {
condition = var.customer_managed_key != null && var.sku == "Premium" || var.customer_managed_key == null
error_message = "The Premium SKU is required if a customer managed key is defined."
}
precondition {
condition = var.customer_managed_key != null && contains(var.managed_identities.user_assigned_resource_ids, try(var.customer_managed_key.user_assigned_identity.resource_id, "null")) || var.customer_managed_key == null
error_message = "The user assigned managed identity for the customer managed key encryption must be assigned to the container registry."
}
}
}
resource "azurerm_management_lock" "this" {
count = var.lock != null ? 1 : 0
lock_level = var.lock.kind
name = coalesce(var.lock.name, "lock-${var.name}")
scope = azurerm_container_registry.this.id
notes = var.lock.kind == "CanNotDelete" ? "Cannot delete the resource or its child resources." : "Cannot delete or modify the resource or its child resources."
}
resource "azurerm_role_assignment" "this" {
for_each = var.role_assignments
principal_id = each.value.principal_id
scope = azurerm_container_registry.this.id
condition = each.value.condition
condition_version = each.value.condition_version
delegated_managed_identity_resource_id = each.value.delegated_managed_identity_resource_id
principal_type = each.value.principal_type
role_definition_id = strcontains(lower(each.value.role_definition_id_or_name), lower(local.role_definition_resource_substring)) ? each.value.role_definition_id_or_name : null
role_definition_name = strcontains(lower(each.value.role_definition_id_or_name), lower(local.role_definition_resource_substring)) ? null : each.value.role_definition_id_or_name
skip_service_principal_aad_check = each.value.skip_service_principal_aad_check
}
resource "azurerm_monitor_diagnostic_setting" "this" {
for_each = var.diagnostic_settings
name = each.value.name != null ? each.value.name : "diag-${var.name}"
target_resource_id = azurerm_container_registry.this.id
eventhub_authorization_rule_id = each.value.event_hub_authorization_rule_resource_id
eventhub_name = each.value.event_hub_name
log_analytics_destination_type = each.value.log_analytics_destination_type
log_analytics_workspace_id = each.value.workspace_resource_id
partner_solution_id = each.value.marketplace_partner_resource_id
storage_account_id = each.value.storage_account_resource_id
dynamic "enabled_log" {
for_each = each.value.log_categories
content {
category = enabled_log.value
}
}
dynamic "enabled_log" {
for_each = each.value.log_groups
content {
category_group = enabled_log.value
}
}
dynamic "metric" {
for_each = each.value.metric_categories
content {
category = metric.value
}
}
}