Skip to content

Commit

Permalink
Fixed overriding of Bicep parameters in Deployment Stack cmdlets to s…
Browse files Browse the repository at this point in the history
…upport SecureString parameters. (Azure#26670)

* Fixed overriding of Bicep parameters in Deployment Stack cmdlets to support SecureString parameters.

* Added session record.

---------

Co-authored-by: Dante DG <test>
  • Loading branch information
dantedallag authored Nov 13, 2024
1 parent 2cb03f7 commit 691d74a
Show file tree
Hide file tree
Showing 5 changed files with 1,091 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Newtonsoft.Json;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentStacks;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Json;
using Microsoft.WindowsAzure.Commands.Common;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient
{
Expand Down Expand Up @@ -905,24 +907,14 @@ private void HandleErrors(DeploymentStack deploymentStack)

private IDictionary<string, DeploymentParameter> ConvertParameterHashtableToDictionary(Hashtable parameters)
{
var paramDictionary = new Dictionary<string, DeploymentParameter>();

foreach (string key in parameters.Keys)
{
paramDictionary[key] = new DeploymentParameter();
var paramTable = (Hashtable)parameters[key];

if (paramTable["reference"] != null)
{
paramDictionary[key].Reference = JsonConvert.DeserializeObject<KeyVaultParameterReference>(paramTable["reference"].ToString());
}
else
{
paramDictionary[key].Value = paramTable["value"];
}
}

return paramDictionary;
Dictionary<string, object> parametersDictionary = parameters?.ToDictionary(false);
string parametersContent = parametersDictionary != null
? PSJsonSerializer.Serialize(parametersDictionary)
: null;

return !string.IsNullOrEmpty(parametersContent)
? parametersContent.FromJson<Dictionary<string, DeploymentParameter>>()
: null;
}

private DeploymentStack waitStackCompletion(Func<Task<AzureOperationResponse<DeploymentStack>>> getStack, params string[] status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ public void TestGetResourceGroupDeploymentStack()
{
TestRunner.RunTestScript("Test-GetResourceGroupDeploymentStack");
}


[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestNewResourceGroupDeploymentStackFromBicepparamFileWithOverrides()
{
TestRunner.RunTestScript("Test-NewResourceGroupDeploymentStackFromBicepparamFileWithOverrides");
}

[Fact()]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestNewResourceGroupDeploymentStack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,57 @@ function Test-GetResourceGroupDeploymentStack
}
}

<#
.SYNOPSIS
Tests deployment via .bicepparam file with inline parameter overrides.
#>
function Test-NewResourceGroupDeploymentStackFromBicepparamFileWithOverrides
{
# Setup
$rgname = Get-ResourceGroupName
$rname = Get-ResourceName
$rglocation = "West US 2"
$expectedAllOutput = @'
{
"array": [
"abc"
],
"string": "hello",
"object": {
"def": "ghi"
},
"int": 42,
"bool": true,
"secureString": "glabble"
}
'@ | ConvertFrom-Json

try
{
# Test
New-AzResourceGroup -Name $rgname -Location $rglocation

$deployment = New-AzResourceGroupDeploymentStack -Name $rname -ResourceGroupName $rgname -ActionOnUnmanage DetachAll -DenySettingsMode None -TemplateParameterFile deployWithParamOverrides.bicepparam `
-myArray @("abc") `
-myObject @{"def" = "ghi";} `
-myString "hello" `
-myInt 42 `
-myBool $true `
-mySecureString (ConvertTo-SecureString -String "glabble" -AsPlainText -Force)

# Assert
Assert-AreEqual Succeeded $deployment.ProvisioningState

$actualAllOutput = $deployment.Outputs["all"].Value.ToString() | ConvertFrom-Json
Assert-AreEqual ($expectedAllOutput | ConvertTo-Json) ($actualAllOutput | ConvertTo-Json)
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

<#
.SYNOPSIS
Tests New operation on deployment stacks at the RG scope.
Expand Down
Loading

0 comments on commit 691d74a

Please sign in to comment.