Skip to content

Commit 5d1ba6a

Browse files
Support $ref statements for user-defined types in Bicep files (#23032)
* Avoid emitting duplicate warnings * Support custom types using syntax * Add changelog entry * Update ChangeLog.md --------- Co-authored-by: Yabo Hu <[email protected]>
1 parent 1a81676 commit 5d1ba6a

28 files changed

+1634
-94
lines changed

src/Resources/ResourceManager/Extensions/JsonExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static class JsonExtensions
4545
TypeNameHandling = TypeNameHandling.None,
4646
DateParseHandling = DateParseHandling.None,
4747
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
48+
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
4849
NullValueHandling = NullValueHandling.Ignore,
4950
MissingMemberHandling = MissingMemberHandling.Ignore,
5051
ContractResolver = new CamelCasePropertyNamesWithOverridesContractResolver(),
@@ -64,6 +65,7 @@ public static class JsonExtensions
6465
TypeNameHandling = TypeNameHandling.None,
6566
DateParseHandling = DateParseHandling.None,
6667
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
68+
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
6769
NullValueHandling = NullValueHandling.Ignore,
6870
ContractResolver = new CamelCasePropertyNamesWithOverridesContractResolver(),
6971
Converters = new List<JsonConverter>

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public abstract class DeploymentCmdletBase : ResourceManagerCmdletBase
7373

7474
protected string protectedTemplateUri;
7575

76-
protected IReadOnlyDictionary<string, TemplateFileParameterV1> bicepparamFileParameters;
76+
protected IReadOnlyDictionary<string, TemplateParameterFileParameter> bicepparamFileParameters;
7777

7878
private ITemplateSpecsClient templateSpecsClient;
7979

@@ -235,7 +235,7 @@ private string GetParameterJsonFilePath()
235235

236236
if (BicepUtility.IsBicepparamFile(TemplateParameterFile))
237237
{
238-
BuildAndUseBicepParameters();
238+
BuildAndUseBicepParameters(emitWarnings: false);
239239
}
240240

241241
if (BicepUtility.IsBicepFile(TemplateFile))
@@ -343,7 +343,7 @@ private string GetParameterJsonFilePath()
343343
return dynamicParameters;
344344
}
345345

