diff --git a/infra/modules/azure_app_service_plan_autoscaler/data.tf b/infra/modules/azure_app_service_plan_autoscaler/data.tf index 5cb2899c..f38eb65e 100644 --- a/infra/modules/azure_app_service_plan_autoscaler/data.tf +++ b/infra/modules/azure_app_service_plan_autoscaler/data.tf @@ -1,12 +1,12 @@ data "azurerm_linux_web_app" "this" { - count = local.is_app_service ? 1 : 0 + count = var.target_service.app_service_name != null && local.is_app_service ? 1 : 0 resource_group_name = var.resource_group_name name = var.target_service.app_service_name } data "azurerm_linux_function_app" "this" { - count = local.is_function_app ? 1 : 0 + count = var.target_service.function_app_name != null && local.is_function_app ? 1 : 0 resource_group_name = var.resource_group_name name = var.target_service.function_app_name diff --git a/infra/modules/azure_app_service_plan_autoscaler/locals.tf b/infra/modules/azure_app_service_plan_autoscaler/locals.tf index 706e20da..6feaf407 100644 --- a/infra/modules/azure_app_service_plan_autoscaler/locals.tf +++ b/infra/modules/azure_app_service_plan_autoscaler/locals.tf @@ -3,13 +3,13 @@ locals { is_app_service = var.target_service.app_service_name != null is_function_app = var.target_service.function_app_name != null - base_name = local.is_app_service ? data.azurerm_linux_web_app.this[0].name : data.azurerm_linux_function_app.this[0].name + base_name = local.is_app_service ? var.target_service.app_service_name : var.target_service.function_app_name autoscale_name = var.autoscale_name == null ? replace(replace(replace(local.base_name, "fn", "as"), "func", "as"), "app", "as") : var.autoscale_name - resource_group_name = local.is_app_service ? data.azurerm_linux_web_app.this[0].resource_group_name : data.azurerm_linux_function_app.this[0].resource_group_name - location = local.is_app_service ? data.azurerm_linux_web_app.this[0].location : data.azurerm_linux_function_app.this[0].location - app_service_id = local.is_app_service ? data.azurerm_linux_web_app.this[0].id : data.azurerm_linux_function_app.this[0].id - app_service_plan_id = local.is_app_service ? data.azurerm_linux_web_app.this[0].service_plan_id : data.azurerm_linux_function_app.this[0].service_plan_id + resource_group_name = var.resource_group_name + location = local.is_app_service ? coalesce(var.target_service.location, data.azurerm_linux_web_app.this[0].location) : coalesce(var.target_service.location, data.azurerm_linux_function_app.this[0].location) + app_service_id = local.is_app_service ? coalesce(var.target_service.app_service_id, data.azurerm_linux_web_app.this[0].id) : coalesce(var.target_service.function_app_id, data.azurerm_linux_function_app.this[0].id) + app_service_plan_id = local.is_app_service ? coalesce(var.target_service.app_service_plan_id, data.azurerm_linux_web_app.this[0].service_plan_id) : coalesce(var.target_service.app_service_plan_id, data.azurerm_linux_function_app.this[0].service_plan_id) requests_rule_increase = { metric_trigger = { diff --git a/infra/modules/azure_app_service_plan_autoscaler/variables.tf b/infra/modules/azure_app_service_plan_autoscaler/variables.tf index 4674205a..def4ab12 100644 --- a/infra/modules/azure_app_service_plan_autoscaler/variables.tf +++ b/infra/modules/azure_app_service_plan_autoscaler/variables.tf @@ -16,13 +16,54 @@ variable "autoscale_name" { variable "target_service" { type = object({ - app_service_name = optional(string) - function_app_name = optional(string) + app_service_id = optional(string) + app_service_name = optional(string) + function_app_id = optional(string) + function_app_name = optional(string) + app_service_plan_id = optional(string) + location = optional(string) }) + description = < account } + for_each = { for account in local.accounts : "${account.resource_group_name}|${account.account_name}" => account if account.account_name != null } name = each.value.account_name resource_group_name = each.value.resource_group_name diff --git a/infra/modules/azure_role_assignments/modules/cosmos/locals.tf b/infra/modules/azure_role_assignments/modules/cosmos/locals.tf index 02f2ce9d..b7dac4fb 100644 --- a/infra/modules/azure_role_assignments/modules/cosmos/locals.tf +++ b/infra/modules/azure_role_assignments/modules/cosmos/locals.tf @@ -8,7 +8,8 @@ locals { for assignment in flatten([ for entry in var.cosmos : [ for collection in entry.collections : { - account_name = entry.account_name + account_name = coalesce(entry.account_name, reverse(split("/", entry.account_id))[0]) + account_id = coalesce(entry.account_id, data.azurerm_cosmosdb_account.cosmos["${entry.resource_group_name}|${coalesce(entry.account_name, reverse(split("/", entry.account_id))[0])}"].id) resource_group_name = entry.resource_group_name role = entry.role database = entry.database diff --git a/infra/modules/azure_role_assignments/modules/cosmos/main.tf b/infra/modules/azure_role_assignments/modules/cosmos/main.tf index 0f4c9b21..9292811a 100644 --- a/infra/modules/azure_role_assignments/modules/cosmos/main.tf +++ b/infra/modules/azure_role_assignments/modules/cosmos/main.tf @@ -3,7 +3,7 @@ resource "azurerm_cosmosdb_sql_role_assignment" "this" { resource_group_name = each.value.resource_group_name account_name = each.value.account_name - role_definition_id = "${data.azurerm_cosmosdb_account.cosmos["${each.value.resource_group_name}|${each.value.account_name}"].id}/sqlRoleDefinitions/${local.role_definition_id[lower(each.value.role)]}" + role_definition_id = "${each.value.account_id}/sqlRoleDefinitions/${local.role_definition_id[lower(each.value.role)]}" principal_id = var.principal_id - scope = "${data.azurerm_cosmosdb_account.cosmos["${each.value.resource_group_name}|${each.value.account_name}"].id}${each.value.scope}" + scope = "${each.value.account_id}${each.value.scope}" } \ No newline at end of file diff --git a/infra/modules/azure_role_assignments/modules/cosmos/variables.tf b/infra/modules/azure_role_assignments/modules/cosmos/variables.tf index b34b0413..42d375e7 100644 --- a/infra/modules/azure_role_assignments/modules/cosmos/variables.tf +++ b/infra/modules/azure_role_assignments/modules/cosmos/variables.tf @@ -6,7 +6,8 @@ variable "principal_id" { variable "cosmos" { description = "A list of CosmosDB role assignments" type = list(object({ - account_name = string + account_name = optional(string) + account_id = optional(string) resource_group_name = string role = string database = optional(string, "*") @@ -26,6 +27,7 @@ variable "cosmos" { for entry in var.cosmos : [ for collection in entry.collections : { account_name = entry.account_name + account_id = entry.account_id resource_group_name = entry.resource_group_name role = entry.role database = entry.database @@ -38,6 +40,7 @@ variable "cosmos" { for entry in var.cosmos : [ for collection in entry.collections : { account_name = entry.account_name + account_id = entry.account_id resource_group_name = entry.resource_group_name role = entry.role database = entry.database @@ -49,5 +52,12 @@ variable "cosmos" { error_message = "Each assignment must be unique." } + validation { + condition = alltrue([ + for assignment in var.cosmos : (assignment.account_name != null || assignment.account_id != null) + ]) + error_message = "Either account_name or account_id must be populated." + } + default = [] } \ No newline at end of file diff --git a/infra/modules/azure_role_assignments/modules/event_hub/data.tf b/infra/modules/azure_role_assignments/modules/event_hub/data.tf index d0954146..5f16ad0a 100644 --- a/infra/modules/azure_role_assignments/modules/event_hub/data.tf +++ b/infra/modules/azure_role_assignments/modules/event_hub/data.tf @@ -1,12 +1,5 @@ data "azurerm_eventhub_namespace" "this" { - for_each = { for namespace in local.namespaces : "${namespace.resource_group_name}|${namespace.namespace_name}" => namespace } + for_each = { for namespace in local.namespaces : "${namespace.resource_group_name}|${namespace.namespace_name}" => namespace if namespace.namespace_name == null } name = each.value.namespace_name resource_group_name = each.value.resource_group_name } - -data "azurerm_eventhub" "this" { - for_each = { for event_hub in local.event_hubs : "${event_hub.namespace_name}|${event_hub.event_hub_name}" => event_hub if event_hub.event_hub_name != "*" } - name = each.value.event_hub_name - namespace_name = each.value.namespace_name - resource_group_name = each.value.resource_group_name -} diff --git a/infra/modules/azure_role_assignments/modules/event_hub/locals.tf b/infra/modules/azure_role_assignments/modules/event_hub/locals.tf index 5142ef96..bb2a41f4 100644 --- a/infra/modules/azure_role_assignments/modules/event_hub/locals.tf +++ b/infra/modules/azure_role_assignments/modules/event_hub/locals.tf @@ -1,15 +1,16 @@ locals { namespaces = distinct([for assignment in var.event_hub : { namespace_name = assignment.namespace_name, resource_group_name = assignment.resource_group_name }]) - event_hubs = distinct([for assignment in local.assignments : { event_hub_name = assignment.event_hub_name, namespace_name = assignment.namespace_name, resource_group_name = assignment.resource_group_name }]) assignments = { for assignment in flatten([ for entry in var.event_hub : [ for event_hub_name in entry.event_hub_names : { namespace_name = entry.namespace_name + namespace_id = coalesce(entry.namespace_id, data.azurerm_eventhub_namespace.this["${each.value.resource_group_name}|${each.value.namespace_name}"].id) resource_group_name = entry.resource_group_name role = entry.role event_hub_name = event_hub_name + event_hub_id = entry.namespace_id != null ? "${entry.namespace_id}/eventhubs/${event_hub_name}" : null } ] ]) : "${assignment.namespace_name}|${assignment.event_hub_name}|${assignment.role}" => assignment diff --git a/infra/modules/azure_role_assignments/modules/event_hub/main.tf b/infra/modules/azure_role_assignments/modules/event_hub/main.tf index 38560219..e54e12a5 100644 --- a/infra/modules/azure_role_assignments/modules/event_hub/main.tf +++ b/infra/modules/azure_role_assignments/modules/event_hub/main.tf @@ -1,6 +1,6 @@ resource "azurerm_role_assignment" "this" { for_each = local.assignments role_definition_name = local.role_definition_name[lower(each.value.role)] - scope = each.value.event_hub_name == "*" ? data.azurerm_eventhub_namespace.this["${each.value.resource_group_name}|${each.value.namespace_name}"].id : data.azurerm_eventhub.this["${each.value.namespace_name}|${each.value.event_hub_name}"].id + scope = each.value.event_hub_name == "*" ? each.value.namespace_id : each.value.event_hub_id principal_id = var.principal_id } \ No newline at end of file diff --git a/infra/modules/azure_role_assignments/modules/event_hub/variables.tf b/infra/modules/azure_role_assignments/modules/event_hub/variables.tf index bd2bf0a4..28994ca4 100644 --- a/infra/modules/azure_role_assignments/modules/event_hub/variables.tf +++ b/infra/modules/azure_role_assignments/modules/event_hub/variables.tf @@ -4,47 +4,48 @@ variable "principal_id" { } variable "event_hub" { - description = "A list of event hub role assignments" type = list(object({ - namespace_name = string + namespace_name = optional(string) + namespace_id = optional(string) resource_group_name = string - event_hub_names = optional(list(string), ["*"]) + event_hub_names = optional(list(string)) + event_hub_ids = optional(list(string)) role = string })) + description = <