diff --git a/.devcontainer/Makefile b/.devcontainer/Makefile index 61bab4c..407671e 100644 --- a/.devcontainer/Makefile +++ b/.devcontainer/Makefile @@ -1,6 +1,6 @@ IMAGE_NAME ?= takekazuomi/devcontainers-bicep -TAG ?= 0.3.1 -BICEP_RELEASE = v0.3.1 +TAG ?= 0.3.126 +BICEP_RELEASE = v0.3.126 help: ## Show this help. @sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST) diff --git a/README.md b/README.md index e947787..0192b6a 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ This definition requires an Azure subscription to use. You can create a [free ac ## ChangeLog +- v0.3.126 Improvements to child resource declarations, Loop enhancements - v0.3.1 bicep v0.3.1 loop support - v0.0.9 bicep v0.2.212 conditional resources support - v0.0.7 install GNU make diff --git a/src/loop-index.bicep b/src/loop-index.bicep index 9ce9787..42cb460 100644 --- a/src/loop-index.bicep +++ b/src/loop-index.bicep @@ -12,7 +12,7 @@ var storageConfigurations = [ ] resource storageAccountResources 'Microsoft.Storage/storageAccounts@2019-06-01' = [for (config, i) in storageConfigurations: { - name: storageAccountNamePrefix + config.suffix + i + name: 'storageAccountNamePrefix${config.suffix}${i}' location: resourceGroup().location properties: { supportsHttpsTrafficOnly: true diff --git a/src/nested-loops.bicep b/src/nested-loops.bicep index ffd3dbd..da0ad97 100644 --- a/src/nested-loops.bicep +++ b/src/nested-loops.bicep @@ -1,9 +1,22 @@ -resource parentResources 'Microsoft.Example/examples@2020-06-06' = [for parent in parents where parent.enabled: { +var parents = [ + { + name: 'name' + enabled: true + children: [ + { + name: 'name' + settingValue: 'value' + } + ] + } +] + +resource parentResources 'Microsoft.Example/examples@2020-06-06' = [for parent in parents: if (parent.enabled) { name: parent.name properties: { - children: [for child in parent.children where parent.includeChildren && child.enabled: { + children: [for child in parent.children: { name: child.name setting: child.settingValue }] } -}] \ No newline at end of file +}] diff --git a/src/nsg-module.bicep b/src/nsg-module.bicep new file mode 100644 index 0000000..e53a625 --- /dev/null +++ b/src/nsg-module.bicep @@ -0,0 +1,11 @@ +param name string + +resource nsg 'Microsoft.Network/networkSecurityGroups@2020-06-01' = { + name: name + location: resourceGroup().location +} + +output n object = { + name: nsg.name + id: nsg.id +} diff --git a/src/output-loop-module.bicep b/src/output-loop-module.bicep new file mode 100644 index 0000000..68a148b --- /dev/null +++ b/src/output-loop-module.bicep @@ -0,0 +1,20 @@ +// Output loops. +// Directly referencing a resource module or module collection is not currently supported in output loops. In order to loop outputs we need to apply an array indexer to the expression. + +var nsgNames = [ + 'nsg1' + 'nsg2' + 'nsg3' +] + +output nsgs array = [for i in range(0, length(nsgNames)): { + name: nsg[i].outputs.n.name + resourceId: nsg[i].outputs.n.id +}] + +module nsg 'nsg-module.bicep' = [for name in nsgNames: { + name: name + params:{ + name: name + } +}] diff --git a/src/output-loop-module.json b/src/output-loop-module.json new file mode 100644 index 0000000..61393ef --- /dev/null +++ b/src/output-loop-module.json @@ -0,0 +1,80 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "functions": [], + "variables": { + "nsgNames": [ + "nsg1", + "nsg2", + "nsg3" + ] + }, + "resources": [ + { + "copy": { + "name": "nsg", + "count": "[length(variables('nsgNames'))]" + }, + "type": "Microsoft.Resources/deployments", + "apiVersion": "2019-10-01", + "name": "[variables('nsgNames')[copyIndex()]]", + "properties": { + "expressionEvaluationOptions": { + "scope": "inner" + }, + "mode": "Incremental", + "parameters": { + "name": { + "value": "[variables('nsgNames')[copyIndex()]]" + } + }, + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "name": { + "type": "string" + } + }, + "functions": [], + "resources": [ + { + "type": "Microsoft.Network/networkSecurityGroups", + "apiVersion": "2020-06-01", + "name": "[parameters('name')]", + "location": "[resourceGroup().location]" + } + ], + "outputs": { + "n": { + "type": "object", + "value": { + "name": "[parameters('name')]", + "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('name'))]" + } + } + } + } + } + } + ], + "outputs": { + "nsgs": { + "type": "array", + "copy": { + "count": "[length(range(0, length(variables('nsgNames'))))]", + "input": { + "name": "[reference(resourceId('Microsoft.Resources/deployments', variables('nsgNames')[range(0, length(variables('nsgNames')))[copyIndex()]]), '2019-10-01').outputs.name]", + "resourceId": "[reference(resourceId('Microsoft.Resources/deployments', variables('nsgNames')[range(0, length(variables('nsgNames')))[copyIndex()]]), '2019-10-01').outputs.n.value]" + } + } + } + }, + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.3.1.62928", + "templateHash": "11043703718792320070" + } + } +} \ No newline at end of file diff --git a/src/output-loop.bicep b/src/output-loop.bicep new file mode 100644 index 0000000..39a5fdd --- /dev/null +++ b/src/output-loop.bicep @@ -0,0 +1,19 @@ +// Output loops. +// Directly referencing a resource module or module collection is not currently supported in output loops. In order to loop outputs we need to apply an array indexer to the expression. + +var nsgNames = [ + 'nsg1' + 'nsg2' + 'nsg3' +] + +resource nsg 'Microsoft.Network/networkSecurityGroups@2020-06-01' = [for name in nsgNames: { + name: name + location: resourceGroup().location +}] + +output nsgs array = [for i in range(0, length(nsgNames)): { + name: nsg[i].name + + resourceId: nsg[i].id +}] diff --git a/src/output-loop2.bicep b/src/output-loop2.bicep new file mode 100644 index 0000000..4ac0eeb --- /dev/null +++ b/src/output-loop2.bicep @@ -0,0 +1,18 @@ +// Output loops. +// Directly referencing a resource module or module collection is not currently supported in output loops. In order to loop outputs we need to apply an array indexer to the expression. + +var nsgNames = [ + 'nsg1' + 'nsg2' + 'nsg3' +] + +resource nsg 'Microsoft.Network/networkSecurityGroups@2020-06-01' = [for name in nsgNames: { + name: name + location: resourceGroup().location +}] + +output nsgs array = [for i in range(0, length(nsgNames)): { + name: nsg[i].name + resourceId: nsg[i].id +}] diff --git a/src/parent-child-resource.bicep b/src/parent-child-resource.bicep new file mode 100644 index 0000000..6a12659 --- /dev/null +++ b/src/parent-child-resource.bicep @@ -0,0 +1,11 @@ +resource myParent 'My.Rp/parentType@2020-01-01' = { + name: 'myParent' + location: 'West US' +} + +resource myChild 'My.Rp/parentType/childType@2020-01-01' = { + parent: myParent // pass parent reference + name: 'myChild' // don't require the full name to be formatted with '/' characters +} + +output childProp string = myChild.properties.someProp diff --git a/src/resource-nesting.bicep b/src/resource-nesting.bicep new file mode 100644 index 0000000..7034ec1 --- /dev/null +++ b/src/resource-nesting.bicep @@ -0,0 +1,18 @@ +// https://github.com/Azure/bicep/blob/main/docs/spec/resources.md#resource-nesting +resource myParent 'My.Rp/parentType@2020-01-01' = { + name: 'myParent' + location: 'West US' + + // declares a resource of type 'My.Rp/parentType/childType@2020-01-01' + resource myChild 'childType' = { + name: 'myChild' + properties: { + displayName: 'child in ${parent.location}' + } + } +} + +output childProp string = myParent::myChild.properties.displayName + + +// Error BCP082: The name "parent" does not exist in the current context. Did you mean "myParent"? diff --git a/src/resource-nesting.json b/src/resource-nesting.json new file mode 100644 index 0000000..eb3a1c1 --- /dev/null +++ b/src/resource-nesting.json @@ -0,0 +1,37 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "functions": [], + "resources": [ + { + "type": "My.Rp/parentType/childType", + "apiVersion": "2020-01-01", + "name": "[format('{0}/{1}', 'myParent', 'myChild')]", + "properties": { + "displayName": "[format('child in {0}', reference(resourceId('My.Rp/parentType', 'myParent'), '2020-01-01', 'full').location)]" + }, + "dependsOn": [ + "[resourceId('My.Rp/parentType', 'myParent')]" + ] + }, + { + "type": "My.Rp/parentType", + "apiVersion": "2020-01-01", + "name": "myParent", + "location": "West US" + } + ], + "outputs": { + "childProp": { + "type": "string", + "value": "[reference(resourceId('My.Rp/parentType/childType', 'myParent', 'myChild')).displayName]" + } + }, + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.3.126.58533", + "templateHash": "13471392052954043995" + } + } +} \ No newline at end of file diff --git a/src/variable-loop.bicep b/src/variable-loop.bicep new file mode 100644 index 0000000..2ee54ac --- /dev/null +++ b/src/variable-loop.bicep @@ -0,0 +1,21 @@ +var array1 = [ + 1 + 2 + 3 + 4 + 5 +] + +var array2 = [ + 1 + 2 + 3 + 4 + 5 +] + +var array3 = [for i in array1: { +i +}] + +output r array = array3