-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.tf
More file actions
96 lines (81 loc) · 2.87 KB
/
main.tf
File metadata and controls
96 lines (81 loc) · 2.87 KB
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
data "azurerm_client_config" "current" {}
data "azuread_client_config" "current" {}
data "azuread_user" "current" {
object_id = data.azuread_client_config.current.object_id
}
# Resource group creation is idempotent in ARM (PUT). This will create the RG if it doesn't exist,
# or update tags if it already exists.
resource "azapi_resource" "resource_group" {
type = "Microsoft.Resources/resourceGroups@2022-09-01"
name = var.resource_group_name
location = var.location
parent_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
body = jsonencode({
tags = var.tags
})
response_export_values = [
"id",
"name"
]
}
resource "random_string" "suffix" {
length = var.random_suffix_length
upper = false
special = false
numeric = true
keepers = {
resource_group_name = var.resource_group_name
location = var.location
capacity_base = var.capacity_name
sku_name = var.sku_name
sku_tier = var.sku_tier
}
}
locals {
rg_id = azapi_resource.resource_group.id
suffix = var.append_random_suffix ? random_string.suffix.result : ""
capacity_name = var.append_random_suffix ? "${var.capacity_name}-${local.suffix}" : var.capacity_name
# AzAPI schema validation for Microsoft.Fabric/capacities currently enforces a strict name pattern:
# ^[a-z][a-z0-9]*$
# To keep tfvars friendly (allowing '-' etc.), we sanitize the requested name for the ARM resource name.
capacity_name_sanitized_raw = join("", regexall("[a-z0-9]", lower(local.capacity_name)))
capacity_name_sanitized = can(regex("^[a-z]", local.capacity_name_sanitized_raw)) ? local.capacity_name_sanitized_raw : "fc${local.capacity_name_sanitized_raw}"
admin_members_effective = (
length(var.admin_members) > 0
? var.admin_members
: (var.use_current_user_as_admin ? [data.azuread_user.current.user_principal_name] : [])
)
}
# Microsoft Fabric capacity (ARM resource provider: Microsoft.Fabric)
# API version reference: https://learn.microsoft.com/azure/templates/Microsoft.Fabric/2023-11-01/capacities
resource "azapi_resource" "fabric_capacity" {
type = "Microsoft.Fabric/capacities@2023-11-01"
name = local.capacity_name_sanitized
location = var.location
parent_id = local.rg_id
lifecycle {
precondition {
condition = length(local.admin_members_effective) > 0
error_message = "No Fabric admins configured. Set admin_members (list of UPNs) or enable use_current_user_as_admin to auto-resolve the current user."
}
}
body = jsonencode({
sku = {
name = var.sku_name
tier = var.sku_tier
}
properties = {
administration = {
members = local.admin_members_effective
}
}
tags = var.tags
})
response_export_values = [
"id",
"name"
]
depends_on = [
azapi_resource.resource_group
]
}