Skip to content

Commit

Permalink
Fixes null parameter overrides default #2795 (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Apr 3, 2024
1 parent d27f529 commit 908baf0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 32 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

What's changed since v1.35.0:

- Bug fixes:
- Fixed null parameter overrides default value by @BernieWhite.
[#2795](https://github.com/Azure/PSRule.Rules.Azure/issues/2795)

## v1.35.0

What's changed since v1.34.2:
Expand Down
3 changes: 3 additions & 0 deletions src/PSRule.Rules.Azure/Data/Template/TemplateVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ private bool AssignParameterLiteral(string name, JObject parameter)

private void AddParameterAssignment(string name, JToken value)
{
if (value == null || value.Type == JTokenType.Null || value.Type == JTokenType.Undefined)
return;

_ParameterAssignments.Add(name, value);
}

Expand Down
14 changes: 13 additions & 1 deletion tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,11 +1099,23 @@ public void UnionMockWithArray()
/// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2751.
/// </summary>
[Fact]
public void Index_into_mock_output_object()
public void ProcessTemplate_WhenIndexIntoMock_ShouldReturnMock()
{
var resources = ProcessTemplate(GetSourcePath("Tests.Bicep.35.json"), null, out _);
}

/// <summary>
/// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2795.
/// </summary>
[Fact]
public void ProcessTemplate_WhenParameterNullWithDefault_ShouldUseDefault()
{
var resources = ProcessTemplate(GetSourcePath("Tests.Bicep.27.json"), null, out _);

var actual = resources.FirstOrDefault(r => r["type"].Value<string>() == "Microsoft.Storage/storageAccounts");
Assert.Equal("Standard_LRS", actual["sku"]["name"].Value<string>());
}

#region Helper methods

private static string GetSourcePath(string fileName)
Expand Down
3 changes: 3 additions & 0 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

module child 'Tests.Bicep.27.child.bicep' = {
name: 'child'
params: {
skuName: null
}
}

output childFromFor string = child.outputs.fromFor
59 changes: 33 additions & 26 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.child.bicep
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

param skuName string = 'Standard_LRS'
param minTLSVersion string?
param corsRules corsRule

Expand All @@ -17,11 +18,13 @@ param accessPolicies array?
@secure()
param secrets object?

var formattedAccessPolicies = [for accessPolicy in (accessPolicies ?? []): {
objectId: accessPolicy.objectId
tenantId: contains(accessPolicy, 'tenantId') ? accessPolicy.tenantId : tenant().tenantId
permissions: {}
}]
var formattedAccessPolicies = [
for accessPolicy in (accessPolicies ?? []): {
objectId: accessPolicy.objectId
tenantId: contains(accessPolicy, 'tenantId') ? accessPolicy.tenantId : tenant().tenantId
permissions: {}
}
]

var secretList = secrets.?secureList ?? []

Expand All @@ -30,7 +33,7 @@ resource storage 'Microsoft.Storage/storageAccounts@2023-01-01' = {
#disable-next-line no-loc-expr-outside-params
location: resourceGroup().location
sku: {
name: 'Standard_LRS'
name: skuName
}
kind: 'StorageV2'
properties: {
Expand Down Expand Up @@ -61,12 +64,14 @@ resource kv 'Microsoft.KeyVault/vaults@2023-07-01' = {
}
}

resource kvSecret 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = [for item in secretList: {
name: item.name
properties: {
value: item.value
resource kvSecret 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = [
for item in secretList: {
name: item.name
properties: {
value: item.value
}
}
}]
]

resource storageAccount_objectReplicationPolicy 'Microsoft.Storage/storageAccounts/objectReplicationPolicies@2022-09-01' = {
name: 'default'
Expand All @@ -85,22 +90,24 @@ resource storageAccount_objectReplicationPolicy 'Microsoft.Storage/storageAccoun
}
}

resource storageAccount_objectReplicationPolicyItems 'Microsoft.Storage/storageAccounts/objectReplicationPolicies@2022-09-01' = [for (item, index) in [ 1 ]: {
name: 'default${index}'
parent: storage
properties: {
sourceAccount: 'sourceId'
destinationAccount: 'destId'
rules: [
{
ruleId: null
sourceContainer: 'source'
destinationContainer: 'dest'
filters: null
}
]
resource storageAccount_objectReplicationPolicyItems 'Microsoft.Storage/storageAccounts/objectReplicationPolicies@2022-09-01' = [
for (item, index) in [1]: {
name: 'default${index}'
parent: storage
properties: {
sourceAccount: 'sourceId'
destinationAccount: 'destId'
rules: [
{
ruleId: null
sourceContainer: 'source'
destinationContainer: 'dest'
filters: null
}
]
}
}
}]
]

output policyId string = storageAccount_objectReplicationPolicy.properties.policyId
output ruleIds string[] = map(storageAccount_objectReplicationPolicy.properties.rules, rule => rule.ruleId)
Expand Down
19 changes: 14 additions & 5 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.23.1.45101",
"templateHash": "1063354008601109842"
"version": "0.26.54.24096",
"templateHash": "7737727980270162371"
}
},
"resources": [
Expand All @@ -18,15 +18,20 @@
"scope": "inner"
},
"mode": "Incremental",
"parameters": {
"skuName": {
"value": null
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.23.1.45101",
"templateHash": "11238883118622659404"
"version": "0.26.54.24096",
"templateHash": "14665743710501821887"
}
},
"definitions": {
Expand Down Expand Up @@ -68,6 +73,10 @@
}
},
"parameters": {
"skuName": {
"type": "string",
"defaultValue": "Standard_LRS"
},
"minTLSVersion": {
"type": "string",
"nullable": true
Expand Down Expand Up @@ -118,7 +127,7 @@
"name": "test",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard_LRS"
"name": "[parameters('skuName')]"
},
"kind": "StorageV2",
"properties": {
Expand Down

0 comments on commit 908baf0

Please sign in to comment.