Search code examples
azureazure-bicep

Azure Bicep change a Json Array value


I am trying to adjust some base Json config based on environment and location. This is my attempt:

var updatedEventHubs = [for ns in eventHubNameSpaces : {
    desc: ns.desc
    sku: ns.sku
    properties: ns.properties
    networkRuleSets: ns.networkRuleSets
    eventHubs: [for eh in ns.eventHubs : {
        desc: '${location_short[location]}-${environment}${envNumber}-${purpose}-${eh.desc}-ev'
        messageRetentionInDays: eh.messageRetentionInDays
        partitionCount: eh.partitionCount
        status: eh.status
        authorizationRulesRights: eh.authorizationRulesRights
        consumerGroups: eh.consumerGroups
      }
    ]
  }
]

But on the inner for loop, where I need to change desc, I have the following compilation error:

For-expressions are not supported in this context. For-expressions may be used as values of resource, module, variable, and output declarations, or values of resource and module properties.bicep(BCP138)

The results array will then be used in a module to call an Azure Container Registry. I can supply that code if it helps, but the question more relates around adjusting the base values at runtime

Remainder of code. This is called using the array above:

module eventHubs 'br:myacr.azurecr.io/bicep/modules/eventhubs/eventhubnamespace/eventhubnamespace.bicep:v2.2.0' = [for (eh, i) in updatedEventHubs: {  
  name: 'EventHub${location_short[location]}-${environment}${envNumber}-${purpose}-${la.desc}-deploy'
  scope: resourceGroup(resourceGroupsMonitoring[i].name)
  params: {
    name: 'my-evhns'
    location: location
    sku: eh.sku
    disableLocalAuth: eh.properties.disableLocalAuth
    isAutoInflateEnabled: eh.properties.isAutoInflateEnabled
    kafkaEnabled: eh.properties.kafkaEnabled
    maximumThroughputUnits: eh.properties.maximumThroughputUnits
    minimumTlsVersion: eh.properties.minimumTlsVersion
    publicNetworkAccess: eh.properties.publicNetworkAccess
    zoneRedundant: eh.properties.zoneRedundant
    networkRuleSets: eh.networkRuleSets
    tags: tags
    privateEndPoints: [{
        peName: 'my-evhns-pe'
        peRG:  resourceGroupsMonitoring[i].name
        peSubId: subscription().subscriptionId
        resource: 'namespace'
        virtualNetworkRG: 'my-network-rg'
        virtualNetworkName: 'my-lan-vnet'
        virtualNetworkSubId: subscription().subscriptionId
        subnetName: 'endpoints-sn'
        indexValue: 0
    }]
    eventHubs: eh.eventHubs
  }
}]

Solution

  • In the end, I placed the loop in the ACR call:

    module eventHubs 'br:myacr.azurecr.io/bicep/modules/eventhubs/eventhubnamespace/eventhubnamespace.bicep:v2.2.1' = [for (eh, i) in eventHubNameSpaces: {  
      name: 'EventHub-deploy'
      scope: resourceGroup(resourceGroupsSiem[i].name)
      params: {
        name: 'my-evhns'
        location: location
        sku: eh.sku
        disableLocalAuth: eh.properties.disableLocalAuth
        isAutoInflateEnabled: eh.properties.isAutoInflateEnabled
        kafkaEnabled: eh.properties.kafkaEnabled
        maximumThroughputUnits: eh.properties.maximumThroughputUnits
        minimumTlsVersion: eh.properties.minimumTlsVersion
        publicNetworkAccess: eh.properties.publicNetworkAccess
        zoneRedundant: eh.properties.zoneRedundant
        networkRuleSets: eh.networkRuleSets
        tags: tags
        privateEndPoints: [{
            peName: 'my-evhns-pe'
            peRG:  resourceGroupsSiem[i].name
            peSubId: subscription().subscriptionId
            resource: 'namespace'
            virtualNetworkRG: 'my-network-rg'
            virtualNetworkName: 'my-lan-vnet'
            virtualNetworkSubId: subscription().subscriptionId
            subnetName: 'endpoints-sn'
            indexValue: 0
        }]
        eventHubs: [for ehs in eh.eventHubs : {
          name: '${location_short[location]}-${environment}${envNumber}-${purpose}-${eh.desc}'
          messageRetentionInDays: ehs.messageRetentionInDays
          partitionCount: ehs.partitionCount
          status: ehs.status
          authorizationRulesRights: ehs.authorizationRulesRights
          consumerGroups: ehs.consumerGroups
          }
        ]
      }
    }]