346-
private static void AddToParametersHashtable(IReadOnlyDictionary<string, TemplateFileParameterV1> parameters, Hashtable parameterObject)
346+
private static void AddToParametersHashtable(IReadOnlyDictionary<string, TemplateParameterFileParameter> parameters, Hashtable parameterObject)
347347
{
348348
parameters.ForEach(dp =>
349349
{
@@ -366,7 +366,7 @@ protected Hashtable GetTemplateParameterObject()
366366
var parameterObject = new Hashtable();
367367
if (bicepparamFileParameters != null)
368368
{
369-
BuildAndUseBicepParameters();
369+
BuildAndUseBicepParameters(emitWarnings: true);
370370
AddToParametersHashtable(bicepparamFileParameters, parameterObject);
371371
return parameterObject;
372372
}
@@ -487,9 +487,10 @@ private IReadOnlyDictionary<string, object> GetDynamicParametersDictionary()
487487
x => x.Value);
488488
}
489489

490-
protected void BuildAndUseBicepParameters()
490+
protected void BuildAndUseBicepParameters(bool emitWarnings)
491491
{
492-
var output = BicepUtility.BuildParams(this.ResolvePath(TemplateParameterFile), GetDynamicParametersDictionary(), this.WriteVerbose, this.WriteWarning);
492+
BicepUtility.OutputCallback nullCallback = null;
493+
var output = BicepUtility.BuildParams(this.ResolvePath(TemplateParameterFile), GetDynamicParametersDictionary(), this.WriteVerbose, emitWarnings ? this.WriteWarning : nullCallback);
493494
bicepparamFileParameters = GetParametersFromJson(output.parametersJson);
494495

495496
if (TemplateObject == null &&
@@ -545,7 +546,7 @@ private Hashtable GetCombinedTemplateParameterObject()
545546
return TemplateParameterObject;
546547
}
547548

548-
private IReadOnlyDictionary<string, TemplateFileParameterV1> GetParametersFromJson(string parametersJson)
549+
private IReadOnlyDictionary<string, TemplateParameterFileParameter> GetParametersFromJson(string parametersJson)
549550
{
550551
using (var reader = new StringReader(parametersJson))
551552
{

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public abstract class DeploymentCreateCmdlet: DeploymentWhatIfCmdlet
3131

3232
protected abstract ConfirmImpact ConfirmImpact { get; }
3333

34-
protected abstract PSDeploymentCmdletParameters DeploymentParameters { get; }
34+
/// <summary>
35+
/// It's important not to call this function more than once during an invocation, as it can call the Bicep CLI.
36+
/// This is slow, and can also cause diagnostics to be emitted multiple times.
37+
/// </summary>
38+
protected abstract PSDeploymentCmdletParameters BuildDeploymentParameters();
3539

3640
protected abstract bool ShouldSkipConfirmationIfNoChange();
3741

@@ -76,18 +80,20 @@ protected override void OnProcessRecord()
7680

7781
protected void ExecuteDeployment()
7882
{
79-
if (!string.IsNullOrEmpty(this.DeploymentParameters.DeploymentDebugLogLevel))
83+
var parameters = BuildDeploymentParameters();
84+
85+
if (!string.IsNullOrEmpty(parameters.DeploymentDebugLogLevel))
8086
{
8187
this.WriteWarning(Resources.WarnOnDeploymentDebugSetting);
8288
}
8389

84-
if (this.DeploymentParameters.ScopeType == DeploymentScopeType.ResourceGroup)
90+
if (parameters.ScopeType == DeploymentScopeType.ResourceGroup)
8591
{
86-
this.WriteObject(this.NewResourceManagerSdkClient.ExecuteResourceGroupDeployment(this.DeploymentParameters));
92+
this.WriteObject(this.NewResourceManagerSdkClient.ExecuteResourceGroupDeployment(parameters));
8793
}
8894
else
8995
{
90-
this.WriteObject(this.NewResourceManagerSdkClient.ExecuteDeployment(this.DeploymentParameters));
96+
this.WriteObject(this.NewResourceManagerSdkClient.ExecuteDeployment(parameters));
9197
}
9298
}
9399

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Cmdlet
2020

2121
public abstract class DeploymentWhatIfCmdlet: DeploymentCmdletBase, IDynamicParameters
2222
{
23-
protected abstract PSDeploymentWhatIfCmdletParameters WhatIfParameters { get; }
23+
/// <summary>
24+
/// It's important not to call this function more than once during an invocation, as it can call the Bicep CLI.
25+
/// This is slow, and can also cause diagnostics to be emitted multiple times.
26+
/// </summary>
27+
protected abstract PSDeploymentWhatIfCmdletParameters BuildWhatIfParameters();
2428

2529
protected override void OnProcessRecord()
2630
{
@@ -42,7 +46,8 @@ protected PSWhatIfOperationResult ExecuteWhatIf()
4246
// Write status message.
4347
this.WriteInformation(information, tags);
4448

45-
PSWhatIfOperationResult whatIfResult = NewResourceManagerSdkClient.ExecuteDeploymentWhatIf(this.WhatIfParameters);
49+
var parameters = this.BuildWhatIfParameters();
50+
var whatIfResult = NewResourceManagerSdkClient.ExecuteDeploymentWhatIf(parameters);
4651

4752
// Clear status before returning result.
4853
this.WriteInformation(clearInformation, tags);

src/Resources/ResourceManager/Implementation/Deployments/GetAzureManagementGroupDeploymentWhatIfResultCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class GetAzureManagementGroupDeploymentWhatIfResultCmdlet : DeploymentWha
5353
[ValidateChangeTypes]
5454
public string[] ExcludeChangeType { get; set; }
5555

56-
protected override PSDeploymentWhatIfCmdletParameters WhatIfParameters => new PSDeploymentWhatIfCmdletParameters(
56+
protected override PSDeploymentWhatIfCmdletParameters BuildWhatIfParameters() => new PSDeploymentWhatIfCmdletParameters(
5757
scopeType: DeploymentScopeType.ManagementGroup,
5858
managementGroupId: this.ManagementGroupId,
5959
deploymentName: this.Name,

src/Resources/ResourceManager/Implementation/Deployments/GetAzureSubscriptionDeploymentWhatIfResultCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class GetAzureSubscriptionDeploymentWhatIfResultCmdlet : DeploymentWhatIf
5050
[ValidateChangeTypes]
5151
public string[] ExcludeChangeType { get; set; }
5252

53-
protected override PSDeploymentWhatIfCmdletParameters WhatIfParameters => new PSDeploymentWhatIfCmdletParameters(
53+
protected override PSDeploymentWhatIfCmdletParameters BuildWhatIfParameters() => new PSDeploymentWhatIfCmdletParameters(
5454
DeploymentScopeType.Subscription,
5555
deploymentName: this.Name,
5656
location: this.Location,

src/Resources/ResourceManager/Implementation/Deployments/GetAzureTenantDeploymentWhatIfResultCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class GetAzureTenantDeploymentWhatIfResultCmdlet : DeploymentWhatIfCmdlet
4949
[ValidateChangeTypes]
5050
public string[] ExcludeChangeType { get; set; }
5151

52-
protected override PSDeploymentWhatIfCmdletParameters WhatIfParameters => new PSDeploymentWhatIfCmdletParameters(
52+
protected override PSDeploymentWhatIfCmdletParameters BuildWhatIfParameters() => new PSDeploymentWhatIfCmdletParameters(
5353
DeploymentScopeType.Tenant,
5454
deploymentName: this.Name,
5555
location: this.Location,

src/Resources/ResourceManager/Implementation/Deployments/NewAzureManagementGroupDeploymentCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class NewAzureManagementGroupDeploymentCmdlet : DeploymentCreateCmdlet
7575
typeof(NewAzureManagementGroupDeploymentCmdlet),
7676
typeof(CmdletAttribute))).ConfirmImpact;
7777

78-
protected override PSDeploymentCmdletParameters DeploymentParameters => new PSDeploymentCmdletParameters()
78+
protected override PSDeploymentCmdletParameters BuildDeploymentParameters() => new PSDeploymentCmdletParameters()
7979
{
8080
ScopeType = DeploymentScopeType.ManagementGroup,
8181
ManagementGroupId = this.ManagementGroupId,
@@ -92,7 +92,7 @@ public class NewAzureManagementGroupDeploymentCmdlet : DeploymentCreateCmdlet
9292
Tags = TagsHelper.ConvertToTagsDictionary(this.Tag)
9393
};
9494

95-
protected override PSDeploymentWhatIfCmdletParameters WhatIfParameters => new PSDeploymentWhatIfCmdletParameters(
95+
protected override PSDeploymentWhatIfCmdletParameters BuildWhatIfParameters() => new PSDeploymentWhatIfCmdletParameters(
9696
DeploymentScopeType.ManagementGroup,
9797
managementGroupId: this.ManagementGroupId,
9898
deploymentName: this.Name,

src/Resources/ResourceManager/Implementation/Deployments/NewAzureSubscriptionDeploymentCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class NewAzureSubscriptionDeploymentCmdlet : DeploymentCreateCmdlet
7272
typeof(NewAzureSubscriptionDeploymentCmdlet),
7373
typeof(CmdletAttribute))).ConfirmImpact;
7474

75-
protected override PSDeploymentCmdletParameters DeploymentParameters => new PSDeploymentCmdletParameters()
75+
protected override PSDeploymentCmdletParameters BuildDeploymentParameters() => new PSDeploymentCmdletParameters()
7676
{
7777
ScopeType = DeploymentScopeType.Subscription,
7878
Location = this.Location,
@@ -88,7 +88,7 @@ public class NewAzureSubscriptionDeploymentCmdlet : DeploymentCreateCmdlet
8888
Tags = TagsHelper.ConvertToTagsDictionary(this.Tag)
8989
};
9090

91-
protected override PSDeploymentWhatIfCmdletParameters WhatIfParameters => new PSDeploymentWhatIfCmdletParameters(
91+
protected override PSDeploymentWhatIfCmdletParameters BuildWhatIfParameters() => new PSDeploymentWhatIfCmdletParameters(
9292
DeploymentScopeType.Subscription,
9393
deploymentName: this.Name,
9494
location: this.Location,

src/Resources/ResourceManager/Implementation/Deployments/NewAzureTenantDeploymentCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class NewAzureTenantDeploymentCmdlet: DeploymentCreateCmdlet
7171
typeof(NewAzureTenantDeploymentCmdlet),
7272
typeof(CmdletAttribute))).ConfirmImpact;
7373

74-
protected override PSDeploymentCmdletParameters DeploymentParameters => new PSDeploymentCmdletParameters()
74+
protected override PSDeploymentCmdletParameters BuildDeploymentParameters() => new PSDeploymentCmdletParameters()
7575
{
7676
ScopeType = DeploymentScopeType.Tenant,
7777
Location = this.Location,
@@ -87,7 +87,7 @@ public class NewAzureTenantDeploymentCmdlet: DeploymentCreateCmdlet
8787
Tags = TagsHelper.ConvertToTagsDictionary(this.Tag)
8888
};
8989

90-
protected override PSDeploymentWhatIfCmdletParameters WhatIfParameters => new PSDeploymentWhatIfCmdletParameters(
90+
protected override PSDeploymentWhatIfCmdletParameters BuildWhatIfParameters() => new PSDeploymentWhatIfCmdletParameters(
9191
DeploymentScopeType.Tenant,
9292
deploymentName : this.Name,
9393
location : this.Location,

0 commit comments

Comments
 (0)