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

Emit API versions for conditional resource references #6163

Merged
merged 2 commits into from
Mar 9, 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
6 changes: 3 additions & 3 deletions src/Bicep.Core.Samples/Files/Loops_LF/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "6135893197178781329"
"templateHash": "12306829397421129331"
}
},
"parameters": {
Expand Down Expand Up @@ -850,11 +850,11 @@
},
"lastNameServers": {
"type": "array",
"value": "[reference(resourceId('Microsoft.Network/dnsZones', format('indexedZone-{0}-{1}', parameters('accounts')[sub(length(parameters('accounts')), 1)].name, sub(length(parameters('accounts')), 1)))).nameServers]"
"value": "[reference(resourceId('Microsoft.Network/dnsZones', format('indexedZone-{0}-{1}', parameters('accounts')[sub(length(parameters('accounts')), 1)].name, sub(length(parameters('accounts')), 1))), '2018-05-01').nameServers]"
},
"lastModuleOutput": {
"type": "string",
"value": "[reference(resourceId('Microsoft.Resources/deployments', format('stuff-{0}', sub(length(parameters('accounts')), 1)))).outputs.myOutput.value]"
"value": "[reference(resourceId('Microsoft.Resources/deployments', format('stuff-{0}', sub(length(parameters('accounts')), 1))), '2020-10-01').outputs.myOutput.value]"
}
}
}
6 changes: 3 additions & 3 deletions src/Bicep.Core.Samples/Files/NestedResources_LF/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "17608560117045014269"
"templateHash": "3051020952527793145"
}
},
"parameters": {
Expand Down Expand Up @@ -79,8 +79,8 @@
"apiVersion": "2020-12-01",
"name": "[format('{0}/{1}/{2}', 'conditionParent', 'conditionChild', 'conditionGrandchild')]",
"properties": {
"size": "[reference(resourceId('My.Rp/parentType', 'conditionParent')).size]",
"style": "[reference(resourceId('My.Rp/parentType/childType', 'conditionParent', 'conditionChild')).style]"
"size": "[reference(resourceId('My.Rp/parentType', 'conditionParent'), '2020-12-01').size]",
"style": "[reference(resourceId('My.Rp/parentType/childType', 'conditionParent', 'conditionChild'), '2020-12-01').style]"
},
"dependsOn": [
"[resourceId('My.Rp/parentType/childType', 'conditionParent', 'conditionChild')]",
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core.Samples/Files/Resources_CRLF/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "4897965198946026578"
"templateHash": "2564861032733655849"
}
},
"parameters": {
Expand Down Expand Up @@ -309,7 +309,7 @@
"name": "extensionDependencies",
"properties": {
"res1": "[resourceId('Microsoft.Compute/virtualMachines', 'vmName')]",
"res1runtime": "[reference(resourceId('Microsoft.Compute/virtualMachines', 'vmName')).something]",
"res1runtime": "[reference(resourceId('Microsoft.Compute/virtualMachines', 'vmName'), '2020-06-01').something]",
"res2": "[extensionResourceId(resourceId('Microsoft.Compute/virtualMachines', 'vmName'), 'My.Rp/extensionResource', 'extension')]",
"res2runtime": "[reference(extensionResourceId(resourceId('Microsoft.Compute/virtualMachines', 'vmName'), 'My.Rp/extensionResource', 'extension')).something]",
"res3": "[extensionResourceId(extensionResourceId(resourceId('Microsoft.Compute/virtualMachines', 'vmName'), 'My.Rp/extensionResource', 'extension'), 'My.Rp/extensionResource', 'extension')]",
Expand Down
9 changes: 7 additions & 2 deletions src/Bicep.Core/Emit/ExpressionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public FunctionExpression GetModuleOutputsReferenceExpression(ModuleSymbol modul
new JTokenExpression("outputs"));
}

if (moduleSymbol.DeclaringModule.Value is IfConditionSyntax)
if (moduleSymbol.DeclaringModule.HasCondition())
{
return AppendProperties(
CreateFunction(
Expand Down Expand Up @@ -698,7 +698,12 @@ public FunctionExpression GetReferenceExpression(ResourceMetadata resource, Synt
new JTokenExpression("full"));
}

if (resource.IsExistingResource && !context.Settings.EnableSymbolicNames)
var shouldIncludeApiVersion =
!context.Settings.EnableSymbolicNames &&
(resource.IsExistingResource ||
(resource is DeclaredResourceMetadata { Symbol.DeclaringResource: var declaringResource } && declaringResource.HasCondition()));

if (shouldIncludeApiVersion)
{
var apiVersion = resource.TypeReference.ApiVersion ?? throw new InvalidOperationException($"Expected resource type {resource.TypeReference.FormatName()} to contain version");

Expand Down
2 changes: 2 additions & 0 deletions src/Bicep.Core/Syntax/ModuleDeclarationSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@ public ModuleDeclarationSyntax(IEnumerable<SyntaxBase> leadingNodes, Token keywo

public ObjectSyntax GetBody() =>
this.TryGetBody() ?? throw new InvalidOperationException($"A valid module body is not available on this module due to errors. Use {nameof(TryGetBody)}() instead.");

public bool HasCondition() => this.Value is IfConditionSyntax or ForSyntax { Body: IfConditionSyntax };
}
}
2 changes: 2 additions & 0 deletions src/Bicep.Core/Syntax/ResourceDeclarationSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ public ResourceDeclarationSyntax(IEnumerable<SyntaxBase> leadingNodes, Token key

public ObjectSyntax GetBody() =>
this.TryGetBody() ?? throw new InvalidOperationException($"A valid resource body is not available on this module due to errors. Use {nameof(TryGetBody)}() instead.");

public bool HasCondition() => this.Value is IfConditionSyntax or ForSyntax { Body: IfConditionSyntax };
}
}