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

ARM deployment fails when properties are passed an an object

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

这样做将允许在复制数组的每个元素中存在一组不同的参数。这样做的原因是因为某些属性可能需要根据其他属性的值有条件地包含或排除。例如,在 API 管理产品的情况下,文档对 subscriptionsLimit 属性 -

的说明如下

Can be present only if subscriptionRequired property is present and has a value of false.

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

如果我使用 variables('productsJArray')[copyIndex()].whatever 引用 properties 对象中的每个参数,则部署成功。但是,这是不可取的,因为这意味着每个 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
                }
        }
}

这里的问题是我在设置资源属性时传递了包括 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}}]

我现在只传递必需的 'properties' -

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