-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
219 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# This module uses azapi as a workaround since azurerm_role_assignment does not support billing role assignment for MCA. | ||
# See https://github.com/hashicorp/terraform-provider-azurerm/issues/15211. | ||
# Once this is resolved, refactor this module. | ||
|
||
terraform { | ||
required_version = "> 1.1" | ||
required_providers { | ||
azurerm = { | ||
source = "hashicorp/azurerm" | ||
version = "3.81.0" | ||
} | ||
azuread = { | ||
source = "hashicorp/azuread" | ||
version = "2.46.0" | ||
} | ||
azapi = { | ||
source = "Azure/azapi" | ||
version = "1.13.1" | ||
} | ||
} | ||
} | ||
|
||
|
||
data "azurerm_billing_mca_account_scope" "mca" { | ||
billing_account_name = var.billing_account_name | ||
billing_profile_name = var.billing_profile_name | ||
invoice_section_name = var.invoice_section_name | ||
} | ||
|
||
resource "azuread_application" "mca" { | ||
display_name = var.service_principal_name | ||
} | ||
|
||
resource "azuread_service_principal" "mca" { | ||
client_id = azuread_application.mca.client_id | ||
} | ||
|
||
data "azapi_resource_list" "billing_role_definitions" { | ||
type = "Microsoft.Billing/billingAccounts/billingProfiles/invoiceSections/billingRoleDefinitions@2020-05-01" | ||
parent_id = data.azurerm_billing_mca_account_scope.mca.id | ||
response_export_values = ["*"] | ||
} | ||
|
||
locals { | ||
azure_subscription_creator_role_id = jsondecode( | ||
data.azapi_resource_list.billing_role_definitions.output).value[ | ||
index(jsondecode(data.azapi_resource_list.billing_role_definitions.output).value[*].properties.roleName, "Azure subscription creator") | ||
].id | ||
} | ||
|
||
resource "azapi_resource_action" "add_role_assignment_subscription_creator" { | ||
type = "Microsoft.Billing/billingAccounts/billingProfiles/invoiceSections@2019-10-01-preview" | ||
resource_id = data.azurerm_billing_mca_account_scope.mca.id | ||
action = "createBillingRoleAssignment" | ||
method = "POST" | ||
when = "apply" | ||
response_export_values = ["*"] | ||
body = jsonencode({ | ||
properties = { | ||
principalId = azuread_service_principal.mca.object_id | ||
roleDefinitionId = local.azure_subscription_creator_role_id | ||
} | ||
}) | ||
} | ||
|
||
resource "azapi_resource_action" "remove_role_assignment_subscription_creator" { | ||
type = "Microsoft.Billing/billingAccounts/billingProfiles/invoiceSections/billingRoleAssignments@2019-10-01-preview" | ||
resource_id = jsondecode(azapi_resource_action.add_role_assignment_subscription_creator.output).id | ||
method = "DELETE" | ||
when = "destroy" | ||
} | ||
|
||
//--------------------------------------------------------------------------- | ||
// Create new client secret and associate it with the application | ||
//--------------------------------------------------------------------------- | ||
resource "time_rotating" "mca_secret_rotation" { | ||
rotation_days = 365 | ||
} | ||
|
||
resource "azuread_application_password" "mca" { | ||
application_id = azuread_application.mca.id | ||
rotate_when_changed = { | ||
rotation = time_rotating.mca_secret_rotation.id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
output "billing_scope" { | ||
value = data.azurerm_billing_mca_account_scope.mca.id | ||
} | ||
|
||
output "credentials" { | ||
description = "Service Principal application id and object id" | ||
value = { | ||
Enterprise_Application_Object_ID = azuread_service_principal.mca.id | ||
Application_Client_ID = azuread_application.mca.client_id | ||
Client_Secret = "Execute `terraform output mca_service_principal_password` to see the password" | ||
} | ||
} | ||
|
||
output "application_client_secret" { | ||
description = "Client Secret Of the Application." | ||
value = azuread_application_password.mca.value | ||
sensitive = true | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
variable "service_principal_name" { | ||
type = string | ||
} | ||
|
||
variable "billing_account_name" { | ||
type = string | ||
} | ||
|
||
variable "billing_profile_name" { | ||
type = string | ||
} | ||
|
||
variable "invoice_section_name" { | ||
type = string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters