Skip to content

Commit

Permalink
fix(parameters): 🐛 Fixed handling of .bicepparam files
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-mrzyglod committed Aug 23, 2024
1 parent d4ac18f commit 4ec51de
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions ace/Caching/AzureStorageCacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public AzureStorageCacheHandler(string key, string accountName)
this.key = this.GenerateKey(key);
}

public AzureStorageCacheHandler(string scopeId, string? resourceGroupName, string template, string parameters, string accountName)
public AzureStorageCacheHandler(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters, string accountName)
: this(accountName)
{
this.key = this.GenerateKey(scopeId, resourceGroupName, template, parameters);
}

private string GenerateKey(string scopeId, string? resourceGroupName, string template, string parameters)
private string GenerateKey(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters)
{
return this.GenerateKey($"{scopeId}|{resourceGroupName}|{template}|{parameters}");
}
Expand Down
4 changes: 2 additions & 2 deletions ace/Caching/LocalCacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static LocalCacheHandler()
}
}

public LocalCacheHandler(string scopeId, string? resourceGroupName, string template, string parameters)
public LocalCacheHandler(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters)
{
this.key = this.GenerateKey(scopeId, resourceGroupName, template, parameters);
}
Expand All @@ -32,7 +32,7 @@ public LocalCacheHandler()
this.key = this.GenerateKey("vm");
}

private string GenerateKey(string scopeId, string? resourceGroupName, string template, string parameters)
private string GenerateKey(string scopeId, string? resourceGroupName, string template, ParametersSchema parameters)
{
return this.GenerateKey($"{scopeId}|{resourceGroupName}|{template}|{parameters}");
}
Expand Down
10 changes: 4 additions & 6 deletions ace/EstimatePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ internal class EstimatePayload
public EstimatePayloadProperties properties { get; }
public string? location { get; }

public EstimatePayload(string template, DeploymentMode deploymentMode, string parameters)
public EstimatePayload(string template, DeploymentMode deploymentMode, ParametersSchema parameters)
{
properties = new EstimatePayloadProperties(template, deploymentMode, parameters);
}

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

public EstimatePayloadProperties(string template, DeploymentMode deploymentMode, string parameters)
public EstimatePayloadProperties(string template, DeploymentMode deploymentMode, ParametersSchema parameters)
{
var templateParametersObject = JsonSerializer.Deserialize<TemplateParameters>(parameters);

this.template = template;
this.parameters = JsonSerializer.Serialize(templateParametersObject?.parameters, new JsonSerializerOptions()
this.parameters = JsonSerializer.Serialize(parameters.Parameters, new JsonSerializerOptions()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
Expand Down
10 changes: 6 additions & 4 deletions ace/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ private static async Task<ApplicationResult> Estimate(FileInfo templateFile,
}

var parameters = "{}";
var isUsingBicepparamFile = false;
if (options.ParametersFile != null)
{
var fileContent = options.ParametersFile.FullName.EndsWith(".bicepparam") ? new BicepCompiler(logger).CompileBicepparam(options.ParametersFile, _cancellationTokenSource.Token) : File.ReadAllText(options.ParametersFile.FullName);
{
isUsingBicepparamFile = options.ParametersFile.FullName.EndsWith(".bicepparam");
var fileContent = isUsingBicepparamFile ? new BicepCompiler(logger).CompileBicepparam(options.ParametersFile, _cancellationTokenSource.Token) : File.ReadAllText(options.ParametersFile.FullName);
if (fileContent == null)
{
var error = $"Couldn't read parameters file {options.ParametersFile.FullName}";
Expand All @@ -260,7 +262,7 @@ private static async Task<ApplicationResult> Estimate(FileInfo templateFile,
{
try
{
parser = new TemplateParser(template, parameters, options.InlineParameters, scopeId, resourceGroupName, logger);
parser = new TemplateParser(template, parameters, options.InlineParameters, scopeId, resourceGroupName, isUsingBicepparamFile, logger);
}
catch (JsonException ex)
{
Expand All @@ -280,7 +282,7 @@ private static async Task<ApplicationResult> Estimate(FileInfo templateFile,
}
}

var whatIfParser = new WhatIfParser(templateType, scopeId, resourceGroupName, template, parameters, logger, commandType, location, options);
var whatIfParser = new WhatIfParser(templateType, scopeId, resourceGroupName, template, parser.Parameters, logger, commandType, location, options);
var whatIfData = await whatIfParser.GetWhatIfData(_cancellationTokenSource.Token);
if (whatIfData == null)
{
Expand Down
9 changes: 9 additions & 0 deletions ace/Template/BicepparamParametersSchema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace ACE.Template;

internal class BicepparamParametersSchema
{
[JsonPropertyName("parametersJson")]
public string? ParametersJson { get; set; }
}
8 changes: 7 additions & 1 deletion ace/Template/ParametersSchema.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;

internal class ParametersSchema
{
Expand All @@ -16,6 +17,11 @@ public ParametersSchema()
Schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#";
ContentVersion = "1.0.0.0";
}

public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}

internal class Parameter
Expand Down
25 changes: 22 additions & 3 deletions ace/Template/TemplateParser.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
using Microsoft.Extensions.Logging;
using ACE.Template;
using Microsoft.Extensions.Logging;
using System.Text.Encodings.Web;
using System.Text.Json;

internal class TemplateParser
{
public TemplateSchema Template { get; private set; }
public ParametersSchema? Parameters => this.parameters;

private readonly ParametersSchema? parameters;
private readonly IEnumerable<string>? inlineParameters;
private readonly string scopeId;
private readonly string? resourceGroupName;
private readonly ILogger logger;

public TemplateParser(string template, string parameters, IEnumerable<string>? inlineParameters, string scopeId, string? resourceGroupName, ILogger logger)
public TemplateParser(
string template,
string parameters,
IEnumerable<string>? inlineParameters,
string scopeId,
string? resourceGroupName,
bool isUsingBicepparamFile,
ILogger logger)
{
var t = JsonSerializer.Deserialize<TemplateSchema>(template) ?? throw new InvalidOperationException("Couldn't parse given template.");
Template = t;

this.parameters = JsonSerializer.Deserialize<ParametersSchema>(parameters);
if(isUsingBicepparamFile)
{
var bicepparamSchema = JsonSerializer.Deserialize<BicepparamParametersSchema>(parameters);
this.parameters = JsonSerializer.Deserialize<ParametersSchema>(bicepparamSchema?.ParametersJson ?? "{}");
}
else
{
this.parameters = JsonSerializer.Deserialize<ParametersSchema>(parameters);
}

this.inlineParameters = inlineParameters;
this.scopeId = scopeId;
this.resourceGroupName = resourceGroupName;
Expand Down
4 changes: 2 additions & 2 deletions ace/WhatIf/AzureWhatIfHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class AzureWhatIfHandler
private readonly string? resourceGroupName;
private readonly string template;
private readonly DeploymentMode deploymentMode;
private readonly string parameters;
private readonly ParametersSchema parameters;
private readonly ILogger logger;
private readonly CommandType commandType;
private readonly string? location;
Expand All @@ -28,7 +28,7 @@ internal class AzureWhatIfHandler
public AzureWhatIfHandler(string scopeId,
string? resourceGroupName,
string template,
string parameters,
ParametersSchema parameters,
ILogger logger,
CommandType commandType,
string? location,
Expand Down
4 changes: 2 additions & 2 deletions ace/WhatIf/WhatIfParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class WhatIfParser
private readonly string scopeId;
private readonly string? resourceGroupName;
private readonly string template;
private readonly string parameters;
private readonly ParametersSchema parameters;
private readonly ILogger<Program> logger;
private readonly CommandType commandType;
private readonly string? location;
Expand All @@ -20,7 +20,7 @@ public WhatIfParser(
string scopeId,
string? resourceGroupName,
string template,
string parameters,
ParametersSchema parameters,
ILogger<Program> logger,
CommandType commandType,
string? location,
Expand Down

0 comments on commit 4ec51de

Please sign in to comment.