遍历不存在的Azure资源管理器(ARM)对象属性?

时间:2019-01-20 14:14:23

标签: azure azure-resource-manager

我的对象的属性数量不相等(并且希望保持这种状态),即第二个对象缺少属性“ 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",
}]}

现在,我不知道如何遍历对象时检查属性是否存在。由于缺少属性“ id”,下面给出错误信息:“ [[resourceID('Microsoft.Network/routeTables',parameters('subnets')[copyIndex('subnets']]。routeTable)]”

我对嵌套“ id”属性的条件似乎无效,即

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

1 个答案:

答案 0 :(得分:1)

好的,这是我能想到的最好的方法

{
    "$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')]))]"
                        }
                    }
                ]
            }
        }
    ]
}

您可能可以通过一些嵌套循环使它更好。但这也可以。

PS。我在动态创建名称时曾为nsg \ udr使用不同的名称,在您的方案中,如果存在这些名称,它将使用相同的名称(这不会)。