我们正在尝试使用参数对象创建模板,因此可以选择在不同资源中具有多个值,即部署一个事件中心命名空间,该命名空间可能有多个授权规则和 eventthub,但参数中的另一个对象用于第二个事件中心命名空间可能只有一个。
模板如下:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"eventhubs": {
"type": "object",
"metadata": {
"description": "JSON object that describes the deployment. see example parameters file"
}
}
},
"variables": {
"resourceNamePrefix": "[substring(resourceGroup().name, 0, 8)]",
"datacenterCode": "[substring(resourceGroup().name, 0, 3)]",
"productCode": "[substring(resourceGroup().name, 3, 3)]",
"environmentLevel": "[substring(resourceGroup().name, 6, 2)]"
},
"resources": [
{
"type": "Microsoft.EventHub/namespaces",
"name": "[concat(variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].name)]",
"apiVersion": "2015-08-01",
"location": "[resourceGroup().location]",
"sku": {
"name": "[concat(variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].sku.name)]",
"tier": "[parameters('eventhubs').instances[copyIndex()].sku.tier]",
"capacity": "[parameters('eventhubs').instances[copyIndex()].sku.capacity]"
},
"copy": {
"name": "eventHubCopy",
"count": "[length(parameters('eventhubs').instances)]"
},
"properties": {
"serviceBusEndpoint": "[concat('https://',variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].name,'.servicebus.windows.net:443/')]",
"enabled": "[parameters('eventhubs').instances[copyIndex()].properties.enabled]"
},
"resources": [
*** PARAMETER OBJECT ***
]
"dependsOn": []
}
],
"outputs": {}
}
和参数文件:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"eventhubs": {
"value": {
"instances": [
{
"name": "EVT001",
"sku": {
"name": "Standard",
"tier": "Standard",
"capacity": 4
},
"scale": null,
"properties": {
"enabled": "true"
},
"resources": [
{
"type": "AuthorizationRules",
"name": "SendKey",
"apiVersion": "2015-08-01",
"properties": {
"rights": [
"Send"
]
}
},
{
"type": "AuthorizationRules",
"name": "ListenKey",
"apiVersion": "2015-08-01",
"properties": {
"rights": [
"Listen"
]
}
},
{
"type": "EventHub",
"name": "TestHub",
"apiVersion": "2015-08-01",
"properties": {
"messageRetentionInDays": 7,
"status": "Active",
"partitionCount": 4
}
}
]
},
{
"name": "EVT002",
"sku": {
"name": "Standard",
"tier": "Standard",
"capacity": 4
},
"scale": null,
"properties": {
"enabled": "true"
},
"resources": [
{
"type": "AuthorizationRules",
"name": "SendKey",
"apiVersion": "2015-08-01",
"properties": {
"rights": [
"Send"
]
}
},
{
"type": "EventHub",
"name": "TestHub",
"apiVersion": "2015-08-01",
"properties": {
"messageRetentionInDays": 7,
"status": "Active",
"partitionCount": 4
}
},
{
"type": "EventHub",
"name": "SecondHub",
"apiVersion": "2015-08-01",
"properties": {
"messageRetentionInDays": 7,
"status": "Active",
"partitionCount": 4
}
}
]
}
]
}
}
}
}
我正在尝试做的是将参数文件中资源数组的内容移动到模板文件中的嵌套资源数组中。将数组移动到对象时这是可能的,但是我在将数组移动到数组时遇到以下问题:
"resources": "[parameters('eventhubs').instances[copyIndex()].properties]", <--- value must be of type array
"resources": [ { "[parameters('eventhubs').instances[copyIndex()].properties]" } ], <--- expecting a name and value as it's in an object
"resources": [ "[parameters('eventhubs').instances[copyIndex()].properties]" ], <--- value must be of the following types: object
在参数文件的数组中的对象周围添加另一组方括号也无济于事。
使用 createArray 函数时出现同样的错误。
我的解决方法是
"resources": [
{
"type": "AuthorizationRules",
"name": "[parameters('eventhubs').instances[copyIndex()].resources[0].name]",
"apiversion": "[parameters('eventhubs').instances[copyIndex()].resources[0].apiversion]",
"properties": "[parameters('eventhubs').instances[copyIndex()].resources[0].properties]",
"dependsOn": [ "[concat(variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].name)]" ]
}
],
但是 type 属性不能是表达式,因此不适用于我们的模板被使用和使用的方式。
有可能做我正在尝试的事情吗?