Skip to content

Commit

Permalink
Merge pull request #2 from fjdev/feature/add-management-group-associa…
Browse files Browse the repository at this point in the history
…tion

Add management group subscription association resource
  • Loading branch information
fjdev authored Jul 10, 2024
2 parents 9c88f51 + b1d6549 commit 3968a40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ No modules.

| Name | Type |
|------|------|
| [azurerm_subscription.subscription](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subscription) | resource |
| [azurerm_management_group_subscription_association.mgsubassoc](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/management_group_subscription_association) | resource |
| [azurerm_subscription.sub](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subscription) | resource |
| [azurerm_billing_enrollment_account_scope.ea](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/billing_enrollment_account_scope) | data source |
| [azurerm_billing_mca_account_scope.mca](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/billing_mca_account_scope) | data source |
| [azurerm_billing_mpa_account_scope.mpa](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/billing_mpa_account_scope) | data source |
Expand All @@ -29,6 +30,7 @@ No modules.
|------|-------------|------|---------|:--------:|
| <a name="input_alias"></a> [alias](#input\_alias) | (Optional) The Alias name for the subscription. Terraform will generate a new GUID if this is not supplied. Changing this forces a new Subscription to be created. | `string` | `null` | no |
| <a name="input_billing_scope_id"></a> [billing\_scope\_id](#input\_billing\_scope\_id) | (Optional) The Azure Billing Scope ID. Can be a Microsoft Customer Account Billing Scope ID, a Microsoft Partner Account Billing Scope ID or an Enrollment Billing Scope ID. | `string` | `null` | no |
| <a name="input_management_group_id"></a> [management\_group\_id](#input\_management\_group\_id) | (Required) The ID of the Management Group to associate the Subscription with. Changing this forces a new Management to be created. | `string` | `null` | no |
| <a name="input_subscription_id"></a> [subscription\_id](#input\_subscription\_id) | (Optional) The ID of the Subscription. Changing this forces a new Subscription to be created. | `string` | `null` | no |
| <a name="input_subscription_name"></a> [subscription\_name](#input\_subscription\_name) | (Required) The Name of the Subscription. This is the Display Name in the portal. | `string` | `null` | no |
| <a name="input_subscription_type"></a> [subscription\_type](#input\_subscription\_type) | (Optional) The Subscription Type to use and the required parameters for that Subscription Type. Possible values are 'EA', 'MCA' and 'MPA'. | <pre>map(object({<br> billing_account_name = optional(string)<br> enrollment_account_name = optional(string)<br> billing_profile_name = optional(string)<br> invoice_section_name = optional(string)<br> customer_name = optional(string)<br> }))</pre> | `null` | no |
Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "azurerm_subscription" "subscription" {
resource "azurerm_subscription" "sub" {
subscription_name = var.subscription_name
alias = var.alias
billing_scope_id = var.subscription_id == null ? lookup(local.billing_scope_ids, keys(var.subscription_type)[0], var.billing_scope_id) : null
Expand Down
7 changes: 7 additions & 0 deletions management_group_subscription_association.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "azurerm_management_group_subscription_association" "mgsubassoc" {
count = var.management_group_id != null ? 1 : 0

management_group_id = var.management_group_id
subscription_id = data.azurerm_subscriptions.created.subscriptions[0].subscription_id
}

6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ variable "subscription_type" {
description = "(Optional) The Subscription Type to use and the required parameters for that Subscription Type. Possible values are 'EA', 'MCA' and 'MPA'."
default = null
}

variable "management_group_id" {
type = string
description = "(Required) The ID of the Management Group to associate the Subscription with. Changing this forces a new Management to be created."
default = null
}

0 comments on commit 3968a40

Please sign in to comment.