Skip to content

Commit 9d9bf2c

Browse files
authored
Fixes expand with direct outputs reference Azure#2935 (Azure#2940)
* Fixes expand with direct outputs reference Azure#2935 * Bump change log
1 parent a067414 commit 9d9bf2c

8 files changed

+136
-5
lines changed

docs/CHANGELOG-v1.md

+7
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers
3131

3232
What's changed since pre-release v1.38.0-B0011:
3333

34+
- New rules:
35+
- Azure Kubernetes Service:
36+
- Added check to automatically upgrade AKS cluster node image by @sharmilamusunuru.
37+
[#2445](https://github.com/Azure/PSRule.Rules.Azure/issues/2445)
3438
- Engineering:
3539
- Quality updates to rule documentation by @BernieWhite.
3640
[#2570](https://github.com/Azure/PSRule.Rules.Azure/issues/2570)
41+
- Bug fixes:
42+
- Fixed failed to expand with direct outputs reference by @BernieWhite.
43+
[#2935](https://github.com/Azure/PSRule.Rules.Azure/issues/2935)
3744

3845
## v1.38.0-B0011 (pre-release)
3946

src/PSRule.Rules.Azure/Data/Template/DeploymentValue.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using System;
@@ -59,7 +59,7 @@ public bool TryProperty(string propertyName, out object value)
5959
}
6060
}
6161

62-
private sealed class DeploymentOutputs : ILazyObject
62+
private sealed class DeploymentOutputs : JObject, ILazyObject
6363
{
6464
private readonly DeploymentValue _Deployment;
6565

@@ -77,6 +77,18 @@ public bool TryProperty(string propertyName, out object value)
7777
value = lazy.GetValue();
7878
return true;
7979
}
80+
81+
public override JToken this[object key]
82+
{
83+
get
84+
{
85+
return key is string propertyName && TryProperty(propertyName, out var o) ? JToken.FromObject(o) : default;
86+
}
87+
set
88+
{
89+
90+
}
91+
}
8092
}
8193

8294
public JObject Value => _Value.Value;

src/PSRule.Rules.Azure/Data/Template/ExpressionHelpers.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ internal static bool TryPropertyOrField(object o, string propertyName, out objec
205205
return true;
206206
}
207207

208+
if (o is ILazyObject lazy && lazy.TryProperty(propertyName, out value))
209+
return true;
210+
208211
if (o is JObject jObject)
209212
{
210213
if (!jObject.TryGetValue(propertyName, StringComparison.OrdinalIgnoreCase, out var propertyToken))
@@ -224,9 +227,6 @@ internal static bool TryPropertyOrField(object o, string propertyName, out objec
224227
return true;
225228
}
226229

227-
if (o is ILazyObject lazy && lazy.TryProperty(propertyName, out value))
228-
return true;
229-
230230
// Try dictionary
231231
if (o is IDictionary dictionary && dictionary.TryGetValue(propertyName, out value))
232232
return true;

tests/PSRule.Rules.Azure.Tests/PSRule.Rules.Azure.Tests.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@
257257
<None Update="Tests.Bicep.4.json">
258258
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
259259
</None>
260+
<None Update="Tests.Bicep.40.json">
261+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
262+
</None>
260263
<None Update="Tests.Bicep.5.json">
261264
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
262265
</None>

tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,21 @@ public void ProcessTemplate_WhenUsingSpreadOperator_ReturnsSingleObject()
12491249
Assert.Equal(new string[] { "bar" }, output["value"].Values<string>());
12501250
}
12511251

1252+
/// <summary>
1253+
/// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2935
1254+
/// </summary>
1255+
[Fact]
1256+
public void ProcessTemplate_WhenOutputsPropertyDirectly_ReturnsObject()
1257+
{
1258+
_ = ProcessTemplate(GetSourcePath("Tests.Bicep.40.json"), null, out var templateContext);
1259+
1260+
Assert.True(templateContext.RootDeployment.TryOutput("items", out JObject output));
1261+
var items = output["value"].Value<JArray>();
1262+
1263+
Assert.Equal("/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/ps-rule-test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/child", items[0].Value<string>());
1264+
Assert.NotNull(items[1].Value<JObject>());
1265+
}
1266+
12521267
#region Helper methods
12531268

12541269
private static string GetSourcePath(string fileName)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2935
5+
6+
module child './Tests.Bicep.40.child.bicep' = {
7+
name: 'child'
8+
params: {
9+
name: 'child'
10+
}
11+
}
12+
13+
output items array = union([child.outputs.id, child.outputs], [])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
param name string
5+
param location string = resourceGroup().location
6+
7+
resource identity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
8+
name: name
9+
location: location
10+
}
11+
12+
output id string = identity.id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"metadata": {
5+
"_generator": {
6+
"name": "bicep",
7+
"version": "0.28.1.47646",
8+
"templateHash": "3288768750295631438"
9+
}
10+
},
11+
"resources": [
12+
{
13+
"type": "Microsoft.Resources/deployments",
14+
"apiVersion": "2022-09-01",
15+
"name": "child",
16+
"properties": {
17+
"expressionEvaluationOptions": {
18+
"scope": "inner"
19+
},
20+
"mode": "Incremental",
21+
"parameters": {
22+
"name": {
23+
"value": "child"
24+
}
25+
},
26+
"template": {
27+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
28+
"contentVersion": "1.0.0.0",
29+
"metadata": {
30+
"_generator": {
31+
"name": "bicep",
32+
"version": "0.28.1.47646",
33+
"templateHash": "4224472757695175223"
34+
}
35+
},
36+
"parameters": {
37+
"name": {
38+
"type": "string"
39+
},
40+
"location": {
41+
"type": "string",
42+
"defaultValue": "[resourceGroup().location]"
43+
}
44+
},
45+
"resources": [
46+
{
47+
"type": "Microsoft.ManagedIdentity/userAssignedIdentities",
48+
"apiVersion": "2023-01-31",
49+
"name": "[parameters('name')]",
50+
"location": "[parameters('location')]"
51+
}
52+
],
53+
"outputs": {
54+
"id": {
55+
"type": "string",
56+
"value": "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', parameters('name'))]"
57+
}
58+
}
59+
}
60+
}
61+
}
62+
],
63+
"outputs": {
64+
"items": {
65+
"type": "array",
66+
"value": "[union(createArray(reference(resourceId('Microsoft.Resources/deployments', 'child'), '2022-09-01').outputs.id.value, reference(resourceId('Microsoft.Resources/deployments', 'child'), '2022-09-01').outputs), createArray())]"
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)