将属性传递给对象时,ARM部署失败

时间:2018-12-11 11:09:47

标签: azure properties arm-template

使用资源迭代部署ARM模板时,我想将资源属性作为对象传递。

这样做将允许复制数组的每个元素内存在不同的参数集。这是因为某些属性可能需要根据其他属性的值而有条件地包含或排除。例如,对于API Management产品,文档针对subscriptionsLimit属性声明以下内容-

  

仅当subscriptionRequired属性存在且值为false时才可以出现。

但是,当在下面部署示例模板时,部署会在Azure中挂起。查看相关事件,我可以看到部署资源的操作失败,并出现内部服务器错误(500),但是没有其他详细信息。

如果我使用properties引用variables('productsJArray')[copyIndex()].whatever对象中的每个参数,则部署成功。但是,这是不希望的,因为这意味着每个properties对象都必须包含相同的参数,这并不总是允许的,并且可能导致部署失败。

示例模板

请注意,我已经输出variables('productsJArray')[copyIndex()],它是一个有效的对象。我什至将输出复制到模板中并成功部署。

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "apiManagementServiceName": {
            "type": "string",
            "metadata": {
                "description": "The name of the API Management instance."
            }
        },
        "productsJson": {
            "type": "string",
            "metadata": {
                "description": "A JSON representation of the Products to add."
            }
        }
    },
    "variables": {
        "productsJArray": "[json(parameters('productsJson'))]"
    },
    "resources": [
        {
            "condition": "[greater(length(variables('productsJArray')), 0)]",
            "type": "Microsoft.ApiManagement/service/products",
            "name": "[concat(parameters('apiManagementServiceName'), '/', variables('productsJArray')[copyIndex()].name)]",
            "apiVersion": "2018-06-01-preview",
            "properties": "[variables('productsJArray')[copyIndex()]]",
            "copy": {
                "name": "productscopy",
                "count": "[if(greater(length(variables('productsJArray')), 0), length(variables('productsJArray')), 1)]"
            }
        }
    ]
}

示例参数

{
        "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
                "apiManagementServiceName": {
                        "value": "my-api-management"
                },
                "productsJson": {
                        "value": "[{\"name\":\"my-product\",\"displayName\":\"My Product\",\"description\":\"My product is awesome.\",\"state\":\"published\",\"subscriptionRequired\":true,\"approvalRequired\":false}]"
                }
        }
}

变量'productsJArray [0]'的输出

"outputs": {
        "properties": {
                "type": "Object",
                "value": {
                        "approvalRequired": false,
                        "description": "My product is awesome.",
                        "displayName": "My Product",
                        "name": "my-product",
                        "state": "published",
                        "subscriptionRequired": true
                }
        }
}

1 个答案:

答案 0 :(得分:0)

这里的问题是在设置资源属性时,我传递了name参数以及其他参数。显然这是错误的,但是如果MS以更人性化的方式处理错误(猜测他们无法考虑所有问题),那将有所帮助。

我已经更新了传入的productsJson参数-

[{\"name\":\"cs-automation\",\"properties\":{\"displayName\":\"CS Automation Subscription\",\"state\":\"published\",\"description\":\"Allows access to the ConveyorBot v1 API.\",\"subscriptionRequired\":true,\"approvalRequired\":false}}]

我现在仅传递必需的“属性”-

"resources": [
    {
        "type": "Microsoft.ApiManagement/service/products",
        "name": "[concat(parameters('apiManagementServiceName'), '/', variables('productsJArray')[copyIndex()].name)]",
        "apiVersion": "2018-06-01-preview",
        "properties": "[variables('productsJArray')[copyIndex()].properties]"
    }
]