Skip to content

Commit

Permalink
add subscription_update_existing
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-taylor committed Nov 27, 2023
1 parent 3bcec39 commit 1ab928e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,16 @@ Type: `map(string)`

Default: `{}`

### <a name="input_subscription_update_existing"></a> [subscription\_update\_existing](#input\_subscription\_update\_existing)

Description: Whether to update an existing subscription with the supplied tags and display name.
If enabled, the following must also be supplied:
- `subscription_id`

Type: `bool`

Default: `false`

### <a name="input_subscription_use_azapi"></a> [subscription\_use\_azapi](#input\_subscription\_use\_azapi)

Description: Whether to create a new subscription using the azapi provider. This may be required if the principal running
Expand Down
3 changes: 2 additions & 1 deletion main.subscription.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# to be able to deploy resources to an existing subscription.
module "subscription" {
source = "./modules/subscription"
count = var.subscription_alias_enabled || var.subscription_management_group_association_enabled ? 1 : 0
count = (var.subscription_id != "" && var.subscription_update_existing) || var.subscription_alias_enabled || var.subscription_management_group_association_enabled ? 1 : 0

subscription_alias_enabled = var.subscription_alias_enabled
subscription_alias_name = var.subscription_alias_name
Expand All @@ -14,5 +14,6 @@ module "subscription" {
subscription_management_group_id = var.subscription_management_group_id
subscription_tags = var.subscription_tags
subscription_use_azapi = var.subscription_use_azapi
subscription_update_existing = var.subscription_update_existing
subscription_workload = var.subscription_workload
}
10 changes: 10 additions & 0 deletions modules/subscription/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ Type: `map(string)`

Default: `{}`

### <a name="input_subscription_update_existing"></a> [subscription\_update\_existing](#input\_subscription\_update\_existing)

Description: Whether to update an existing subscription with the supplied tags and display name.
If enabled, the following must also be supplied:
- `subscription_id`

Type: `bool`

Default: `false`

### <a name="input_subscription_use_azapi"></a> [subscription\_use\_azapi](#input\_subscription\_use\_azapi)

Description: Whether to use the azapi\_resource resource to create the subscription alias. This includes the subscription alias in the management group.
Expand Down
10 changes: 5 additions & 5 deletions modules/subscription/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resource "azurerm_subscription" "this" {
# This resource ensures that we can manage the management group for the subscription
# throughout its lifecycle.
resource "azurerm_management_group_subscription_association" "this" {
count = var.subscription_management_group_association_enabled && !var.subscription_use_azapi ? 1 : 0
count = var.subscription_management_group_association_enabled ? 1 : 0
management_group_id = "/providers/Microsoft.Management/managementGroups/${var.subscription_management_group_id}"
subscription_id = "/subscriptions/${local.subscription_id}"
}
Expand Down Expand Up @@ -43,10 +43,10 @@ resource "azapi_resource" "subscription" {
}

resource "azapi_update_resource" "subscription_tags" {
count = var.subscription_alias_enabled && var.subscription_use_azapi ? 1 : 0
count = (var.subscription_alias_enabled && var.subscription_use_azapi) || (var.subscription_id != "" && var.subscription_update_existing) ? 1 : 0

type = "Microsoft.Resources/tags@2022-09-01"
resource_id = "/subscriptions/${jsondecode(azapi_resource.subscription[0].output).properties.subscriptionId}/providers/Microsoft.Resources/tags/default"
resource_id = "/subscriptions/${local.subscription_id}/providers/Microsoft.Resources/tags/default"
body = jsonencode({
properties = {
tags = var.subscription_tags
Expand All @@ -55,10 +55,10 @@ resource "azapi_update_resource" "subscription_tags" {
}

resource "azapi_resource_action" "subscription_rename" {
count = var.subscription_alias_enabled && var.subscription_use_azapi ? 1 : 0
count = (var.subscription_alias_enabled && var.subscription_use_azapi) || (var.subscription_id != "" && var.subscription_update_existing) ? 1 : 0

type = "Microsoft.Resources/subscriptions@2021-10-01"
resource_id = "/subscriptions/${jsondecode(azapi_resource.subscription[0].output).properties.subscriptionId}"
resource_id = "/subscriptions/${local.subscription_id}"
method = "POST"
action = "providers/Microsoft.Subscription/rename"
body = jsonencode({
Expand Down
10 changes: 10 additions & 0 deletions modules/subscription/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,13 @@ variable "subscription_use_azapi" {
Whether to use the azapi_resource resource to create the subscription alias. This includes the subscription alias in the management group.
DESCRIPTION
}

variable "subscription_update_existing" {
type = bool
default = false
description = <<DESCRIPTION
Whether to update an existing subscription with the supplied tags and display name.
If enabled, the following must also be supplied:
- `subscription_id`
DESCRIPTION
}
10 changes: 10 additions & 0 deletions variables.subscription.tf
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,13 @@ If disabled, supply the `subscription_id` variable to use an existing subscripti
> If the command fails for any reason, the provider will attempt to cancel the subscription anyway.
DESCRIPTION
}

variable "subscription_update_existing" {
type = bool
default = false
description = <<DESCRIPTION
Whether to update an existing subscription with the supplied tags and display name.
If enabled, the following must also be supplied:
- `subscription_id`
DESCRIPTION
}

0 comments on commit 1ab928e

Please sign in to comment.