Skip to content

Commit

Permalink
Only add an explicit dependency on an existing resource when the depl…
Browse files Browse the repository at this point in the history
…oyments engine will use the GET response (#15693)

Resolves #13674 
Resolves #15686 

This PR reapplies the change from #15447 now that the bug in indexing
expression traversal is fixed.
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/15693)
  • Loading branch information
jeskew authored Dec 5, 2024
1 parent e29cbf1 commit b326faa
Show file tree
Hide file tree
Showing 34 changed files with 217 additions and 260 deletions.
22 changes: 11 additions & 11 deletions src/Bicep.Core.IntegrationTests/Emit/DependencyInferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void Implicit_dependencies_on_existing_resource_identifying_properties_ar
""");

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["existingSa"]""");
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["deployedSa"]""");
result.Template.Should().HaveJsonAtPath("$.resources.existingSa.dependsOn", """["deployedSa"]""");
}

Expand Down Expand Up @@ -279,7 +279,7 @@ public void Implicit_dependencies_on_existing_resource_collection_identifying_pr
""");

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["[format('existingSa[{0}]', 0)]"]""");
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["deployedSa"]""");
result.Template.Should().HaveJsonAtPath("$.resources.existingSa.dependsOn", """["deployedSa"]""");
}

Expand Down Expand Up @@ -316,7 +316,7 @@ public void Existing_resource_function_calls_are_expressed_as_direct_dependencie
""");

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["existingSa"]""");
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["deployedSa"]""");
result.Template.Should().HaveJsonAtPath("$.resources.existingSa.dependsOn", """["deployedSa"]""");
}

Expand Down Expand Up @@ -353,7 +353,7 @@ public void Existing_resource_collection_function_calls_are_expressed_as_direct_
""");

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["[format('existingSa[{0}]', 0)]"]""");
result.Template.Should().HaveJsonAtPath("$.resources.newSa.dependsOn", """["deployedSa"]""");
result.Template.Should().HaveJsonAtPath("$.resources.existingSa.dependsOn", """["deployedSa"]""");
}

Expand Down Expand Up @@ -394,7 +394,7 @@ public void Using_an_existing_resource_as_an_explicit_parent_does_not_generate_a
""");

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveJsonAtPath("$.resources.sa.dependsOn", """["subnet"]""");
result.Template.Should().NotHaveValueAtPath("$.resources.sa.dependsOn");
}

[DataTestMethod]
Expand Down Expand Up @@ -436,7 +436,7 @@ public void Non_looped_resource_depending_on_looped_existing_resource_should_dep

if (useSymbolicNameCodegen)
{
result.Template.Should().HaveJsonAtPath("$.resources.vault.dependsOn", """["subnets"]""");
result.Template.Should().HaveJsonAtPath("$.resources.vault.dependsOn", """["vnets"]""");
}
else
{
Expand Down Expand Up @@ -485,7 +485,7 @@ public void Looped_resource_depending_on_looped_existing_resource_should_depend_
{
result.Template.Should().HaveJsonAtPath(
"$.resources.vault.dependsOn",
"""["[format('subnets[{0}]', sub(range(10, 10)[copyIndex()], 10))]"]""");
"""["[format('vnets[{0}]', mod(range(0, 10)[sub(range(10, 10)[copyIndex()], 10)], 2))]"]""");
}
else
{
Expand Down Expand Up @@ -538,7 +538,7 @@ public void Looped_resource_depending_on_looped_variable_should_depend_on_transi
{
result.Template.Should().HaveJsonAtPath(
"$.resources.vault.dependsOn",
"""["[format('subnets[{0}]', sub(range(20, 10)[sub(range(10, 10)[copyIndex()], 10)], 20))]"]""");
"""["[format('vnets[{0}]', mod(range(0, 10)[sub(range(20, 10)[sub(range(10, 10)[copyIndex()], 10)], 20)], 2))]"]""");
}
else
{
Expand Down Expand Up @@ -587,7 +587,7 @@ public void CopyIndex_only_appears_in_compiled_expression_if_all_links_in_chain_

if (useSymbolicNameCodegen)
{
result.Template.Should().HaveJsonAtPath("$.resources.vault.dependsOn", """["[format('subnets[{0}]', sub(range(10, 10)[copyIndex()], 10))]"]""");
result.Template.Should().HaveJsonAtPath("$.resources.vault.dependsOn", """["[format('vnets[{0}]', 0)]"]""");
}
else
{
Expand Down Expand Up @@ -636,7 +636,7 @@ public void CopyIndex_only_appears_in_compiled_expression_if_all_links_in_chain_

if (useSymbolicNameCodegen)
{
result.Template.Should().HaveJsonAtPath("$.resources.vault.dependsOn", """["[format('subnets[{0}]', 0)]"]""");
result.Template.Should().HaveJsonAtPath("$.resources.vault.dependsOn", """["[format('vnets[{0}]', mod(range(0, 10)[0], 2))]"]""");
}
else
{
Expand Down Expand Up @@ -699,6 +699,6 @@ public void Using_an_existing_resource_as_a_scope_does_not_generate_an_explicit_
("empty.bicep", string.Empty));

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveJsonAtPath("$.resources.empty.dependsOn", """["rg"]""");
result.Template.Should().NotHaveValueAtPath("$.resources.empty.dependsOn");
}
}
7 changes: 3 additions & 4 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5066,7 +5066,7 @@ public void Test_Issue10343()
"));

