Skip to content

Commit fd72e96

Browse files
Support deploying Azure resources with parameters file described with the Bicep language (#21567)
* add integration test for deployment using bicepparam * add changes for bicepparam file deployment * updated changelog --------- Co-authored-by: Yunchi Wang <[email protected]>
1 parent a57565d commit fd72e96

File tree

13 files changed

+9421
-2
lines changed

13 files changed

+9421
-2
lines changed

src/Resources/ResourceManager/Implementation/CmdletBase/ResourceWithParameterCmdletBase.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using Microsoft.Azure.Management.ResourceManager;
2020
using Microsoft.Azure.Management.ResourceManager.Models;
2121
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22-
22+
using Newtonsoft.Json.Bson;
2323
using Newtonsoft.Json.Linq;
2424

2525
using System;
@@ -191,9 +191,19 @@ protected override void OnBeginProcessing()
191191
throw new NotSupportedException($"'-TemplateUri {TemplateUri}' is not supported. Please download the bicep file and pass it using -TemplateFile.");
192192
}
193193

194+
if (BicepUtility.IsBicepparamFile(TemplateParameterFile) && !BicepUtility.IsBicepFile(TemplateFile))
195+
{
196+
throw new NotSupportedException($"Bicepparam file {TemplateParameterFile} is only supported with a Bicep template file");
197+
}
198+
194199
if (BicepUtility.IsBicepFile(TemplateFile))
200+
{
195201
BuildAndUseBicepTemplate();
196202

203+
if (BicepUtility.IsBicepparamFile(TemplateParameterFile))
204+
BuildAndUseBicepParameters();
205+
}
206+
197207
if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt))
198208
{
199209
// Resolve the static parameter names for this cmdlet:
@@ -460,5 +470,10 @@ protected void BuildAndUseBicepTemplate()
460470
{
461471
TemplateFile = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose, this.WriteWarning);
462472
}
473+
474+
protected void BuildAndUseBicepParameters()
475+
{
476+
TemplateParameterFile = BicepUtility.BuildParamFile(this.ResolvePath(TemplateParameterFile), this.WriteVerbose, this.WriteWarning);
477+
}
463478
}
464479
}

src/Resources/ResourceManager/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/ResourceManager/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,10 @@ You can help us improve the accuracy of the result by opening an issue here: htt
512512
<data name="BuildBicepFileToJsonFailed" xml:space="preserve">
513513
<value>Build bicep file '{0}' to json failed.</value>
514514
</data>
515+
<data name="InvalidBicepparamFilePath" xml:space="preserve">
516+
<value>Invalid Bicepparam file path.</value>
517+
</data>
518+
<data name="BuildBicepparamFileToJsonFailed" xml:space="preserve">
519+
<value>Build bicepparam file '{0}' to json failed.</value>
520+
</data>
515521
</root>

src/Resources/ResourceManager/Utilities/BicepUtility.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ internal static class BicepUtility
3232

3333
private const string MinimalVersionRequirementForBicepPublishWithOptionalDocumentationUriParameter = "0.14.46";
3434

35+
private const string MinimalVersionRequirementForBicepparamFileBuild = "0.16.1";
36+
3537
public delegate void OutputCallback(string msg);
3638

3739
public static bool IsBicepFile(string templateFilePath) =>
3840
".bicep".Equals(Path.GetExtension(templateFilePath), StringComparison.OrdinalIgnoreCase);
3941

