-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixes UDF reference to exported variable #3120 (#3125) * Fixes name expand of existing resource references #3123 (#3126) * Release v1.39.2 (#3127)
- Loading branch information
1 parent
a3c6ae5
commit ab90306
Showing
14 changed files
with
310 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/PSRule.Rules.Azure/Data/Template/UserDefinedFunctionContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace PSRule.Rules.Azure.Data.Template; | ||
|
||
#nullable enable | ||
|
||
/// <summary> | ||
/// A nested context for user-defined functions. | ||
/// </summary> | ||
/// <param name="context">The parent <see cref="ITemplateContext"/> context.</param> | ||
internal sealed class UserDefinedFunctionContext(ITemplateContext context) : NestedTemplateContext(context) | ||
{ | ||
private const string PROPERTY_NAME = "name"; | ||
|
||
private readonly Dictionary<string, object> _Parameters = new(StringComparer.OrdinalIgnoreCase); | ||
|
||
public override bool TryParameter(string parameterName, out object? value) | ||
{ | ||
return _Parameters.TryGetValue(parameterName, out value); | ||
} | ||
|
||
public override bool TryVariable(string variableName, out object? value) | ||
{ | ||
return base.TryVariable(variableName, out value); | ||
} | ||
|
||
internal void SetParameters(JObject[] parameters, object[]? args) | ||
{ | ||
if (parameters == null || parameters.Length == 0 || args == null || args.Length == 0) | ||
return; | ||
|
||
for (var i = 0; i < parameters.Length; i++) | ||
{ | ||
var name = parameters[i][PROPERTY_NAME]?.Value<string>(); | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
_Parameters.Add(name!, args[i]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#nullable restore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/PSRule.Rules.Azure.Tests/Bicep/SymbolicNameTestCases/Tests.Bicep.3.bicep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/3123 | ||
// Contributed by @CharlesToniolo | ||
|
||
var webAppsNames = [ | ||
'example1' | ||
'example2' | ||
] | ||
|
||
resource webApps 'Microsoft.Web/sites@2022-09-01' existing = [ | ||
for webAppName in webAppsNames: { | ||
name: webAppName | ||
} | ||
] | ||
|
||
#disable-next-line no-unused-existing-resources | ||
resource webAppSettings 'Microsoft.Web/sites/config@2022-09-01' existing = [ | ||
for i in range(0, length(webAppsNames)): { | ||
name: 'appsettings' | ||
parent: webApps[i] | ||
} | ||
] |
43 changes: 43 additions & 0 deletions
43
tests/PSRule.Rules.Azure.Tests/Bicep/SymbolicNameTestCases/Tests.Bicep.3.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"$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.30.23.60470", | ||
"templateHash": "3757483382650491065" | ||
} | ||
}, | ||
"variables": { | ||
"webAppsNames": [ | ||
"example1", | ||
"example2" | ||
] | ||
}, | ||
"resources": { | ||
"webApps": { | ||
"copy": { | ||
"name": "webApps", | ||
"count": "[length(variables('webAppsNames'))]" | ||
}, | ||
"existing": true, | ||
"type": "Microsoft.Web/sites", | ||
"apiVersion": "2022-09-01", | ||
"name": "[variables('webAppsNames')[copyIndex()]]" | ||
}, | ||
"webAppSettings": { | ||
"copy": { | ||
"name": "webAppSettings", | ||
"count": "[length(range(0, length(variables('webAppsNames'))))]" | ||
}, | ||
"existing": true, | ||
"type": "Microsoft.Web/sites/config", | ||
"apiVersion": "2022-09-01", | ||
"name": "[format('{0}/{1}', variables('webAppsNames')[range(0, length(variables('webAppsNames')))[copyIndex()]], 'appsettings')]", | ||
"dependsOn": [ | ||
"[format('webApps[{0}]', range(0, length(variables('webAppsNames')))[copyIndex()])]" | ||
] | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/PSRule.Rules.Azure.Tests/Bicep/UserDefinedFunctionTestCases/Tests.Bicep.1.bicep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/3120 | ||
// Based on work contributed by @GABRIELNGBTUC | ||
|
||
import * as child from './Tests.Bicep.1.child.bicep' | ||
|
||
var v1 = [] | ||
var v2 = [ | ||
2 | ||
] | ||
|
||
func getV3() array => union(v1, v2) | ||
|
||
output o1 array = getV3() | ||
output o2 array = child.getV3() | ||
output o3 array = union(child.v1, child.v2) | ||
output o4 array = union(v2, child.v2) | ||
output o5 array = child.getV4() |
18 changes: 18 additions & 0 deletions
18
tests/PSRule.Rules.Azure.Tests/Bicep/UserDefinedFunctionTestCases/Tests.Bicep.1.child.bicep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
@export() | ||
var v1 = [] | ||
|
||
@export() | ||
var v2 = [ | ||
1 | ||
] | ||
|
||
@export() | ||
func getV3() array => union(v1, v2) | ||
|
||
import * as child2 from './Tests.Bicep.1.child2.bicep' | ||
|
||
@export() | ||
func getV4() array => union(child2.v1, child2.v2) |
10 changes: 10 additions & 0 deletions
10
tests/PSRule.Rules.Azure.Tests/Bicep/UserDefinedFunctionTestCases/Tests.Bicep.1.child2.bicep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
@export() | ||
var v1 = [] | ||
|
||
@export() | ||
var v2 = [ | ||
3 | ||
] |
92 changes: 92 additions & 0 deletions
92
tests/PSRule.Rules.Azure.Tests/Bicep/UserDefinedFunctionTestCases/Tests.Bicep.1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{ | ||
"$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.30.23.60470", | ||
"templateHash": "3104036790319433669" | ||
} | ||
}, | ||
"functions": [ | ||
{ | ||
"namespace": "__bicep", | ||
"members": { | ||
"getV3": { | ||
"parameters": [], | ||
"output": { | ||
"type": "array", | ||
"value": "[union(variables('v1'), variables('v2'))]" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"namespace": "_1", | ||
"members": { | ||
"getV3": { | ||
"parameters": [], | ||
"output": { | ||
"type": "array", | ||
"value": "[union(variables('_1.v1'), variables('_1.v2'))]" | ||
}, | ||
"metadata": { | ||
"__bicep_imported_from!": { | ||
"sourceTemplate": "Tests.Bicep.1.child.bicep" | ||
} | ||
} | ||
}, | ||
"getV4": { | ||
"parameters": [], | ||
"output": { | ||
"type": "array", | ||
"value": "[union(variables('_2.v1'), variables('_2.v2'))]" | ||
}, | ||
"metadata": { | ||
"__bicep_imported_from!": { | ||
"sourceTemplate": "Tests.Bicep.1.child.bicep" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"variables": { | ||
"v1": [], | ||
"v2": [ | ||
2 | ||
], | ||
"_1.v1": [], | ||
"_1.v2": [ | ||
1 | ||
], | ||
"_2.v1": [], | ||
"_2.v2": [ | ||
3 | ||
] | ||
}, | ||
"resources": {}, | ||
"outputs": { | ||
"o1": { | ||
"type": "array", | ||
"value": "[__bicep.getV3()]" | ||
}, | ||
"o2": { | ||
"type": "array", | ||
"value": "[_1.getV3()]" | ||
}, | ||
"o3": { | ||
"type": "array", | ||
"value": "[union(variables('_1.v1'), variables('_1.v2'))]" | ||
}, | ||
"o4": { | ||
"type": "array", | ||
"value": "[union(variables('v2'), variables('_1.v2'))]" | ||
}, | ||
"o5": { | ||
"type": "array", | ||
"value": "[_1.getV4()]" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.