|
| 1 | +data "azurerm_client_config" "current" {} |
| 2 | + |
| 3 | +resource "azapi_resource" "resource_group" { |
| 4 | + type = "Microsoft.Resources/resourceGroups@2022-09-01" |
| 5 | + name = var.resource_group_name |
| 6 | + location = var.location |
| 7 | + parent_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}" |
| 8 | + |
| 9 | + body = jsonencode({ |
| 10 | + tags = var.tags |
| 11 | + }) |
| 12 | + |
| 13 | + response_export_values = [ |
| 14 | + "id", |
| 15 | + "name" |
| 16 | + ] |
| 17 | +} |
| 18 | + |
| 19 | +resource "random_string" "suffix" { |
| 20 | + length = var.random_suffix_length |
| 21 | + upper = false |
| 22 | + special = false |
| 23 | + numeric = true |
| 24 | + |
| 25 | + keepers = { |
| 26 | + resource_group_name = var.resource_group_name |
| 27 | + location = var.location |
| 28 | + base_name = var.foundry_account_name |
| 29 | + sku = var.sku_name |
| 30 | + allow_project_management = tostring(var.allow_project_management) |
| 31 | + public_network_access = tostring(var.public_network_access_enabled) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +locals { |
| 36 | + suffix = var.append_random_suffix ? random_string.suffix.result : "" |
| 37 | + name_raw = var.append_random_suffix ? "${var.foundry_account_name}-${local.suffix}" : var.foundry_account_name |
| 38 | + |
| 39 | + custom_subdomain_name_effective = ( |
| 40 | + (var.custom_subdomain_name != null ? trimspace(var.custom_subdomain_name) : "") != "" |
| 41 | + ? trimspace(var.custom_subdomain_name) |
| 42 | + : "cog-${replace(lower(local.name_raw), "_", "-")}" # best-effort derivation |
| 43 | + ) |
| 44 | + |
| 45 | + project_name_effective = ( |
| 46 | + (var.project_name != null ? trimspace(var.project_name) : "") != "" |
| 47 | + ? trimspace(var.project_name) |
| 48 | + : substr(replace(local.name_raw, "-", ""), 0, 64) |
| 49 | + ) |
| 50 | + |
| 51 | + project_display_name_effective = ( |
| 52 | + (var.project_display_name != null ? trimspace(var.project_display_name) : "") != "" |
| 53 | + ? trimspace(var.project_display_name) |
| 54 | + : "project-${local.name_raw}" |
| 55 | + ) |
| 56 | + |
| 57 | + public_network_access = var.public_network_access_enabled ? "Enabled" : "Disabled" |
| 58 | +} |
| 59 | + |
| 60 | +resource "azapi_resource" "foundry_account" { |
| 61 | + # Use the newer api-version so Foundry-specific properties (like allowProjectManagement) |
| 62 | + # are recognized by the resource provider. Schema validation is disabled to avoid |
| 63 | + # AzAPI embedded schema lagging the service. |
| 64 | + type = "Microsoft.CognitiveServices/accounts@2025-06-01" |
| 65 | + schema_validation_enabled = false |
| 66 | + name = local.name_raw |
| 67 | + location = var.location |
| 68 | + parent_id = azapi_resource.resource_group.id |
| 69 | + |
| 70 | + identity { |
| 71 | + type = var.enable_system_assigned_identity ? "SystemAssigned" : "None" |
| 72 | + } |
| 73 | + |
| 74 | + body = jsonencode({ |
| 75 | + kind = "AIServices" |
| 76 | + properties = { |
| 77 | + allowProjectManagement = var.allow_project_management |
| 78 | + customSubDomainName = local.custom_subdomain_name_effective |
| 79 | + publicNetworkAccess = local.public_network_access |
| 80 | + disableLocalAuth = false |
| 81 | + dynamicThrottlingEnabled = false |
| 82 | + restrictOutboundNetworkAccess = false |
| 83 | + } |
| 84 | + sku = { |
| 85 | + name = var.sku_name |
| 86 | + } |
| 87 | + tags = var.tags |
| 88 | + }) |
| 89 | + |
| 90 | + response_export_values = ["*"] |
| 91 | + |
| 92 | + depends_on = [ |
| 93 | + azapi_resource.resource_group |
| 94 | + ] |
| 95 | +} |
| 96 | + |
| 97 | +resource "azapi_resource" "foundry_project" { |
| 98 | + count = var.create_project ? 1 : 0 |
| 99 | + |
| 100 | + # The projects subresource may not have schema coverage in AzAPI; disable schema validation. |
| 101 | + type = "Microsoft.CognitiveServices/accounts/projects@2025-06-01" |
| 102 | + schema_validation_enabled = false |
| 103 | + name = local.project_name_effective |
| 104 | + location = var.location |
| 105 | + parent_id = azapi_resource.foundry_account.id |
| 106 | + |
| 107 | + identity { |
| 108 | + type = "SystemAssigned" |
| 109 | + } |
| 110 | + |
| 111 | + body = jsonencode({ |
| 112 | + properties = { |
| 113 | + displayName = local.project_display_name_effective |
| 114 | + description = var.project_description |
| 115 | + } |
| 116 | + tags = var.tags |
| 117 | + }) |
| 118 | + |
| 119 | + response_export_values = ["*"] |
| 120 | + |
| 121 | + depends_on = [ |
| 122 | + azapi_resource.foundry_account |
| 123 | + ] |
| 124 | +} |
0 commit comments