-
Notifications
You must be signed in to change notification settings - Fork 84
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(subscription): optional use of AzAPI for Subscription creation #234
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
58ed06d
Optionally use AzAPI for Subscription
danwatco 22d23a6
Spelling + parent
danwatco 57edea6
Formatting
danwatco 219f47c
Whitespace
danwatco d71b120
Change to 'this'
danwatco 7553070
Rename
danwatco 3794b95
Wrote tests
danwatco 326ac7f
Added comments
danwatco d4a5d2c
Added deployment tests
danwatco 7150d88
Update docs
danwatco e6ee19d
Update description
danwatco 80b5058
Update docs
danwatco 6c31e65
Remove trailing spaces
danwatco 707b7db
Docs fix
danwatco 584f19d
Merge branch 'main' into main
matt-FFFFFF 09bb4f8
Added deployment test
danwatco 3cb3e31
Merge branch 'main' of https://github.com/danwatco/terraform-azurerm-…
danwatco 83aaf12
Merge branch 'main' into main
danwatco 62a3377
Merge branch 'main' into main
matt-FFFFFF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,17 +1,42 @@ | ||
# The azurerm_subscription resource represents the subscription alias that is being created. | ||
resource "azurerm_subscription" "this" { | ||
count = var.subscription_alias_enabled ? 1 : 0 | ||
count = var.subscription_alias_enabled && !(var.subscription_use_azapi) ? 1 : 0 | ||
subscription_name = var.subscription_display_name | ||
alias = var.subscription_alias_name | ||
billing_scope_id = var.subscription_billing_scope | ||
workload = var.subscription_workload | ||
tags = var.subscription_tags | ||
} | ||
|
||
# Optionally make use of AzAPI to create the subscription to allow creation without access | ||
# to the default management group. | ||
resource "azapi_resource" "subscription" { | ||
count = var.subscription_alias_enabled && var.subscription_use_azapi ? 1 : 0 | ||
|
||
type = "Microsoft.Subscription/aliases@2021-10-01" | ||
name = var.subscription_alias_name | ||
parent_id = "/" | ||
|
||
body = jsonencode({ | ||
properties = { | ||
displayName = var.subscription_display_name | ||
workload = var.subscription_workload | ||
billingScope = var.subscription_billing_scope | ||
subscriptionId = null | ||
additionalProperties = { | ||
managementGroupId = var.subscription_management_group_association_enabled ? var.subscription_management_group_id : null | ||
tags = var.subscription_tags | ||
} | ||
} | ||
}) | ||
} | ||
|
||
# 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 ? 1 : 0 | ||
management_group_id = "/providers/Microsoft.Management/managementGroups/${var.subscription_management_group_id}" | ||
subscription_id = "/subscriptions/${local.subscription_id}" | ||
} | ||
|
||
|
53 changes: 53 additions & 0 deletions
53
modules/subscription/testdata/TestDeploySubscriptionAliasAZAPIManagementGroupValid/main.tf
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,53 @@ | ||
variable "subscription_billing_scope" { | ||
type = string | ||
} | ||
|
||
variable "subscription_management_group_id" { | ||
type = string | ||
} | ||
|
||
variable "subscription_alias_name" { | ||
type = string | ||
} | ||
|
||
variable "subscription_display_name" { | ||
type = string | ||
} | ||
|
||
variable "subscription_workload" { | ||
type = string | ||
} | ||
|
||
variable "subscription_management_group_association_enabled" { | ||
type = bool | ||
} | ||
|
||
variable "subscription_alias_enabled" { | ||
type = bool | ||
} | ||
|
||
variable "subscription_use_azapi" { | ||
type = bool | ||
} | ||
|
||
resource "azapi_resource" "mg" { | ||
type = "Microsoft.Management/managementGroups@2021-04-01" | ||
parent_id = "/" | ||
name = var.subscription_management_group_id | ||
} | ||
|
||
module "subscription_test" { | ||
source = "../../" | ||
subscription_alias_name = var.subscription_alias_name | ||
subscription_display_name = var.subscription_display_name | ||
subscription_workload = var.subscription_workload | ||
subscription_management_group_id = azapi_resource.mg.name | ||
subscription_billing_scope = var.subscription_billing_scope | ||
subscription_management_group_association_enabled = var.subscription_management_group_association_enabled | ||
subscription_alias_enabled = var.subscription_alias_enabled | ||
subscription_use_azapi = var.subscription_use_azapi | ||
} | ||
|
||
output "subscription_id" { | ||
value = module.subscription_test.subscription_id | ||
} |
13 changes: 13 additions & 0 deletions
13
...s/subscription/testdata/TestDeploySubscriptionAliasAZAPIManagementGroupValid/terraform.tf
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,13 @@ | ||
terraform { | ||
required_version = ">= 1.3.0" | ||
required_providers { | ||
azurerm = { | ||
source = "hashicorp/azurerm" | ||
version = ">= 3.7.0" | ||
} | ||
azapi = { | ||
source = "Azure/azapi" | ||
version = ">= 1.0.0" | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luke-taylor You don't need this block if using AzAPI, so you can count = 0 it.