42+
public static bool IsBicepparamFile(string parametersFilePath) =>
43+
".bicepparam".Equals(Path.GetExtension(parametersFilePath), StringComparison.OrdinalIgnoreCase);
44+
4045
public static string BuildFile(string bicepTemplateFilePath, OutputCallback writeVerbose = null, OutputCallback writeWarning = null)
4146
{
4247
if (!FileUtilities.DataStore.FileExists(bicepTemplateFilePath))
@@ -58,6 +63,27 @@ public static string BuildFile(string bicepTemplateFilePath, OutputCallback writ
5863
return buildResultPath;
5964
}
6065

66+
public static string BuildParamFile(string bicepParamFilePath, OutputCallback writeVerbose = null, OutputCallback writeWarning = null)
67+
{
68+
if (!FileUtilities.DataStore.FileExists(bicepParamFilePath))
69+
{
70+
throw new AzPSArgumentException(Properties.Resources.InvalidBicepparamFilePath, "TemplateParameterFile");
71+
}
72+
73+
string tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
74+
Directory.CreateDirectory(tempDirectory);
75+
76+
string buildResultPath = Path.Combine(tempDirectory, Path.GetFileName(bicepParamFilePath)).Replace(".bicepparam", ".json");
77+
RunBicepCommand($"bicep build-params '{bicepParamFilePath}' --outfile '{buildResultPath}'", MinimalVersionRequirementForBicepparamFileBuild, writeVerbose, writeWarning);
78+
79+
if (!FileUtilities.DataStore.FileExists(buildResultPath))
80+
{
81+
throw new AzPSApplicationException(string.Format(Properties.Resources.BuildBicepparamFileToJsonFailed, bicepParamFilePath));
82+
}
83+
84+
return buildResultPath;
85+
}
86+
6187
public static void PublishFile(string bicepFilePath, string target, string documentationUri = null, OutputCallback writeVerbose = null, OutputCallback writeWarning = null)
6288
{
6389
if (!FileUtilities.DataStore.FileExists(bicepFilePath))

src/Resources/Resources.Test/Resources.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<None Update="Resources\*.json" CopyToOutputDirectory="PreserveNewest" />
3030
<None Update="*.json" CopyToOutputDirectory="PreserveNewest" />
3131
<None Update="*.bicep" CopyToOutputDirectory="PreserveNewest" />
32+
<None Update="*.bicepparam" CopyToOutputDirectory="PreserveNewest" />
3233
<None Update="ScenarioTests\*.pfx" CopyToOutputDirectory="PreserveNewest" />
3334
</ItemGroup>
3435

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ public void TestTestDeploymentFromBicepFile()
195195
TestRunner.RunTestScript("Test-TestDeploymentFromBicepFile");
196196
}
197197

198+
[Fact]
199+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
200+
public void TestNewDeploymentFromBicepFileAndBicepparamFile()
201+
{
202+
TestRunner.RunTestScript("Test-NewDeploymentFromBicepFileAndBicepparamFile");
203+
}
204+
198205
//Please make sure to re-record this test if any changes are made to WhatIf, QueryString or ResourceGroupDeployments
199206
[Fact]
200207
[Trait(Category.AcceptanceType, Category.CheckIn)]

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ function Test-TestDeploymentFromBicepFile
843843
New-AzResourceGroup -Name $rgname -Location $location
844844

845845
$list = Test-AzResourceGroupDeployment -ResourceGroupName $rgname -TemplateFile sampleDeploymentBicepFile.bicep
846-
846+
847847
# Assert
848848
Assert-AreEqual 0 @($list).Count
849849
}
@@ -854,6 +854,39 @@ function Test-TestDeploymentFromBicepFile
854854
}
855855
}
856856

857+
<#
858+
.SYNOPSIS
859+
Tests deployment via Bicep file.
860+
#>
861+
function Test-NewDeploymentFromBicepFileAndBicepparamFile
862+
{
863+
# Setup
864+
$rgname = Get-ResourceGroupName
865+
$rname = Get-ResourceName
866+
$rglocation = "West US 2"
867+
868+
try
869+
{
870+
# Test
871+
New-AzResourceGroup -Name $rgname -Location $rglocation
872+
873+
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile sampleDeploymentBicepFileWithoutParamValues.bicep -TemplateParameterFile sampleDeploymentBicepFileParams.bicepparam
874+
875+
# Assert
876+
Assert-AreEqual Succeeded $deployment.ProvisioningState
877+
878+
$subId = (Get-AzContext).Subscription.SubscriptionId
879+
$deploymentId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deployments/$rname"
880+
$getById = Get-AzResourceGroupDeployment -Id $deploymentId
881+
Assert-AreEqual $getById.DeploymentName $deployment.DeploymentName
882+
}
883+
finally
884+
{
885+
# Cleanup
886+
Clean-ResourceGroup $rgname
887+
}
888+
}
889+
857890
<#
858891
.SYNOPSIS
859892
is running live in target environment

0 commit comments

Comments
 (0)