Skip to content

Commit 4ec51de

Browse files
fix(parameters): 🐛 Fixed handling of .bicepparam files
1 parent d4ac18f commit 4ec51de

File tree

9 files changed

+56
-22
lines changed

9 files changed

+56
-22
lines changed

ace/Caching/AzureStorageCacheHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public AzureStorageCacheHandler(string key, string accountName)
3535
this.key = this.GenerateKey(key);
3636
}
3737

38-
public AzureStorageCacheHandler(string scopeId, string? resourceGroupName, string template, string parameters, string accountName)
38+
public AzureStorageCacheHandler(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters, string accountName)
3939
: this(accountName)
4040
{
4141
this.key = this.GenerateKey(scopeId, resourceGroupName, template, parameters);
4242
}
4343

44-
private string GenerateKey(string scopeId, string? resourceGroupName, string template, string parameters)
44+
private string GenerateKey(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters)
4545
{
4646
return this.GenerateKey($"{scopeId}|{resourceGroupName}|{template}|{parameters}");
4747
}

ace/Caching/LocalCacheHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static LocalCacheHandler()
1919
}
2020
}
2121

22-
public LocalCacheHandler(string scopeId, string? resourceGroupName, string template, string parameters)
22+
public LocalCacheHandler(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters)
2323
{
2424
this.key = this.GenerateKey(scopeId, resourceGroupName, template, parameters);
2525
}
@@ -32,7 +32,7 @@ public LocalCacheHandler()
3232
this.key = this.GenerateKey("vm");
3333
}
3434

35-
private string GenerateKey(string scopeId, string? resourceGroupName, string template, string parameters)
35+
private string GenerateKey(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters)
3636
{
3737
return this.GenerateKey($"{scopeId}|{resourceGroupName}|{template}|{parameters}");
3838
}

ace/EstimatePayload.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ internal class EstimatePayload
1010
public EstimatePayloadProperties properties { get; }
1111
public string? location { get; }
1212

13-
public EstimatePayload(string template, DeploymentMode deploymentMode, string parameters)
13+
public EstimatePayload(string template, DeploymentMode deploymentMode, ParametersSchema parameters)
1414
{
1515
properties = new EstimatePayloadProperties(template, deploymentMode, parameters);
1616
}
1717

