Skip to content

Commit de04754

Browse files
Felix Ziegerfelixzieger
authored andcommitted
feat: configurable metering assignment scope
1 parent aa67c1d commit de04754

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

main.tf

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ data "azurerm_management_group" "replicator_custom_role_scope" {
1717
}
1818

1919
data "azurerm_management_group" "replicator_assignment_scopes" {
20-
for_each = var.replicator_assignment_scopes
20+
for_each = toset(var.replicator_assignment_scopes)
21+
name = each.key
22+
}
23+
24+
data "azurerm_management_group" "metering_assignment_scopes" {
25+
for_each = toset(var.metering_assignment_scopes)
2126
name = each.key
2227
}
2328

2429
locals {
2530
replicator_assignment_scopes = [
2631
for management_group in data.azurerm_management_group.replicator_assignment_scopes : management_group.id
2732
]
33+
metering_assignment_scopes = [
34+
for management_group in data.azurerm_management_group.metering_assignment_scopes : management_group.id
35+
]
2836
}
2937

3038
data "azuread_client_config" "current" {}
@@ -46,14 +54,15 @@ module "metering_service_principal" {
4654
source = "./modules/meshcloud-metering-service-principal/"
4755

4856
service_principal_name = var.metering_service_principal_name
49-
assignment_scope = data.azuread_client_config.current.tenant_id
57+
assignment_scopes = local.metering_assignment_scopes
5058
}
5159

5260
module "sso_service_principal" {
5361
count = var.sso_enabled ? 1 : 0
5462
source = "./modules/meshcloud-sso/"
5563

56-
service_principal_name = var.metering_service_principal_name
64+
service_principal_name = var.sso_service_principal_name
65+
meshstack_redirect_uri = var.sso_meshstack_redirect_uri
5766
}
5867

5968
# facilitate migration from v0.1.0 of the module

modules/meshcloud-metering-service-principal/module.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ terraform {
1818
//---------------------------------------------------------------------------
1919
// Assign Cost Management reader role to the enterprise application
2020
//---------------------------------------------------------------------------
21-
# For now we are using the following built-in role
2221
resource "azurerm_role_assignment" "meshcloud_metering" {
23-
scope = var.assignment_scope
22+
for_each = toset(var.assignment_scopes)
23+
scope = each.key
2424
role_definition_name = "Cost Management Reader"
2525
principal_id = azuread_service_principal.meshcloud_metering.id
2626
depends_on = [azuread_service_principal.meshcloud_metering]

modules/meshcloud-metering-service-principal/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ variable "service_principal_name" {
33
description = "Service principal name. Must be unique per Entra ID."
44
}
55

6-
variable "assignment_scope" {
7-
type = string
8-
description = "The scope to which Service Principal permissions should be assigned to. Usually this is the management group id of form `/providers/Microsoft.Management/managementGroups/<tenantId>` that sits atop the subscriptions."
6+
variable "assignment_scopes" {
7+
type = list(string)
8+
description = "The scopes to which Service Principal permissions should be assigned to. Usually this is the management group id of form `/providers/Microsoft.Management/managementGroups/<tenantId>` that sits atop the subscriptions."
99
}

modules/meshcloud-replicator-service-principal/module.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ resource "azuread_service_principal" "meshcloud_replicator" {
163163
// Assign the created ARM role to the Enterprise application
164164
//---------------------------------------------------------------------------
165165
resource "azurerm_role_assignment" "meshcloud_replicator" {
166-
for_each = var.assignment_scopes
166+
for_each = toset(var.assignment_scopes)
167167
scope = each.key
168168
role_definition_id = azurerm_role_definition.meshcloud_replicator.role_definition_resource_id
169169
principal_id = azuread_service_principal.meshcloud_replicator.id

variables.tf

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ variable "replicator_service_principal_name" {
44
description = "Service principal for managing subscriptions. Replicator is the name of the meshStack component. Name must be unique per Entra ID."
55
}
66

7+
variable "replicator_custom_role_scope" {
8+
type = string
9+
description = "Name or UUID of the Management Group of the replicator custom role definition. The custom role definition must be available for all assignment scopes."
10+
}
11+
12+
variable "replicator_assignment_scopes" {
13+
type = list(string)
14+
description = "Names or UUIDs of the Management Groups which replicator should manage."
15+
}
16+
717
variable "metering_service_principal_name" {
818
type = string
919
default = "kraken"
1020
description = "Service principal for collecting cost data. Kraken ist the name of the meshStack component. Name must be unique per Entra ID."
1121
}
1222

13-
variable "replicator_assignment_scopes" {
23+
variable "metering_assignment_scopes" {
1424
type = list(string)
15-
description = "Names or UUIDs of the Management Groups which replicator should manage."
25+
description = "Names or UUIDs of the Management Groups that kraken should collect costs for."
1626
}
1727

1828
variable "sso_enabled" {
@@ -21,6 +31,18 @@ variable "sso_enabled" {
2131
description = "Whether to create SSO Service Principal or not."
2232
}
2333

34+
variable "sso_service_principal_name" {
35+
type = string
36+
default = "sso"
37+
description = "Service principal for Entra ID SSO. Name must be unique per Entra ID."
38+
}
39+
40+
variable "sso_meshstack_redirect_uri" {
41+
type = string
42+
default = "<replace with uri>"
43+
description = "Redirect URI that was provided by meshcloud. It is individual per meshStack."
44+
}
45+
2446
# ---------------------------------------------------------------------------------------------------------------------
2547
# OPTIONAL PARAMETERS
2648
# These parameters have reasonable defaults.
@@ -32,12 +54,6 @@ variable "replicator_enabled" {
3254
description = "Whether to create replicator Service Principal or not."
3355
}
3456

35-
variable "replicator_custom_role_scope" {
36-
type = string
37-
default = "Tenant Root Group"
38-
description = "Name or UUID of the Management Group of the replicator custom role definition. The custom role definition must be available for all assignment scopes."
39-
}
40-
4157
variable "replicator_rg_enabled" {
4258
type = bool
4359
default = false

0 commit comments

Comments
 (0)