var evaluated = TemplateEvaluator.Evaluate(result.Template).ToJToken();
evaluated.Should().HaveValueAtPath("resources.foo3.dependsOn", new JArray("foo2"));
evaluated.Should().HaveValueAtPath("resources.foo3.dependsOn", new JArray("foo1"));
}

// https://github.com/Azure/bicep/issues/11292
Expand Down Expand Up @@ -6316,7 +6316,7 @@ public void Test_Issue13596(bool enableSymbolicNameCodegen)

if (enableSymbolicNameCodegen)
{
result.Template.Should().HaveJsonAtPath("$.resources.secret.dependsOn", """["sa"]""");
result.Template.Should().HaveJsonAtPath("$.resources.secret.dependsOn", """["mod"]""");
}
else
{
Expand Down Expand Up @@ -6478,7 +6478,7 @@ extension microsoftGraph
}

[TestMethod]
public void Test_Issue15686_repro()
public void Test_Issue15686()
{
var result = CompilationHelper.Compile("""
param eventSubscriptionName string
Expand Down Expand Up @@ -6525,7 +6525,6 @@ param functionName string
result.Template.Should().NotBeNull();
result.Template.Should().HaveJsonAtPath("$.resources.eventSubscription.dependsOn", """
[
"eventGridTopic",
"functionApp"
]
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "12632249060210513106"
"templateHash": "14846102413726496336"
}
},
"parameters": {
Expand Down Expand Up @@ -1851,10 +1851,7 @@
}
}
}
},
"dependsOn": [
"kv"
]
}
},
"secureModule2": {
"type": "Microsoft.Resources/deployments",
Expand Down Expand Up @@ -1912,10 +1909,7 @@
}
}
}
},
"dependsOn": [
"scopedKv"
]
}
},
"secureModuleLooped": {
"copy": {
Expand Down Expand Up @@ -1977,11 +1971,7 @@
}
}
}
},
"dependsOn": [
"[format('loopedKv[{0}]', copyIndex())]",
"[format('loopedKv[{0}]', copyIndex())]"
]
}
},
"secureModuleCondition": {
"type": "Microsoft.Resources/deployments",
Expand Down Expand Up @@ -2024,10 +2014,7 @@
}
}
}
},
"dependsOn": [
"kv"
]
}
},
"withSpace": {
"type": "Microsoft.Resources/deployments",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "9375757315581642416"
"templateHash": "15846782372928243436"
}
},
"parameters": {
Expand Down Expand Up @@ -82,10 +82,7 @@
"existing": true,
"type": "My.Rp/parentType/childType",
"apiVersion": "2020-12-01",
"name": "[format('{0}/{1}', 'existingParent', 'existingChild')]",
"dependsOn": [
"existingParent"
]
"name": "[format('{0}/{1}', 'existingParent', 'existingChild')]"
},
"conditionParent::conditionChild::conditionGrandchild": {
"condition": "[and(and(parameters('createParent'), parameters('createChild')), parameters('createGrandchild'))]",
Expand Down Expand Up @@ -162,4 +159,4 @@
"value": "loopChild"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "12953345672979673276"
"templateHash": "5924434758202725836"
}
},
"parameters": {
Expand Down Expand Up @@ -407,7 +407,7 @@
"scope": "[extensionResourceId(extensionResourceId(format('Microsoft.Compute/virtualMachines/{0}', 'vmName'), 'My.Rp/extensionResource', 'extension'), 'Mock.Rp/existingExtensionResource', 'existing1')]",
"name": "existing2",
"dependsOn": [
"existing1"
"extension1"
]
},
"extension3": {
Expand All @@ -416,7 +416,7 @@
"scope": "[extensionResourceId(extensionResourceId(format('Microsoft.Compute/virtualMachines/{0}', 'vmName'), 'My.Rp/extensionResource', 'extension'), 'Mock.Rp/existingExtensionResource', 'existing1')]",
"name": "extension3",
"dependsOn": [
"existing1"
"extension1"
]
},
"storageResources": {
Expand Down Expand Up @@ -665,10 +665,7 @@
"p3_child1": {
"type": "Microsoft.Rp1/resource1/child1",
"apiVersion": "2020-06-01",
"name": "[format('{0}/{1}', 'res1', 'child1')]",
"dependsOn": [
"p3_res1"
]
"name": "[format('{0}/{1}', 'res1', 'child1')]"
},
"p4_res1": {
"existing": true,
Expand All @@ -682,10 +679,7 @@
"type": "Microsoft.Rp1/resource1/child1",
"apiVersion": "2020-06-01",
"scope": "/",
"name": "[format('{0}/{1}', 'res1', 'child1')]",
"dependsOn": [
"p4_res1"
]
"name": "[format('{0}/{1}', 'res1', 'child1')]"
},
"sqlServer": {
"type": "Microsoft.Sql/servers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "11528852364160566097"
"templateHash": "14248540637091011874"
}
},
"parameters": {
Expand Down Expand Up @@ -56,10 +56,7 @@
"secret": "[variables('apimNameValueSet')[copyIndex()].isSecret]",
"value": "[variables('apimNameValueSet')[copyIndex()].value]",
"tags": "[variables('apimNameValueSet')[copyIndex()].tags]"
},
"dependsOn": [
"parentAPIM"
]
}
}
},
"outputs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "403029788224868668"
"templateHash": "6816770503491912011"
}
},
"parameters": {
Expand Down Expand Up @@ -47,10 +47,7 @@
"properties": {
"displayName": "[variables('groupsSet')[copyIndex()].groupDisplayName]",
"description": "[variables('groupsSet')[copyIndex()].groupDescription]"
},
"dependsOn": [
"parentAPIM"
]
}
}
},
"outputs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "13726378283629199613"
"templateHash": "2208926443863165998"
}
},
"parameters": {
Expand Down Expand Up @@ -163,7 +163,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "403029788224868668"
"templateHash": "6816770503491912011"
}
},
"parameters": {
Expand Down Expand Up @@ -204,10 +204,7 @@
"properties": {
"displayName": "[variables('groupsSet')[copyIndex()].groupDisplayName]",
"description": "[variables('groupsSet')[copyIndex()].groupDescription]"
},
"dependsOn": [
"parentAPIM"
]
}
}
},
"outputs": {
Expand Down Expand Up @@ -251,7 +248,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "8034218730798252475"
"templateHash": "16793550498477002119"
}
},
"parameters": {
Expand Down Expand Up @@ -301,10 +298,7 @@
"email": "[variables('usersSet')[copyIndex()].email]",
"state": "[variables('usersSet')[copyIndex()].state]",
"note": "[variables('usersSet')[copyIndex()].notes]"
},
"dependsOn": [
"parentAPIM"
]
}
}
}
}
Expand Down Expand Up @@ -336,7 +330,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "11528852364160566097"
"templateHash": "14248540637091011874"
}
},
"parameters": {
Expand Down Expand Up @@ -386,10 +380,7 @@
"secret": "[variables('apimNameValueSet')[copyIndex()].isSecret]",
"value": "[variables('apimNameValueSet')[copyIndex()].value]",
"tags": "[variables('apimNameValueSet')[copyIndex()].tags]"
},
"dependsOn": [
"parentAPIM"
]
}
}
},
"outputs": {
Expand Down
Loading

0 comments on commit b326faa

Please sign in to comment.