Iterate over not existing Azure resource manager (

2019-08-19 08:57发布

I have object with not equal number of properties (and would like to keep it like this), i.e. second object is missing property "routeTable"

"subnets": {
"value":[
{
"name": "GatewaySubnet",
"addressPrefix": "10.2.0.0/24",
"networkSecurityGroup":"NSG-AllowAll",
"routeTable":"UDR-Default"
},
{
"name":"UnTrusted",
"addressPrefix":"10.2.1.0/24",
"networkSecurityGroup":"NSG-AllowAll",
}]}

Now I don't know how to check if property exists when iterating over object. The below gives error because of missing property "id": "[resourceID('Microsoft.Network/routeTables', parameters('subnets')[copyIndex('subnets')].routeTable)]"

My conditions for nested "id" property do not seem to work i.e.

"networkSecurityGroup": {
"id": "[resourceID('Microsoft.Network/networkSecurityGroups', if(equals(parameters('subnets')[copyIndex('subnets')].networkSecurityGroup, ''), json('null'), parameters('subnets')[copyIndex('subnets')].networkSecurityGroup))]"
}

1条回答
孤傲高冷的网名
2楼-- · 2019-08-19 09:44

ok, this is the best I can come up with:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "deploymentPrefix": {
            "type": "string"
        },
        "subnets": {
            "type": "array",
            "defaultValue": [
                {
                    "name": "GatewaySubnet",
                    "addressPrefix": "10.2.0.0/24",
                    "networkSecurityGroup": "NSG-AllowAll",
                    "routeTable": "UDR-Default"
                },
                {
                    "name": "UnTrusted",
                    "addressPrefix": "10.2.1.0/24",
                    "networkSecurityGroup": "NSG-AllowAll1"
                },
                {
                    "name": "routed",
                    "addressPrefix": "10.2.2.0/24",
                    "routeTable": "UDR-Default1"
                }
            ]
        }
    },
    "variables": {
        "copy": [
            {
                "name": "subnetsBase",
                "count": "[length(parameters('subnets'))]",
                "input": {
                    "name": "[concat('subnet-', parameters('subnets')[copyIndex('subnetsBase')].name)]",
                    "properties": {
                        "addressPrefix": "[parameters('subnets')[copyIndex('subnetsBase')].addressPrefix]"
                    }
                }
            },
            {
                "name": "subnetsUDR",
                "count": "[length(parameters('subnets'))]",
                "input": {
                    "routeTable": {
                        "id": "[if(contains(parameters('subnets')[copyIndex('subnetsUDR')], 'routeTable'), resourceId('Microsoft.Network/routeTables', parameters('subnets')[copyIndex('subnetsUDR')].routeTable), 'skip')]"
                    }
                }
            },
            {
                "name": "subnetsNSG",
                "count": "[length(parameters('subnets'))]",
                "input": {
                    "networkSecurityGroup": {
                        "id": "[if(contains(parameters('subnets')[copyIndex('subnetsNSG')], 'networkSecurityGroup'), resourceId('Microsoft.Network/networkSecurityGroups', parameters('subnets')[copyIndex('subnetsNSG')].networkSecurityGroup), 'skip')]"
                    }
                }
            }
        ]
    },
    "resources": [
        {
            "condition": "[not(contains(variables('subnetsNSG')[copyIndex()].networkSecurityGroup.id, 'skip'))]",
            "apiVersion": "2017-06-01",
            "name": "[if(contains(parameters('subnets')[copyIndex()], 'networkSecurityGroup'), parameters('subnets')[copyIndex()].networkSecurityGroup, 'skip')]",
            "location": "[resourceGroup().location]",
            "type": "Microsoft.Network/networkSecurityGroups",
            "copy": {
                "name": "nsg",
                "count": "[length(parameters('subnets'))]"
            },
            "properties": {
                "securityRules": []
            }
        },
        {
            "condition": "[not(contains(variables('subnetsUDR')[copyIndex()].routeTable.id, 'skip'))]",
            "type": "Microsoft.Network/routeTables",
            "name": "[if(contains(parameters('subnets')[copyIndex()], 'routeTable'), parameters('subnets')[copyIndex()].routeTable, 'skip')]",
            "apiVersion": "2017-10-01",
            "location": "[resourceGroup().location]",
            "copy": {
                "name": "udr",
                "count": "[length(parameters('subnets'))]"
            },
            "properties": {
                "routes": []
            }
        },
        {
            "apiVersion": "2017-06-01",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[concat(parameters('deploymentPrefix'), '-vNet')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "nsg",
                "udr"
            ],
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "10.2.0.0/16"
                    ]
                },
                "copy": [
                    {
                        "name": "subnets",
                        "count": "[length(parameters('subnets'))]",
                        "input": {
                            "name": "[concat('subnet-', parameters('subnets')[copyIndex('subnets')].name)]",
                            "properties": "[union(variables('subnetsBase')[copyIndex('subnets')].properties, if(equals(variables('subnetsUDR')[copyIndex('subnets')].routetable.id, 'skip'), variables('subnetsBase')[copyIndex('subnets')].properties, variables('subnetsUDR')[copyIndex('subnets')]), if(equals(variables('subnetsNSG')[copyIndex('subnets')].networkSecurityGroup.id, 'skip'), variables('subnetsBase')[copyIndex('subnets')].properties, variables('subnetsNSG')[copyIndex('subnets')]))]"
                        }
                    }
                ]
            }
        }
    ]
}

you can probably make it better with some nested loops. but this works as well.

PS. I had use different names for nsg\udr as I am creating those dynamically, in your scenario if those exists it will work with identical names (this wont).

查看更多
登录 后发表回答