7

For my Azure ARM templates, I want to conditionally add an extra NSG rule. If parameter is true, append extra rule to the "securityRules" array. How do I efficiently go about this? I can't use the "condition" properties for nested objects. Creating two resources seems clunky.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Ted
  • 111
  • 1
  • 6

3 Answers3

7

Depending on a condition, you would like to add an additional (string) value to an existing json array. This can be done via the concat function. In order to concat an array and a string value, the string value will need to be transformed into an array too. When the condition is true, the two arrays can be concatenated. When the condition is false, you can concatenate the existing string with an empty array.

"[concat( parameters('existingArray'), if( parameters('condition'), array('Cc'), variables('emptyArray')) )]"

Assuming the orriginal array is: ["Aa", "Bb"]

  • When the condition is true, this will result in: ["Aa", "Bb", "Cc"]
  • When the condition is false, this will result in: ["Aa", "Bb"]
Gijs Walraven
  • 71
  • 1
  • 2
0

Overthought it. Variables are allowed to be defined as arrays. Define two variables, each with the different set of rules. Apply "if" function to securityRules base on parameter "allowInternetAccess".

Ted
  • 111
  • 1
  • 6
0

My solution:

In my parameter file (JSON) operations are defined as follows (this is not the complete parameter file)

"Operation1": {
    "name": "operation-1",
    ...
},
"Operation2": {}

Bicep handling for operations:

var allOperations = [
    apiManagement.api.operations.Operation1
    apiManagement.api.operations.Operation2
]

var usedOperations = [
    apiManagement.api.operations.Operation1
    empty(apiManagement.api.operations.Operation2) ? null : apiManagement.api.operations.Operation2
]

var operations = intersection(allOperations, usedOperations)

module api_operation './shared/api-operation.bicep' = [for operation in operations: {
  name: '${apiManagement.api.name}-${operation.name}'
  params: {
    apiManagement: apiManagement
    operation: operation
  } 
}]