Talang_madave
Talang_madave

Reputation: 17

In Bicep what is replacement for copyIndex() in json : json to bicep converter not working properly

"name": "[concat('storage', copyIndex())]",

Creates these names: storage0 storage1 storage2

what to do in case to achieve the same in bicep. Json to bicep converter not producing the result correctly.

Here is use case :: var : "firewallPublicIPNamePrefix": "[concat(parameters('firewallName'), 'PublicIP')]",

param : "numberOfFirewallPublicIPAddresses": { "type": "int", "defaultValue": 1, "minValue": 1, "maxValue": 100, "metadata": { "description": "Number of public IP addresses for the Azure Firewall" } }, "firewallName": { "type": "string", "defaultValue": "AzFirewall", "metadata": { "description": "The name of the Azure Firewall." } }

changes :

  {
    "condition": "[parameters('createDnatRuleCollection')]",
    "comments": "Azure Firewall Policy NAT Rule Collection",
    "name": "[variables('firewallPolicyDefaultDnatRuleCollectionGroupName')]",
    "type": "Microsoft.Network/firewallPolicies/ruleCollectionGroups",
    "apiVersion": "2020-07-01",
    "dependsOn": [
      "[variables('firewallPolicyId')]",
      "[variables('firewallId')]",
      "[variables('firewallPolicyDefaultNetworkRuleCollectionGroupId')]",
      "[variables('Vm1NicId')]",
      "[variables('Vm2NicId')]"
    ],
    "properties": {
      "priority": "100",
      "ruleCollections": [
        {
          "name": "VirtualMachineNatRules",
          "ruleCollectionType": "FirewallPolicyNatRuleCollection",
          "priority": "300",
          "action": {
            "type": "Dnat"
          },
          "rules": [
            {
              "name": "[concat(parameters('Vm1Name'), '-Rdp-Nat-Rule')]",
              "ruleType": "NatRule",
              "sourceAddresses": [
                "*"
              ],
              "destinationAddresses": [
                "[reference(resourceId('Microsoft.Network/publicIPAddresses', if(equals(parameters('numberOfFirewallPublicIPAddresses'), 1), variables('firewallPublicIPNamePrefix'), concat(variables('firewallPublicIPNamePrefix'), add(copyIndex(), 1))))).ipAddress]"
              ],
              "destinationPorts": [
                "4001"
              ],
              "ipProtocols": [
                "TCP"
              ],
              "translatedAddress": "[reference(variables('Vm1NicId')).ipConfigurations[0].properties.privateIPAddress]",
              "translatedPort": "22"
            },
            {
              "name": "[concat(parameters('Vm2Name'), '-Rdp-Nat-Rule')]",
              "ruleType": "NatRule",
              "sourceAddresses": [
                "*"
              ],
              "destinationAddresses": [
                "[reference(resourceId('Microsoft.Network/publicIPAddresses', if(equals(parameters('numberOfFirewallPublicIPAddresses'), 1), variables('firewallPublicIPNamePrefix'), concat(variables('firewallPublicIPNamePrefix'), add(copyIndex(), 1))))).ipAddress]"
              ],
              "destinationPorts": [
                "4002"
              ],
              "ipProtocols": [
                "TCP"
              ],
              "translatedAddress": "[reference(variables('Vm2NicId')).ipConfigurations[0].properties.privateIPAddress]",
              "translatedPort": "22"
            }
          ]
        }
      ]
    }
  }

how to convert this "destinationAddresses" part to bicep?

Upvotes: 0

Views: 193

Answers (1)

GordonBy
GordonBy

Reputation: 3407

To achieve your example in bicep, you'd do this;

resource storageAcct 'Microsoft.Storage/storageAccounts@2022-09-01' = [for i in range(0, 2): {
  name: 'storage${i}'
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'Storage'
}]

ref: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/loops

If the bicep decompile from json isn't working properly for your Arm template, then consider raising an issue on the bicep project. https://github.com/Azure/bicep/issues

For more complex scenarios (I notice you've added IP Addresses to the question), i'd look at the Bicep support in 0.17 for some IP CIDR capabilities I think the cidrHost('10.144.3.0/24', i) function would simplify your current logic.

Upvotes: 0

Related Questions