Search code examples
azureazure-cosmosdbazure-resource-managerazure-bicep

How can I conditionally select which property to use in a Bicep child resource loop, cosmosDb Autoscale vs Throughput


For deploying a series of ComsosDb collections that have different settings, i have a bicep file that iterates through an array of Collection settings such as the following.

param containers array = [
  {
    name: 'Device'
    partitionKey: '/TID'
    autoScaleEnabled: true
    throughput: 500
    maxAutoScale: '0'
    timeToLiveEnabled: false
    timeToLiveTimeInSec: 50
  }
]

However some collections will have Autoscale enabled while others will just have the throughput defined. This equates to

resource accountName_sql_databaseName_containers_name 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2023-04-15' = [for item in containers: {
  parent: accountName_sql_database
  name: item.name
  properties: {
    options: {
      autoscaleSettings: {
        maxThroughput: 1234
      }
    }
  }
}]

OR

resource accountName_sql_databaseName_containers_name 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2023-04-15' = [for item in containers: {
  parent: accountName_sql_database
  name: item.name
  properties: {
    options: {
      throughput: 12345
    }
  }
}]

Not both, not one with null values. One or the Other. I can't move the resource creation out of this type of loop as that's not supported.

You can't pass null, can't pass 0, can't pass both. I've played around with defining the 'Options' resource outside, but you lose the iterative access. As well as trying to do an inline if/else, but haven't been able to get that to work. something along the lines of

options: item.autoScaleEnabled ? ({maxThroughput: 12}) : FalseValue(){{

How do I select one property and it's value or another property based on a flag I set in an array or otherwise?


Solution

  • You should be able to build the options object based on a condition:

    resource accountName_sql_databaseName_containers_name 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2023-04-15' = [for item in containers: {
      parent: accountName_sql_database
      name: item.name
      properties: {
        options: item.autoScaleEnabled ? {
          autoscaleSettings: {
            maxThroughput: 1234
          }
        } : {
          throughput: 12345
        }
      }
    }]