Skip to content

Commit

Permalink
Changed generation of policySetDefinitions and improved whatif error …
Browse files Browse the repository at this point in the history
…handling (#423)

* enhanced error handling for whatif/bicep

* generate parameter files for policySetDefinitions
  • Loading branch information
daltondhcp authored Sep 9, 2021
1 parent 3c8999b commit e6b5a77
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/internal/classes/AzOpsScope.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
}
else {
if ( (Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix') -notcontains 'parameters.json' -and
("$($this.ResourceProvider)/$($this.Resource)" -eq 'Microsoft.Authorization/policyDefinitions')
("$($this.ResourceProvider)/$($this.Resource)" -in 'Microsoft.Authorization/policyDefinitions', 'Microsoft.Authorization/policySetDefinitions')
) {
$this.StatePath = ($this.GetAzOpsResourcePath() + '.parameters' + (Get-PSFConfigValue -FullName 'AzOps.Core.TemplateParameterFileSuffix'))
}
Expand Down
45 changes: 37 additions & 8 deletions src/internal/functions/New-AzOpsDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@

#region Parse Content
$templateContent = Get-Content $TemplateFilePath | ConvertFrom-Json -AsHashtable

if ($templateContent.metadata._generator.name -eq 'bicep') {
# Detect bicep templates
$bicepTemplate = $true
}
#endregion

#region Process Scope
Expand Down Expand Up @@ -99,7 +104,15 @@
# Validate Template
$results = Get-AzSubscriptionDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
if ($resultsError.exception.InnerException.Message -match 'https://aka.ms/resource-manager-parameter-files' -and $true -eq $bicepTemplate) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateParameterError' -Target $scopeObject
$invalidTemplate = $true
}
else {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -Target $scopeObject -Tag Error -StringValues $resultsError.exception.InnerException.Message
throw $resultsError.exception.InnerException.Message
}

}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
Expand All @@ -113,7 +126,9 @@

$parameters.Name = $DeploymentName
if ($PSCmdlet.ShouldProcess("Start Subscription Deployment?")) {
New-AzSubscriptionDeployment @parameters
if (-not $invalidTemplate) {
New-AzSubscriptionDeployment @parameters
}
}
else {
# Exit deployment
Expand All @@ -134,11 +149,16 @@

$results = Get-AzResourceGroupDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
if ($resultsError.exception.InnerException.Message -match 'InvalidTemplate') {

if ($resultsError.exception.InnerException.Message -match 'https://aka.ms/resource-manager-parameter-files' -and $true -eq $bicepTemplate) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateParameterError' -Target $scopeObject
$invalidTemplate = $true
}
else {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -Target $scopeObject -Tag Error -StringValues $resultsError.exception.InnerException.Message
throw $resultsError.exception.InnerException.Message
}

}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
Expand Down Expand Up @@ -183,10 +203,14 @@

$results = Get-AzSubscriptionDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
if ($resultsError.exception.InnerException.Message -match 'InvalidTemplate') {
if ($resultsError.exception.InnerException.Message -match 'https://aka.ms/resource-manager-parameter-files' -and $true -eq $bicepTemplate) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateParameterError' -Target $scopeObject
$invalidTemplate = $true
}
else {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -Target $scopeObject -Tag Error -StringValues $resultsError.exception.InnerException.Message
throw $resultsError.exception.InnerException.Message
}
}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
Expand Down Expand Up @@ -227,10 +251,15 @@

$results = Get-AzManagementGroupDeploymentWhatIfResult @parameters -ErrorAction Continue -ErrorVariable resultsError
if ($resultsError) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -StringValues $resultsError.Exception.Message -Target $scopeObject
if ($resultsError.exception.InnerException.Message -match 'InvalidTemplate') {

if ($resultsError.exception.InnerException.Message -match 'https://aka.ms/resource-manager-parameter-files' -and $true -eq $bicepTemplate) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateParameterError' -Target $scopeObject
$invalidTemplate = $true
}
else {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.WhatIfWarning' -Target $scopeObject -Tag Error -StringValues $resultsError.exception.InnerException.Message
throw $resultsError.exception.InnerException.Message
}
}
elseif ($results.Error) {
Write-PSFMessage -Level Warning -String 'New-AzOpsDeployment.TemplateError' -StringValues $TemplateFilePath -Target $scopeObject
Expand Down

0 comments on commit e6b5a77

Please sign in to comment.