遍历不存在的Azure资源管理器(ARM)对象属性? [英] Iterate over not existing Azure resource manager (ARM) object properties?

查看:88
本文介绍了遍历不存在的Azure资源管理器(ARM)对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的对象的属性数量不相等(并且希望保持这种状态),即第二个对象缺少属性"routeTable"

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",
}]}

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

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)]"

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

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

推荐答案

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

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.我在动态创建名称时曾为nsg \ udr使用了不同的名称,在您的方案中,如果存在这些名称,它将使用相同的名称(这不会).

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).

这篇关于遍历不存在的Azure资源管理器(ARM)对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