18-
public EstimatePayload(string template, DeploymentMode deploymentMode, string parameters, string? location)
18+
public EstimatePayload(string template, DeploymentMode deploymentMode, ParametersSchema parameters, string? location)
1919
{
2020
properties = new EstimatePayloadProperties(template, deploymentMode, parameters);
2121
this.location = location;
@@ -30,12 +30,10 @@ internal class EstimatePayloadProperties
3030
[JsonConverter(typeof(RawJsonConverter))]
3131
public string parameters { get; }
3232

33-
public EstimatePayloadProperties(string template, DeploymentMode deploymentMode, string parameters)
33+
public EstimatePayloadProperties(string template, DeploymentMode deploymentMode, ParametersSchema parameters)
3434
{
35-
var templateParametersObject = JsonSerializer.Deserialize<TemplateParameters>(parameters);
36-
3735
this.template = template;
38-
this.parameters = JsonSerializer.Serialize(templateParametersObject?.parameters, new JsonSerializerOptions()
36+
this.parameters = JsonSerializer.Serialize(parameters.Parameters, new JsonSerializerOptions()
3937
{
4038
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
4139
});

ace/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,11 @@ private static async Task<ApplicationResult> Estimate(FileInfo templateFile,
241241
}
242242

243243
var parameters = "{}";
244+
var isUsingBicepparamFile = false;
244245
if (options.ParametersFile != null)
245-
{
246-
var fileContent = options.ParametersFile.FullName.EndsWith(".bicepparam") ? new BicepCompiler(logger).CompileBicepparam(options.ParametersFile, _cancellationTokenSource.Token) : File.ReadAllText(options.ParametersFile.FullName);
246+
{
247+
isUsingBicepparamFile = options.ParametersFile.FullName.EndsWith(".bicepparam");
248+
var fileContent = isUsingBicepparamFile ? new BicepCompiler(logger).CompileBicepparam(options.ParametersFile, _cancellationTokenSource.Token) : File.ReadAllText(options.ParametersFile.FullName);
247249
if (fileContent == null)
248250
{
249251
var error = $"Couldn't read parameters file {options.ParametersFile.FullName}";
@@ -260,7 +262,7 @@ private static async Task<ApplicationResult> Estimate(FileInfo templateFile,
260262
{
261263
try
262264
{
263-
parser = new TemplateParser(template, parameters, options.InlineParameters, scopeId, resourceGroupName, logger);
265+
parser = new TemplateParser(template, parameters, options.InlineParameters, scopeId, resourceGroupName, isUsingBicepparamFile, logger);
264266
}
265267
catch (JsonException ex)
266268
{
@@ -280,7 +282,7 @@ private static async Task<ApplicationResult> Estimate(FileInfo templateFile,
280282
}
281283
}
282284

283-
var whatIfParser = new WhatIfParser(templateType, scopeId, resourceGroupName, template, parameters, logger, commandType, location, options);
285+
var whatIfParser = new WhatIfParser(templateType, scopeId, resourceGroupName, template, parser.Parameters, logger, commandType, location, options);
284286
var whatIfData = await whatIfParser.GetWhatIfData(_cancellationTokenSource.Token);
285287
if (whatIfData == null)
286288
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace ACE.Template;
4+
5+
internal class BicepparamParametersSchema
6+
{
7+
[JsonPropertyName("parametersJson")]
8+
public string? ParametersJson { get; set; }
9+
}

ace/Template/ParametersSchema.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Serialization;
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
23

34
internal class ParametersSchema
45
{
@@ -16,6 +17,11 @@ public ParametersSchema()
1617
Schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#";
1718
ContentVersion = "1.0.0.0";
1819
}
20+
21+
public override string ToString()
22+
{
23+
return JsonSerializer.Serialize(this);
24+
}
1925
}
2026

2127
internal class Parameter

ace/Template/TemplateParser.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
using Microsoft.Extensions.Logging;
1+
using ACE.Template;
2+
using Microsoft.Extensions.Logging;
23
using System.Text.Encodings.Web;
34
using System.Text.Json;
45

56
internal class TemplateParser
67
{
78
public TemplateSchema Template { get; private set; }
9+
public ParametersSchema? Parameters => this.parameters;
10+
811
private readonly ParametersSchema? parameters;
912
private readonly IEnumerable<string>? inlineParameters;
1013
private readonly string scopeId;
1114
private readonly string? resourceGroupName;
1215
private readonly ILogger logger;
1316

14-
public TemplateParser(string template, string parameters, IEnumerable<string>? inlineParameters, string scopeId, string? resourceGroupName, ILogger logger)
17+
public TemplateParser(
18+
string template,
19+
string parameters,
20+
IEnumerable<string>? inlineParameters,
21+
string scopeId,
22+
string? resourceGroupName,
23+
bool isUsingBicepparamFile,
24+
ILogger logger)
1525
{
1626
var t = JsonSerializer.Deserialize<TemplateSchema>(template) ?? throw new InvalidOperationException("Couldn't parse given template.");
1727
Template = t;
1828

19-
this.parameters = JsonSerializer.Deserialize<ParametersSchema>(parameters);
29+
if(isUsingBicepparamFile)
30+
{
31+
var bicepparamSchema = JsonSerializer.Deserialize<BicepparamParametersSchema>(parameters);
32+
this.parameters = JsonSerializer.Deserialize<ParametersSchema>(bicepparamSchema?.ParametersJson ?? "{}");
33+
}
34+
else
35+
{
36+
this.parameters = JsonSerializer.Deserialize<ParametersSchema>(parameters);
37+
}
38+
2039
this.inlineParameters = inlineParameters;
2140
this.scopeId = scopeId;
2241
this.resourceGroupName = resourceGroupName;

ace/WhatIf/AzureWhatIfHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal class AzureWhatIfHandler
1818
private readonly string? resourceGroupName;
1919
private readonly string template;
2020
private readonly DeploymentMode deploymentMode;
21-
private readonly string parameters;
21+
private readonly ParametersSchema parameters;
2222
private readonly ILogger logger;
2323
private readonly CommandType commandType;
2424
private readonly string? location;
@@ -28,7 +28,7 @@ internal class AzureWhatIfHandler
2828
public AzureWhatIfHandler(string scopeId,
2929
string? resourceGroupName,
3030
string template,
31-
string parameters,
31+
ParametersSchema parameters,
3232
ILogger logger,
3333
CommandType commandType,
3434
string? location,

ace/WhatIf/WhatIfParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal class WhatIfParser
99
private readonly string scopeId;
1010
private readonly string? resourceGroupName;
1111
private readonly string template;
12-
private readonly string parameters;
12+
private readonly ParametersSchema parameters;
1313
private readonly ILogger<Program> logger;
1414
private readonly CommandType commandType;
1515
private readonly string? location;
@@ -20,7 +20,7 @@ public WhatIfParser(
2020
string scopeId,
2121
string? resourceGroupName,
2222
string template,
23-
string parameters,
23+
ParametersSchema parameters,
2424
ILogger<Program> logger,
2525
CommandType commandType,
2626
string? location,

0 commit comments

Comments
 (0)