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

Support .list() on a variable reference to a resource #5869

Merged
merged 2 commits into from
Feb 7, 2022
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
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