Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes user defined function not found when used as parameter default … #3172

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers
- Bug fixes:
- Fixed projection of default role authorization property `principalType` by @BernieWhite.
[#3163](https://github.com/Azure/PSRule.Rules.Azure/issues/3163)
- Fixed user defined function not found when used as parameter default by @BernieWhite.
[#3169](https://github.com/Azure/PSRule.Rules.Azure/issues/3169)

## v1.40.0-B0063 (pre-release)

Expand Down
6 changes: 3 additions & 3 deletions src/PSRule.Rules.Azure/Data/Template/TemplateVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,12 @@ protected virtual void Template(TemplateContext context, string deploymentName,
if (TryObjectProperty(template, PROPERTY_VARIABLES, out var variables))
FunctionVariables(context, variables);

if (TryObjectProperty(template, PROPERTY_PARAMETERS, out var parameters))
Parameters(context, parameters);

if (TryArrayProperty(template, PROPERTY_FUNCTIONS, out var functions))
Functions(context, functions);

if (TryObjectProperty(template, PROPERTY_PARAMETERS, out var parameters))
Parameters(context, parameters);

if (TryObjectProperty(template, PROPERTY_VARIABLES, out variables))
Variables(context, variables);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Linq;
using Newtonsoft.Json.Linq;
using PSRule.Rules.Azure.Data.Template;

Expand Down Expand Up @@ -31,4 +32,18 @@ public void ProcessTemplate_WhenUserDefinedFunctionReferencesExportedVariables_S
Assert.True(templateContext.RootDeployment.TryOutput("o5", out JObject o5));
Assert.Equal([3], o5["value"].Values<int>());
}

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

Assert.NotNull(resources);

var actual = resources.Where(r => r["type"].Value<string>() == "Microsoft.Storage/storageAccounts").FirstOrDefault();
Assert.Equal("sa5f3e65afb63bb1", actual["name"].Value<string>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/3169
// Based on work contributed by @modbase

targetScope = 'resourceGroup'

module storage 'Tests.Bicep.2.child.bicep' = {
name: 'storage'
params: {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

targetScope = 'resourceGroup'

param name string = customNamingFunction('sa', 1)

import { customNamingFunction } from './Tests.Bicep.2.fn.bicep'

resource storage 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: name
location: resourceGroup().location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

targetScope = 'resourceGroup'

// A custom naming function.
@export()
func customNamingFunction(prefix string, instance int) string =>
'${prefix}${uniqueString(resourceGroup().id)}${instance}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.31.34.60546",
"templateHash": "6735599436485338599"
}
},
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2022-09-01",
"name": "storage",
"properties": {
"expressionEvaluationOptions": {
"scope": "inner"
},
"mode": "Incremental",
"parameters": {},
"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.31.34.60546",
"templateHash": "12715503820119808772"
}
},
"functions": [
{
"namespace": "__bicep",
"members": {
"customNamingFunction": {
"parameters": [
{
"type": "string",
"name": "prefix"
},
{
"type": "int",
"name": "instance"
}
],
"output": {
"type": "string",
"value": "[format('{0}{1}{2}', parameters('prefix'), uniqueString(resourceGroup().id), parameters('instance'))]"
},
"metadata": {
"__bicep_imported_from!": {
"sourceTemplate": "Tests.Bicep.2.fn.bicep"
}
}
}
}
}
],
"parameters": {
"name": {
"type": "string",
"defaultValue": "[__bicep.customNamingFunction('sa', 1)]"
}
},
"resources": {
"storage": {
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2023-05-01",
"name": "[parameters('name')]",
"location": "[resourceGroup().location]",
"kind": "StorageV2",
"sku": {
"name": "Standard_LRS"
}
}
}
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@
<None Update="Bicep\UserDefinedFunctionTestCases\Tests.Bicep.1.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Bicep\UserDefinedFunctionTestCases\Tests.Bicep.2.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Bicep\SymbolicNameTestCases\Tests.Bicep.3.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Loading