ARM Templates: Optional properties in object array references
Hey everyone.
I managed to make syntax that can support optional properties in ARM object array params. this works by using contains, which in the case of a object will return true if property exists and false if not
as docs says:
Checks whether an array contains a value, an object contains a key, or a string contains a substring. The string comparison is case-sensitive. However, when testing if an object contains a key, the comparison is case-insensitive.
ref: https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-object#contains
Example:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"virtualNetworks": {
"type": "array"
}
},
"variables": {
"copy": [
{
"name": "testloopvar",
"count": "[length(parameters('virtualNetworks'))]",
"input": {
"value1": "[if(contains(parameters('virtualNetworks')[copyIndex('testloopvar')], 'value1'),parameters('virtualNetworks')[copyIndex('testloopvar')].value1, null())]",
"value2": "[if(contains(parameters('virtualNetworks')[copyIndex('testloopvar')], 'value2'),parameters('virtualNetworks')[copyIndex('testloopvar')].value2, null())]"
}
}
]
},
"resources": [
]
}
with these params: (you can see both values are not in every item)
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "0.0.0.1",
"parameters": {
"virtualNetworks": {
"value": [
{
"value1": "pvne001ent_dev",
"value2": "pvne001ent_dev2"
},
{
"value1": "pvne002ent_dev"
},
{
"value2": "pvne003ent_dev2"
}
]
}
}
}
this makes it easier to add new properties or to simplify parameter file, rather than having to give all object all properties even though they are using default value
Written on October 19, 2021