获取 ARM 模板中对象 属性 名称的数组
Get array of object property names in ARM template
在 ARM 模板中有没有办法获取包含 JSON 对象的 属性 名称的数组?
我在 the documentation 中没有看到任何明显的东西。我看到的最接近的是 length(object)
来获取对象的 属性 计数,但我认为我什至无法使用复制循环来获取 属性 名称。
我要实现的具体方案是将具有附加插槽粘性设置的 Web appsettings
部署到暂存插槽:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"webSiteName": {
"type": "string"
},
"globalAppSettings": {
"type": "object"
},
"slotName": {
"type": "string"
},
"slotAppSettings": {
"type": "object"
},
"slotStickySettings": {
"type": "array",
// but getPropertyNames(object) is not a real function :(
"defaultValue": "[getPropertyNames(parameters('slotAppSettings'))]"
}
},
"resources": [
{
"type": "Microsoft.Web/sites/config",
"name": "[concat(parameters('webSiteName'), '/appsettings')]",
"apiVersion": "2018-02-01",
"properties": "[parameters('globalAppSettings')]"
},
{
"type": "Microsoft.Web/sites/slots/config",
"name": "[concat(parameters('webSiteName'), '/', parameters('slotName'), '/appsettings')]",
"apiVersion": "2018-02-01",
"properties": "[union(parameters('globalAppSettings'), parameters('slotAppSettings'))]"
},
{
"type": "Microsoft.Web/sites/config",
"name": "[concat(parameters('webSiteName'), '/slotconfignames')]",
"apiVersion": "2015-08-01",
"properties": {
"appSettingNames": "[parameters('slotStickySettings')]"
}
}
]
}
没有直接的方法可以做到这一点,因为没有 return 对象属性的函数。我能够通过将对象转换为字符串然后解析它以找到 属性 名称来完成它。只要您的 属性 值中没有逗号,它就应该可以工作。如果需要,您也可以添加一些检查来处理这种情况。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"testSettings": {
"type": "object",
"defaultValue": {
"a": "demo value 1",
"b": "demo value 2"
}
}
},
"variables": {
"delimiters": [","],
"settingArray": "[split(replace(replace(replace(string(parameters('testSettings')), '{', ''), '}', ''), '\"', ''), variables('delimiters'))]",
"propNameArray": {
"copy": [
{
"name": "copyPropertyNames",
"count": "[length(variables('settingArray'))]",
"input": "[substring(variables('settingArray')[copyIndex('copyPropertyNames')], 0, indexOf(variables('settingArray')[copyIndex('copyPropertyNames')], ':'))]"
}
]
}
},
"resources": [],
"outputs": {
"paramOutput": {
"type": "array",
"value": "[variables('propNameArray').copyPropertyNames]"
}
}
}
在 ARM 模板中有没有办法获取包含 JSON 对象的 属性 名称的数组?
我在 the documentation 中没有看到任何明显的东西。我看到的最接近的是 length(object)
来获取对象的 属性 计数,但我认为我什至无法使用复制循环来获取 属性 名称。
我要实现的具体方案是将具有附加插槽粘性设置的 Web appsettings
部署到暂存插槽:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"webSiteName": {
"type": "string"
},
"globalAppSettings": {
"type": "object"
},
"slotName": {
"type": "string"
},
"slotAppSettings": {
"type": "object"
},
"slotStickySettings": {
"type": "array",
// but getPropertyNames(object) is not a real function :(
"defaultValue": "[getPropertyNames(parameters('slotAppSettings'))]"
}
},
"resources": [
{
"type": "Microsoft.Web/sites/config",
"name": "[concat(parameters('webSiteName'), '/appsettings')]",
"apiVersion": "2018-02-01",
"properties": "[parameters('globalAppSettings')]"
},
{
"type": "Microsoft.Web/sites/slots/config",
"name": "[concat(parameters('webSiteName'), '/', parameters('slotName'), '/appsettings')]",
"apiVersion": "2018-02-01",
"properties": "[union(parameters('globalAppSettings'), parameters('slotAppSettings'))]"
},
{
"type": "Microsoft.Web/sites/config",
"name": "[concat(parameters('webSiteName'), '/slotconfignames')]",
"apiVersion": "2015-08-01",
"properties": {
"appSettingNames": "[parameters('slotStickySettings')]"
}
}
]
}
没有直接的方法可以做到这一点,因为没有 return 对象属性的函数。我能够通过将对象转换为字符串然后解析它以找到 属性 名称来完成它。只要您的 属性 值中没有逗号,它就应该可以工作。如果需要,您也可以添加一些检查来处理这种情况。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"testSettings": {
"type": "object",
"defaultValue": {
"a": "demo value 1",
"b": "demo value 2"
}
}
},
"variables": {
"delimiters": [","],
"settingArray": "[split(replace(replace(replace(string(parameters('testSettings')), '{', ''), '}', ''), '\"', ''), variables('delimiters'))]",
"propNameArray": {
"copy": [
{
"name": "copyPropertyNames",
"count": "[length(variables('settingArray'))]",
"input": "[substring(variables('settingArray')[copyIndex('copyPropertyNames')], 0, indexOf(variables('settingArray')[copyIndex('copyPropertyNames')], ':'))]"
}
]
}
},
"resources": [],
"outputs": {
"paramOutput": {
"type": "array",
"value": "[variables('propNameArray').copyPropertyNames]"
}
}
}