Skip to content

Commit

Permalink
Support .list() on a variable reference to a resource (Azure#5869)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin authored and takekazuomi committed Feb 11, 2022
1 parent 15dceda commit 6e4e4ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
24 changes: 23 additions & 1 deletion src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,7 @@ public void Test_Issue5456_2()

/// <summary>
/// https://github.com/Azure/bicep/issues/3114
/// </summary>
/// </summary>
[TestMethod]
public void Test_Issue3114()
{
Expand All @@ -2943,5 +2943,27 @@ public void Test_Issue3114()
result.Template.Should().NotBeNull();
result.Template.Should().HaveValueAtPath("$.outputs['contentVersion'].value", "[deployment().properties.template.contentVersion]");
}

// https://github.com/Azure/bicep/issues/4833
[TestMethod]
public void Test_Issue4833()
{
var result = CompilationHelper.Compile(@"
param storageName string
resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' existing = {
name: storageName
}
var storage = stg
output badResult object = {
value: storage.listAnything().keys[0].value
}");

result.Template.Should().HaveValueAtPath("$.outputs['badResult'].value", new JObject {
["value"] = "[listAnything(resourceId('Microsoft.Storage/storageAccounts', parameters('storageName')), '2021-04-01').keys[0].value]",
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void AssigningResourceToVariable_ShouldNotGenerateVariables()
},
"Referenced resource should be added to depends on section");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'test2')].properties.allowBlobPublicAccess",
"[reference(resourceId('Microsoft.Storage/storageAccounts', 'test'), '2019-06-01', 'full').properties.allowBlobPublicAccess]",
"[reference(resourceId('Microsoft.Storage/storageAccounts', 'test')).allowBlobPublicAccess]",
"Resource access should be in-lined");
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public void AssigningResourceToVariable_ShouldNotGenerateVariables_ChainedVariab
},
"Referenced resource should be added to depends on section");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'test2')].properties.allowBlobPublicAccess",
"[reference(resourceId('Microsoft.Storage/storageAccounts', 'test'), '2019-06-01', 'full').properties.allowBlobPublicAccess]",
"[reference(resourceId('Microsoft.Storage/storageAccounts', 'test')).allowBlobPublicAccess]",
"Resource access should be in-lined");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core/Emit/ExpressionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private LanguageExpression ConvertFunction(FunctionCallSyntaxBase functionCall)
return CreateFunction(
instanceFunctionCall.Name.IdentifierName,
instanceFunctionCall.Arguments.Select(a => ConvertExpression(a.Expression)));
case ResourceSymbol resourceSymbol when context.SemanticModel.ResourceMetadata.TryLookup(resourceSymbol.DeclaringSyntax) is { } resource:
case DeclaredSymbol declaredSymbol when context.SemanticModel.ResourceMetadata.TryLookup(declaredSymbol.DeclaringSyntax) is { } resource:
if (instanceFunctionCall.Name.IdentifierName.StartsWithOrdinalInsensitively("list"))
{
var converter = indexExpression is not null ?
Expand Down Expand Up @@ -178,7 +178,7 @@ public ExpressionConverter CreateConverterForIndexReplacement(SyntaxBase nameSyn
{
case 0:
// moving the name expression does not produce any inaccessible locals (no locals means no loops)
// regardless if there is an index expression or not, we don't need to append replacements
// regardless if there is an index expression or not, we don't need to append replacements
return this;

case 1 when indexExpression != null:
Expand Down
2 changes: 2 additions & 0 deletions src/Bicep.Core/Semantics/Metadata/ResourceMetadataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public ResourceMetadataCache(SemanticModel semanticModel)

break;
}
case VariableDeclarationSyntax variableDeclarationSyntax:
return this.TryLookup(variableDeclarationSyntax.Value);
}

return null;
Expand Down

0 comments on commit 6e4e4ae

Please sign in to comment.