Azure Bicep如何更改Json数组的值

huangapple go评论54阅读模式
英文:

Azure Bicep change a Json Array value

问题

我正在尝试根据环境和位置调整一些基本的 Json 配置。这是我的尝试:

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
    }]
}]

但是在需要更改 desc 的内部 for loop 中,我遇到了以下编译错误:

在此上下文中不支持 for 表达式。for 表达式可以用作资源、模块、变量和输出声明的值,或者用作资源和模块属性的值。bicep(BCP138)

然后,结果数组将在一个模块中用于调用 Azure 容器注册表。如果有帮助的话,我可以提供那部分代码,但问题更多地涉及在运行时调整基本值。

剩余的代码。使用上面的数组进行调用:

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
  }
}]

希望这能帮到你!

英文:

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
  }
}]

答案1

得分: 0

最终,我将循环放置在了ACR调用中:

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
      }
    ]
  }
}]
英文:

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
      }
    ]
  }
}]

huangapple
  • 本文由 发表于 2023年8月8日 23:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定