Skip to content

Commit

Permalink
Fix validation error surfacing in Deployment Stack cmdlets. (Azure#25122
Browse files Browse the repository at this point in the history
)

* Fix validation error surfacing in Deployment Stack cmdlets.

Some validation errors were not being surfaced correctly because the
exception type was not being accounted for.

* Update ChangeLog.md

---------

Co-authored-by: NoriZC <110961157+NoriZC@users.noreply.github.com>
  • Loading branch information
dantedallag and NoriZC authored Jun 13, 2024
1 parent da64873 commit 27357f9
Show file tree
Hide file tree
Showing 9 changed files with 1,291 additions and 5,026 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -985,17 +985,33 @@ private ErrorDetail ConvertValidationExceptionToError(Exception ex)
return null;
}

var innerExceptionAsError = ConvertValidationExceptionToError(ex.InnerException);

if (ex is DeploymentStacksErrorException)
{
var stackEx = ex as DeploymentStacksErrorException;
return new ErrorDetail(stackEx.Body?.Error.Code, stackEx.Body?.Error.Message, stackEx.Body?.Error.Target, innerExceptionAsError != null ? new ErrorDetail[] { innerExceptionAsError } : null);
return new ErrorDetail(stackEx.Body?.Error.Code, stackEx.Body?.Error.Message, stackEx.Body?.Error.Target, stackEx.Body?.Error.Details);
}
else if (ex is CloudException)
{
var cloudEx = ex as CloudException;
return new ErrorDetail(cloudEx.Body?.Code, cloudEx.Body?.Message, cloudEx.Body?.Target, ConvertCloudErrorListToErrorDetailList(cloudEx.Body?.Details));
}
else
{
var innerExceptionAsError = ConvertValidationExceptionToError(ex.InnerException);
return new ErrorDetail(null, ex.Message, null, innerExceptionAsError != null ? new ErrorDetail[] { innerExceptionAsError } : null);
}
}

private IList<ErrorDetail> ConvertCloudErrorListToErrorDetailList(IList<CloudError> errors)
{
List<ErrorDetail> convertedErrors = new List<ErrorDetail>();

foreach (CloudError error in errors)
{
convertedErrors.Add(new ErrorDetail(error.Code, error.Message, error.Target, error.Details != null ? ConvertCloudErrorListToErrorDetailList(error.Details) : null));
}

return convertedErrors;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function Test-NewResourceGroupDeploymentStack
$badRGname = "badRG114172"
$exceptionMessage = "Error: Code=ResourceGroupNotFound; Message=Resource group '$badRGname' could not be found"
Assert-ThrowsContains { New-AzResourceGroupDeploymentStack -Name $rname -Description "A Stack" -ResourceGroup $badRGname -TemplateFile blankTemplate.json -DenySettingsMode None -ActionOnUnmanage DetachAll -Force } $exceptionMessage

# --- ParameterFileTemplateFileParameterSetName ---

# Test - Success
Expand All @@ -114,6 +114,11 @@ function Test-NewResourceGroupDeploymentStack
$exceptionMessage = "$missingFile' because it does not exist."
Assert-ThrowsContains { New-AzResourceGroupDeploymentStack -Name $rname -Description "A Stack" -ResourceGroup $rgname -TemplateFile StacksRGTemplate.json -TemplateParameterFile $missingFile -DenySettingsMode None -Force } $exceptionMessage

# Test - Failure - Deployment Validation Error - Bad Storage Account Name

$exceptionMessage = "Storage account name must be between 3 and 24 characters in length"
Assert-ThrowsContains { New-AzResourceGroupDeploymentStack -Name $rname -Description "A Stack" -ResourceGroup $rgName -TemplateFile StacksRGBadStorageAccountName.bicep -TemplateParameterFile StacksRGBadStorageAccountName.bicepparam -DenySettingsMode None -ActionOnUnmanage DetachAll -Force } $exceptionMessage

# --- ParameterObjectTemplateFileParameterSetName ---

# Test - Success (with BypassStackOutOfSyncError)
Expand Down Expand Up @@ -694,6 +699,9 @@ function Test-NewSubscriptionDeploymentStack
$exceptionMessage = "$missingFile' because it does not exist."
Assert-ThrowsContains { New-AzSubscriptionDeploymentStack -Name $rname -Description "A Stack" -TemplateFile StacksSubTemplate.json -TemplateParameterFile $missingFile -Location $location -DenySettingsMode None -ActionOnUnmanage DetachAll -Force } $exceptionMessage

$exceptionMessage = "The provided resource group name 'test23!!!' has these invalid characters"
Assert-ThrowsContains { New-AzSubscriptionDeploymentStack -Name $rname -Description "A Stack" -Location $location -TemplateFile StacksSubBadRGName.bicep -TemplateParameterFile StacksSubBadRGName.bicepparam -DenySettingsMode None -ActionOnUnmanage DetachAll -Force } $exceptionMessage

# --- ParameterObjectTemplateFileParameterSetName (with BypassStackOutOfSyncError) ---

# Test - Success
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/Resources/Resources.Test/StacksRGBadStorageAccountName.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
targetScope = 'resourceGroup'

param location string = resourceGroup().location
param name string
var storageSku = 'Standard_LRS'

resource stg 'Microsoft.Storage/storageAccounts@2018-11-01' = {
name: name
location: location
kind: 'Storage'
sku: {
name: storageSku
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using './StacksRGBadStorageAccountName.bicep'

param name = 'TEST123'
9 changes: 9 additions & 0 deletions src/Resources/Resources.Test/StacksSubBadRGName.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
targetScope = 'subscription'

param rgname string
param location string = deployment().location

resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
name: rgname
location: location
}
3 changes: 3 additions & 0 deletions src/Resources/Resources.Test/StacksSubBadRGName.bicepparam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using './StacksSubBadRGName.bicep'

param rgname = 'test23!!!'
1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

## Upcoming Release
* Fixed deployment stack validation error surfacing.
* Fixed default formatting for output objects
* Removed '-InputObject' for
* `Get-AzPolicyAssignment`
Expand Down

0 comments on commit 27357f9

Please sign in to comment.