Let's say you have a need to perform a set of 4 contitional operations on an array passed in as a parameter to an ARM template. For example, let's say you need to pass in a name and a value and perform either no operation or add/subtract/multiply by 2. This gist contains an example deployment JSON, template JSON, and an expande version of the ARM logic. The value will 9999 if none of the supported operations are provided.
Created
January 8, 2019 15:21
-
-
Save markekraus/b29726ffb6345d8a49e8234df367e283 to your computer and use it in GitHub Desktop.
Perform Conditional Operations on Parameters in Azure ARM templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
if( | |
equals( | |
parameters('settings')[copyIndex('settings')].type, | |
'static' | |
), | |
parameters('settings')[copyIndex('settings')].value, | |
if( | |
equals( | |
parameters('settings')[copyIndex('settings')].type, | |
'add' | |
), | |
add( | |
parameters('settings')[copyIndex('settings')].value, | |
2 | |
), | |
if( | |
equals( | |
parameters('settings')[copyIndex('settings')].type, | |
'mul' | |
), | |
mul( | |
parameters('settings')[copyIndex('settings')].value, | |
2 | |
), | |
if( | |
equals( | |
parameters('settings')[copyIndex('settings')].type, | |
'sub' | |
), | |
sub( | |
parameters('settings')[copyIndex('settings')].value, | |
2 | |
), | |
9999 | |
) | |
) | |
) | |
) | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", | |
"contentVersion": "1.0.0.0", | |
"parameters": { | |
"settings": { | |
"type": "array" | |
} | |
}, | |
"variables": { | |
"copy": [ | |
{ | |
"name": "settings", | |
"count": "[length(parameters('settings'))]", | |
"input": { | |
"name": "[parameters('settings')[copyIndex('settings')].name]", | |
"value": "[if(equals(parameters('settings')[copyIndex('settings')].type,'static'),parameters('settings')[copyIndex('settings')].value,if(equals(parameters('settings')[copyIndex('settings')].type,'add'),add(parameters('settings')[copyIndex('settings')].value,2),if(equals(parameters('settings')[copyIndex('settings')].type,'mul'),mul(parameters('settings')[copyIndex('settings')].value,2),if(equals(parameters('settings')[copyIndex('settings')].type,'sub'),sub(parameters('settings')[copyIndex('settings')].value,2),9999))))]" | |
} | |
} | |
] | |
}, | |
"resources": [], | |
"outputs": { | |
"results": { | |
"type": "array", | |
"value": "[variables('settings')]" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", | |
"contentVersion": "1.0.0.0", | |
"parameters": { | |
"settings": { | |
"value": [ | |
{ | |
"name": "a", | |
"value": 1, | |
"type": "static" | |
}, | |
{ | |
"name": "b", | |
"value": 2, | |
"type": "add" | |
}, | |
{ | |
"name": "c", | |
"value": 3, | |
"type": "mul" | |
}, | |
{ | |
"name": "d", | |
"value": 4, | |
"type": "sub" | |
} | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment