Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
Shame Terraform doesn't do recursion nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Cheney committed Jul 31, 2020
1 parent cdd1b59 commit f833b3c
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Note that this module is subject to breaking change as the management_groups var

### Deploying the Definitions

It is very simple to get the policies deployed:
It is very simple to get the management groups deployed:

```terraform
module "management_groups" {
Expand Down
19 changes: 19 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
required_version = ">= 0.13.0"
}

data "azurerm_subscription" "current" {}

data "azurerm_management_group" "tenant_root_group" {
name = data.azurerm_subscription.current.tenant_id
}

module "mg1" {
source = "./modules/mg1"
for_each = var.management_groups

level = 1
display_name = each.key
parent_management_group_id = data.azurerm_management_group.tenant_root_group.id
children = each.value
}
61 changes: 61 additions & 0 deletions modules/mg1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
variable "level" {
type = number
}

variable "display_name" {}

variable "parent_management_group_id" {}

variable "children" {
type = map
}

locals {
subscription_ids = flatten([
for key, value in var.children :
key == "subscription_ids" ? toset(value) : []
])

children = {
for key, value in var.children :
key => value if key != "subscription_ids"
}

next_level = var.level + 1
}

resource "azurerm_management_group" "mg" {
display_name = var.display_name
parent_management_group_id = var.parent_management_group_id
subscription_ids = local.subscription_ids
}

module "mg2" {
source = "../mg2"
for_each = local.children // var.level + 1 == 2 ? local.children : {}

level = local.next_level
display_name = each.key
parent_management_group_id = azurerm_management_group.mg.id
children = each.value
}

output "management_groups" {
value = flatten([
merge(azurerm_management_group.mg, { level = var.level }),
[
for name in keys(local.children) :
module.mg2[name].management_groups
]
])
}

output "management_group_id" {
value = merge({
id = azurerm_management_group.mg.id
},
{
for name in keys(local.children) :
name => module.mg2[name].management_group_id
})
}
61 changes: 61 additions & 0 deletions modules/mg2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
variable "level" {
type = number
}

variable "display_name" {}

variable "parent_management_group_id" {}

variable "children" {
type = map
}

locals {
subscription_ids = flatten([
for key, value in var.children :
key == "subscription_ids" ? toset(value) : []
])

children = {
for key, value in var.children :
key => value if key != "subscription_ids"
}

next_level = var.level + 1
}

resource "azurerm_management_group" "mg" {
display_name = var.display_name
parent_management_group_id = var.parent_management_group_id
subscription_ids = local.subscription_ids
}

module "mg3" {
source = "../mg3"
for_each = local.children // var.level + 1 == 2 ? local.children : {}

level = local.next_level
display_name = each.key
parent_management_group_id = azurerm_management_group.mg.id
children = each.value
}

output "management_groups" {
value = flatten([
merge(azurerm_management_group.mg, { level = var.level }),
[
for name in keys(local.children) :
module.mg3[name].management_groups
]
])
}

output "management_group_id" {
value = merge({
id = azurerm_management_group.mg.id
},
{
for name in keys(local.children) :
name => module.mg3[name].management_group_id
})
}
61 changes: 61 additions & 0 deletions modules/mg3/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
variable "level" {
type = number
}

variable "display_name" {}

variable "parent_management_group_id" {}

variable "children" {
type = map
}

locals {
subscription_ids = flatten([
for key, value in var.children :
key == "subscription_ids" ? toset(value) : []
])

children = {
for key, value in var.children :
key => value if key != "subscription_ids"
}

next_level = var.level + 1
}

resource "azurerm_management_group" "mg" {
display_name = var.display_name
parent_management_group_id = var.parent_management_group_id
subscription_ids = local.subscription_ids
}

module "mg4" {
source = "../mg4"
for_each = local.children // var.level + 1 == 2 ? local.children : {}

level = local.next_level
display_name = each.key
parent_management_group_id = azurerm_management_group.mg.id
children = each.value
}

output "management_groups" {
value = flatten([
merge(azurerm_management_group.mg, { level = var.level }),
[
for name in keys(local.children) :
module.mg4[name].management_groups
]
])
}

output "management_group_id" {
value = merge({
id = azurerm_management_group.mg.id
},
{
for name in keys(local.children) :
name => module.mg4[name].management_group_id
})
}
61 changes: 61 additions & 0 deletions modules/mg4/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
variable "level" {
type = number
}

variable "display_name" {}

variable "parent_management_group_id" {}

variable "children" {
type = map
}

locals {
subscription_ids = flatten([
for key, value in var.children :
key == "subscription_ids" ? toset(value) : []
])

children = {
for key, value in var.children :
key => value if key != "subscription_ids"
}

next_level = var.level + 1
}

resource "azurerm_management_group" "mg" {
display_name = var.display_name
parent_management_group_id = var.parent_management_group_id
subscription_ids = local.subscription_ids
}

module "mg5" {
source = "../mg5"
for_each = local.children // var.level + 1 == 2 ? local.children : {}

level = local.next_level
display_name = each.key
parent_management_group_id = azurerm_management_group.mg.id
children = each.value
}

output "management_groups" {
value = flatten([
merge(azurerm_management_group.mg, { level = var.level }),
[
for name in keys(local.children) :
module.mg5[name].management_groups
]
])
}

output "management_group_id" {
value = merge({
id = azurerm_management_group.mg.id
},
{
for name in keys(local.children) :
name => module.mg5[name].management_group_id
})
}
61 changes: 61 additions & 0 deletions modules/mg5/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
variable "level" {
type = number
}

variable "display_name" {}

variable "parent_management_group_id" {}

variable "children" {
type = map
}

locals {
subscription_ids = flatten([
for key, value in var.children :
key == "subscription_ids" ? toset(value) : []
])

children = {
for key, value in var.children :
key => value if key != "subscription_ids"
}

next_level = var.level + 1
}

resource "azurerm_management_group" "mg" {
display_name = var.display_name
parent_management_group_id = var.parent_management_group_id
subscription_ids = local.subscription_ids
}

module "mg6" {
source = "../mg6"
for_each = local.children // var.level + 1 == 2 ? local.children : {}

level = local.next_level
display_name = each.key
parent_management_group_id = azurerm_management_group.mg.id
children = each.value
}

output "management_groups" {
value = flatten([
merge(azurerm_management_group.mg, { level = var.level }),
[
for name in keys(local.children) :
module.mg6[name].management_groups
]
])
}

output "management_group_id" {
value = merge({
id = azurerm_management_group.mg.id
},
{
for name in keys(local.children) :
name => module.mg6[name].management_group_id
})
}
Loading

0 comments on commit f833b3c

Please sign in to comment.