Skip to content

Commit 5154c8e

Browse files
committed
Updated parameter file parsing and added new test
1 parent 144d3a6 commit 5154c8e

File tree

7 files changed

+1872
-6
lines changed

7 files changed

+1872
-6
lines changed

src/Resources/ResourceManager/SdkClient/ResourceManagerSdkClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,9 @@ private Deployment CreateBasicDeployment(PSDeploymentCmdletParameters parameters
489489
string parametersContent = parametersDictionary != null
490490
? PSJsonSerializer.Serialize(parametersDictionary)
491491
: null;
492+
// NOTE(jcotillo): Adding FromJson<> to parameters as well
492493
deployment.Properties.Parameters = !string.IsNullOrEmpty(parametersContent)
493-
? JObject.Parse(parametersContent)
494+
? parametersContent.FromJson<JObject>()
494495
: null;
495496
}
496497

src/Resources/ResourceManager/Utilities/TemplateUtility.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System.Security;
2727
using Microsoft.WindowsAzure.Commands.Common;
2828
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
29+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2930

3031
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities
3132
{
@@ -88,12 +89,18 @@ public static Dictionary<string, TemplateFileParameterV1> ParseTemplateParameter
8889
{
8990
try
9091
{
91-
parameters = JsonConvert.DeserializeObject<Dictionary<string, TemplateFileParameterV1>>(FileUtilities.DataStore.ReadFileAsText(templateParameterFilePath));
92+
// NOTE(jcotillo): We must use JsonExtensions to ensure the proper use of serialization settings.
93+
// otherwise we could get invalid date time serializations.
94+
parameters =
95+
FileUtilities.DataStore.ReadFileAsStream(templateParameterFilePath)
96+
.FromJson<Dictionary<string, TemplateFileParameterV1>>();
9297
}
9398
catch (JsonSerializationException)
9499
{
95-
parameters = new Dictionary<string, TemplateFileParameterV1>(
96-
JsonConvert.DeserializeObject<TemplateFileParameterV2>(FileUtilities.DataStore.ReadFileAsText(templateParameterFilePath)).Parameters);
100+
var parametersv2 =
101+
FileUtilities.DataStore.ReadFileAsStream(templateParameterFilePath)
102+
.FromJson<TemplateFileParameterV2>();
103+
parameters = new Dictionary<string, TemplateFileParameterV1>(parametersv2.Parameters);
97104
}
98105
}
99106

src/Resources/Resources.Test/ScenarioTests/DeploymentTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,12 @@ public void TestNewDeploymentFromTemplateFileContainingDatetimeOutput()
209209
{
210210
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateFileContainingDatetimeOutput");
211211
}
212+
213+
[Fact]
214+
[Trait(Category.AcceptanceType, Category.CheckIn)]
215+
public void TestNewDeploymentFromTemplateAndParameterFileContainingDatetimeOutput()
216+
{
217+
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateAndParameterFileContainingDatetimeOutput");
218+
}
212219
}
213220
}

src/Resources/Resources.Test/ScenarioTests/DeploymentTests.ps1

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ function Test-NewDeploymentFromTemplateFileContainingDatetimeOutput
942942
New-AzResourceGroup -Name $rgname -Location $rglocation
943943

944944
$datetime = "2021-07-08T22:56:00"
945+
$datetimeFormatted = $datetime | Get-Date
945946
$parameters = @{ "date"= $datetime }
946947

947948
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile simpleTemplateWithDatetimeOutput.json -TemplateParameterObject $parameters
@@ -954,8 +955,53 @@ function Test-NewDeploymentFromTemplateFileContainingDatetimeOutput
954955
$getById = Get-AzResourceGroupDeployment -Id $deploymentId
955956
Assert-AreEqual $getById.DeploymentName $deployment.DeploymentName
956957

957-
$datetimeOutput = $getById.Outputs.date
958-
Assert-AreEqual $datetime $datetimeOutput
958+
$datetimeOutput = $getById.Outputs.date.Value | Get-Date
959+
960+
Assert-AreEqual $datetimeFormatted $datetimeOutput
961+
}
962+
963+
finally
964+
{
965+
# Cleanup
966+
Clean-ResourceGroup $rgname
967+
}
968+
}
969+
970+
<#
971+
.SYNOPSIS
972+
Tests deployment via template and parameter file containing a single datetime string output.
973+
#>
974+
function Test-NewDeploymentFromTemplateAndParameterFileContainingDatetimeOutput
975+
{
976+
# Setup
977+
$rgname = Get-ResourceGroupName
978+
$rname = Get-ResourceName
979+
$rglocation = "West US 2"
980+
981+
try
982+
{
983+
# Test
984+
New-AzResourceGroup -Name $rgname -Location $rglocation
985+
986+
# NOTE(jcotillo): This is the same value as the one from: simpleTemplateWithDatetimeOutputParameters.json
987+
# if the parameter file gets updated, please ensure to update this value as well otherwise test will fail.
988+
$datetime = "2021-07-08T22:56:00"
989+
$datetimeFormatted = $datetime | Get-Date
990+
$parameters = @{ "date"= $datetime }
991+
992+
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile simpleTemplateWithDatetimeOutput.json -TemplateParameterFile simpleTemplateWithDatetimeOutputParameters.json
993+
994+
# Assert
995+
Assert-AreEqual Succeeded $deployment.ProvisioningState
996+
997+
$subId = (Get-AzContext).Subscription.SubscriptionId
998+
$deploymentId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deployments/$rname"
999+
$getById = Get-AzResourceGroupDeployment -Id $deploymentId
1000+
Assert-AreEqual $getById.DeploymentName $deployment.DeploymentName
1001+
1002+
$datetimeOutput = $getById.Outputs.date.Value | Get-Date
1003+
1004+
Assert-AreEqual $datetimeFormatted $datetimeOutput
9591005
}
9601006

9611007
finally

0 commit comments

Comments
 (0)