Skip to content

Commit

Permalink
Management Groups (#295)
Browse files Browse the repository at this point in the history
* Update managementGroups.jq
Switching to template based

* Add managementGroups.template.jq
Initial template structure

* Update AzOpsScope.ps1
Remove parameter file generation for templates

* Delete managementGroups.jq
Replace with jq template file

* Update managementGroups.template.jq
Add support for nested deployments of Management Groups

* Update managementGroups.template.jq
Add condition for subscription resources

* Update managementGroups.template.jq
Fix schema link

* Update managementGroups.template.jq
Add control flows for empty arrays

* Update managementGroups.template.jq
Replace deployment name

* Update Strings.psd1
Add WhatIfWarning message

* Update New-AzOpsDeployment.ps1
Add control flows for WhatIf API errors
  • Loading branch information
ljtill authored Apr 14, 2021
1 parent 3563424 commit 02c2de7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 30 deletions.
3 changes: 0 additions & 3 deletions src/data/template/Microsoft.Management/managementGroups.jq

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
. | . as $input |

[
if $input.Children != null
then
foreach $input.Children[] as $item ([[],[]];
if $item.Type == "/providers/Microsoft.Management/managementGroups"
then
{
"type": "Microsoft.Management/managementGroups",
"apiVersion": "2020-05-01",
"name": $item.Name,
"scope": "/",
"properties": {
"displayName": $item.DisplayName,
"details": {
"parent": {
"id": ($input.Type + "/" + $input.Name)
}
}
}
}
elif $item.Type == "/subscriptions"
then
{
"type": "Microsoft.Management/managementGroups/subscriptions",
"apiVersion": "2021-04-01",
"name": ($input.Name + "/" + $item.Name),
"scope": "/"
}
else
empty
end
)
else
empty
end
] as $resources |

{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [
{
"type": "Microsoft.Management/managementGroups",
"name": .Name,
"apiVersion": "0000-00-00",
"scope": "/",
"properties": {
"displayName": .DisplayName,
"details": {
"parent": {
"id": .ParentId
}
}
}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2020-10-01",
"name": "AzOps-microsoft.management_managementgroups-nested",
"location": "northeurope",
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": $resources,
"outputs": {}
}
},
"dependsOn": [
(.Type + "/" + .Name)
]
}
],
"outputs": {}
}
8 changes: 1 addition & 7 deletions src/internal/classes/AzOpsScope.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,7 @@
$this.StatePath = (Join-Path $this.GetAzOpsManagementGroupPath($this.ManagementGroup) -ChildPath ("$(Get-PSFConfigValue -FullName 'AzOps.Core.AutoGeneratedTemplateFolderPath')\microsoft.management_managementgroups-$($this.ManagementGroup).json").ToLower())
}
else {
#For management group, always create parameter file
if ( (Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix') -notcontains 'parameters.json') {
$this.StatePath = (Join-Path $this.GetAzOpsManagementGroupPath($this.ManagementGroup) -ChildPath (("$(Get-PSFConfigValue -FullName 'AzOps.Core.AutoGeneratedTemplateFolderPath')\microsoft.management_managementgroups-$($this.ManagementGroup)" + '.parameters' + $(Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix'))).ToLower())
}
else {
$this.StatePath = (Join-Path $this.GetAzOpsManagementGroupPath($this.ManagementGroup) -ChildPath (("$(Get-PSFConfigValue -FullName 'AzOps.Core.AutoGeneratedTemplateFolderPath')\microsoft.management_managementgroups-$($this.ManagementGroup)" + $(Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix'))).ToLower())
}
$this.StatePath = (Join-Path $this.GetAzOpsManagementGroupPath($this.ManagementGroup) -ChildPath (("$(Get-PSFConfigValue -FullName 'AzOps.Core.AutoGeneratedTemplateFolderPath')\microsoft.management_managementgroups-$($this.ManagementGroup)" + $(Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix'))).ToLower())
}
}
elseif ($this.IsRoot()) {
Expand Down
64 changes: 44 additions & 20 deletions src/internal/functions/New-AzOpsDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@
if ($TemplateParameterFilePath) {
$parameters.TemplateParameterFile = $TemplateParameterFilePath
}
# Validate Template
$results = Get-AzResourceGroupDeploymentWhatIfResult @parameters
if ($results.Error) {
Write-PSFMessage -Level Error -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject

$results = Get-AzResourceGroupDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
return
}
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | out-string) -Target $scopeObject
else {
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | Out-String) -Target $scopeObject
}

$parameters.Name = $DeploymentName
if ($PSCmdlet.ShouldProcess("Start ResourceGroup Deployment?")) {
New-AzResourceGroupDeployment @parameters
Expand Down Expand Up @@ -156,13 +162,19 @@
if ($TemplateParameterFilePath) {
$parameters.TemplateParameterFile = $TemplateParameterFilePath
}
# Validate Template
$results = Get-AzSubscriptionDeploymentWhatIfResult @parameters
if ($results.Error) {
Write-PSFMessage -Level Error -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject

$results = Get-AzSubscriptionDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
return
}
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | out-string) -Target $scopeObject
else {
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | Out-String) -Target $scopeObject
}

$parameters.Name = $DeploymentName
if ($PSCmdlet.ShouldProcess("Start Subscription Deployment?")) {
New-AzSubscriptionDeployment @parameters
Expand All @@ -187,13 +199,19 @@
if ($TemplateParameterFilePath) {
$parameters.TemplateParameterFile = $TemplateParameterFilePath
}
# Validate Template
$results = Get-AzManagementGroupDeploymentWhatIfResult @parameters
if ($results.Error) {
Write-PSFMessage -Level Error -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject

$results = Get-AzManagementGroupDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
return
}
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | out-string) -Target $scopeObject
else {
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | Out-String) -Target $scopeObject
}

$parameters.Name = $DeploymentName
if ($PSCmdlet.ShouldProcess("Start ManagementGroup Deployment?")) {
New-AzManagementGroupDeployment @parameters
Expand All @@ -217,13 +235,19 @@
if ($TemplateParameterFilePath) {
$parameters.TemplateParameterFile = $TemplateParameterFilePath
}
# Validate Template
$results = Get-AzTenantDeploymentWhatIfResult @parameters
if ($results.Error) {
Write-PSFMessage -Level Error -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject

$results = Get-AzTenantDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
return
}
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | out-string) -Target $scopeObject
else {
Write-PSFMessage -Level Verbose -String 'New-AzOpsDeployment.WhatIfResults' -StringValues ($results | Out-String) -Target $scopeObject
}

$parameters.Name = $DeploymentName
if ($PSCmdlet.ShouldProcess("Start Tenant Deployment?")) {
# Whatif Placeholder
Expand Down
1 change: 1 addition & 0 deletions src/localized/en-us/Strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
'New-AzOpsDeployment.Scope.Unidentified' = 'Unable to determine to scope type for this Az deployment : {0}' # $scopeObject
'New-AzOpsDeployment.Subscription.Processing' = 'Attempting [Subscription] deployment in [{0}] for {1}' # $defaultDeploymentRegion, $scopeObject
'New-AzOpsDeployment.TemplateError' = 'Error validating template: {0}' # $TemplateFilePath
'New-AzOpsDeployment.WhatIfWarning' = 'Error returned from WhatIf API: {0}' # $resultsError
'New-AzOpsDeployment.WhatIfResults' = 'What If Results: {0}' # $TemplateFilePath
'New-AzOpsDeployment.SkipDueToWhatIf' = 'Skipping deployment due to What If' #

Expand Down

0 comments on commit 02c2de7

Please sign in to comment.