Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(invoice-sections): adding invoice section creation and updating #2016

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions billing_invoice_section.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module "invoice_section" {
source = "./modules/billing/invoice_sections"
for_each = var.invoice_sections

global_settings = local.global_settings
client_config = local.client_config
settings = each.value
base_tags = local.global_settings.inherit_tags
}

output "invoice_sections" {
value = module.invoice_section
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
subscriptions = {

sandbox = {
name = "my-sandbox"
create_alias = true
billing_account_name = "0000000-0000-0000-0000-0000000:000000-0000-0000-0000-00000000_2019-05-31"
billing_profile_name = "XXXX-XXXX-XXX-XXX"
#invoice_section_name = "XXXX-XXXX-XXX-XXX"
invoice_section_key = "section_1"
#invoice_section_lz_key = ""
management_group_id = "caf-sandbox-landingzones"
workload = "DevTest"
tags = {
owner = "Cloud Platform Team"
}
}
}

invoice_sections = {
section_1 = {
name = "sandbox-automation-invoice-section"
billing_account_id = "0000000-0000-0000-0000-0000000:000000-0000-0000-0000-00000000_2019-05-31"
billing_profile_id = "XXXX-XXXX-XXX-XXX"
labels = {
"foo" = "baz"
}
tags = {
"tagA" = "valueA"
}
}
}
1 change: 1 addition & 0 deletions locals.combined_objects.tf
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ locals {
combined_objects_wvd_applications = merge(tomap({ (local.client_config.landingzone_key) = module.wvd_applications }), try(var.remote_objects.wvd_applications, {}))
combined_objects_wvd_host_pools = merge(tomap({ (local.client_config.landingzone_key) = module.wvd_host_pools }), try(var.remote_objects.wvd_host_pools, {}))
combined_objects_wvd_workspaces = merge(tomap({ (local.client_config.landingzone_key) = module.wvd_workspaces }), try(var.remote_objects.wvd_workspaces, {}))
combined_objects_invoice_sections = merge(tomap({ (local.client_config.landingzone_key) = module.invoice_section }), try(var.remote_objects.invoice_sections, {}))

combined_objects_subscriptions = merge(
tomap(
Expand Down
18 changes: 18 additions & 0 deletions modules/billing/invoice_sections/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
locals {
tags = var.base_tags ? merge(
var.global_settings.tags,
try(var.settings.tags, null)
) : try(var.settings.tags, null)
}

terraform {
required_providers {
azurecaf = {
source = "aztfmod/azurecaf"
}
azapi = {
source = "azure/azapi"
version = "~> 1.6.0"
}
}
}
29 changes: 29 additions & 0 deletions modules/billing/invoice_sections/module.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
resource "azapi_resource" "invoice_section" {
type = "Microsoft.Billing/billingAccounts/billingProfiles/invoiceSections@2020-05-01"
name = var.settings.name
parent_id = format("/providers/Microsoft.Billing/billingAccounts/%s/billingProfiles/%s", var.settings.billing_account_id, var.settings.billing_profile_id)
response_export_values = ["properties.displayName"]
body = jsonencode({
properties = {
displayName = var.settings.name
labels = try(var.settings.labels, null)
tags = local.tags
}
})
}

output "id" {
value = azapi_resource.invoice_section.id
}
output "name" {
value = var.settings.name
}
output "display_name" {
value = jsondecode(azapi_resource.invoice_section.output).properties.displayName
}
output "billing_account_id" {
value = var.settings.billing_account_id
}
output "billing_profile_id" {
value = var.settings.billing_profile_id
}
13 changes: 13 additions & 0 deletions modules/billing/invoice_sections/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "global_settings" {
description = "Global settings object (see module README.md)"
}
variable "client_config" {
description = "Client configuration object (see module README.md)."
}

variable "settings" {}

variable "base_tags" {
description = "Base tags for the resource to be inherited from the resource group."
type = bool
}
14 changes: 10 additions & 4 deletions modules/subscriptions/subscriptions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data "azurerm_billing_enrollment_account_scope" "sub" {
}

data "azurerm_billing_mca_account_scope" "sub" {
count = try(var.settings.subscription_id, null) == null && var.subscription_key != "logged_in_subscription" && try(var.settings.billing_profile_name, null) != null ? 1 : 0
count = !can(var.settings.subscription_id) && var.subscription_key != "logged_in_subscription" && can(var.settings.billing_profile_name) && !can(var.settings.invoice_section_key) ? 1 : 0

billing_account_name = var.settings.billing_account_name
billing_profile_name = var.settings.billing_profile_name
Expand All @@ -19,16 +19,22 @@ resource "azurerm_subscription" "sub" {
alias = try(var.settings.alias, null) == null ? var.subscription_key : var.settings.alias
subscription_name = var.settings.name
subscription_id = try(var.settings.subscription_id, null) != null ? var.settings.subscription_id : null
billing_scope_id = try(var.settings.billing_scope_id, null) == null ? try(data.azurerm_billing_enrollment_account_scope.sub.0.id, data.azurerm_billing_mca_account_scope.sub.0.id, null) : var.settings.billing_scope_id
workload = try(var.settings.workload, null)
tags = try(var.tags, null)
billing_scope_id = can(local.billing_scope_id) ? local.billing_scope_id : try(data.azurerm_billing_enrollment_account_scope.sub.0.id, data.azurerm_billing_mca_account_scope.sub.0.id, null)

workload = try(var.settings.workload, null)
tags = try(var.tags, null)

lifecycle {
ignore_changes = [
workload
]
}
}
locals {
billing_scope_by_name = try(data.azurerm_billing_enrollment_account_scope.sub.0.id, data.azurerm_billing_mca_account_scope.sub.0.id, null)
billing_scope_by_key = try((can(var.billing_scope_id) ? var.billing_scope_id : null), (can(var.settings.billing_scope_id) ? var.settings.billing_scope_id : null), null)
billing_scope_id = try(coalesce(local.billing_scope_by_name, local.billing_scope_by_key), null)
}


resource "null_resource" "refresh_access_token" {
Expand Down
4 changes: 3 additions & 1 deletion modules/subscriptions/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ variable "client_config" {}
variable "global_settings" {
description = "Global settings object (see module README.md)"
}

variable "billing_scope_id" {
default = {}
}
# For diagnostics settings
variable "diagnostics" {
default = {}
Expand Down
4 changes: 4 additions & 0 deletions subscriptions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ module "subscriptions" {
global_settings = local.global_settings
subscription_key = each.key
settings = each.value
# billing_scope_id can also be set by tfvars (var.settings). This is only the way to inject billing_scope_id by key
billing_scope_id = can(each.value.invoice_section_key) ? local.combined_objects_invoice_sections[try(each.value.invoice_section_lz_key, local.client_config.landingzone_key)][each.value.invoice_section_key].id : null
client_config = local.client_config
diagnostics = local.combined_diagnostics
tags = merge(var.tags, lookup(each.value, "tags", {}))
}


module "subscription_billing_role_assignments" {
source = "./modules/subscription_billing_role_assignment"
for_each = var.subscription_billing_role_assignments
Expand All @@ -31,3 +34,4 @@ module "subscription_billing_role_assignments" {
output "subscriptions" {
value = module.subscriptions
}

4 changes: 4 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,7 @@ variable "load_test" {
default = {}
}


variable "invoice_sections" {
default = {}
}