Azure Bicep如何更改Json数组的值

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

Azure Bicep change a Json Array value

问题

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

  1. var updatedEventHubs = [for ns in eventHubNameSpaces : {
  2. desc: ns.desc
  3. sku: ns.sku
  4. properties: ns.properties
  5. networkRuleSets: ns.networkRuleSets
  6. eventHubs: [for eh in ns.eventHubs : {
  7. desc: '${location_short[location]}-${environment}${envNumber}-${purpose}-${eh.desc}-ev'
  8. messageRetentionInDays: eh.messageRetentionInDays
  9. partitionCount: eh.partitionCount
  10. status: eh.status
  11. authorizationRulesRights: eh.authorizationRulesRights
  12. consumerGroups: eh.consumerGroups
  13. }]
  14. }]

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

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

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

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

  1. module eventHubs 'br:myacr.azurecr.io/bicep/modules/eventhubs/eventhubnamespace/eventhubnamespace.bicep:v2.2.0' = [for (eh, i) in updatedEventHubs: {
  2. name: 'EventHub${location_short[location]}-${environment}${envNumber}-${purpose}-${la.desc}-deploy'
  3. scope: resourceGroup(resourceGroupsMonitoring[i].name)
  4. params: {
  5. name: 'my-evhns'
  6. location: location
  7. sku: eh.sku
  8. disableLocalAuth: eh.properties.disableLocalAuth
  9. isAutoInflateEnabled: eh.properties.isAutoInflateEnabled
  10. kafkaEnabled: eh.properties.kafkaEnabled
  11. maximumThroughputUnits: eh.properties.maximumThroughputUnits
  12. minimumTlsVersion: eh.properties.minimumTlsVersion
  13. publicNetworkAccess: eh.properties.publicNetworkAccess
  14. zoneRedundant: eh.properties.zoneRedundant
  15. networkRuleSets: eh.networkRuleSets
  16. tags: tags
  17. privateEndPoints: [{
  18. peName: 'my-evhns-pe'
  19. peRG: resourceGroupsMonitoring[i].name
  20. peSubId: subscription().subscriptionId
  21. resource: 'namespace'
  22. virtualNetworkRG: 'my-network-rg'
  23. virtualNetworkName: 'my-lan-vnet'
  24. virtualNetworkSubId: subscription().subscriptionId
  25. subnetName: 'endpoints-sn'
  26. indexValue: 0
  27. }]
  28. eventHubs: eh.eventHubs
  29. }
  30. }]

希望这能帮到你!

英文:

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

  1. var updatedEventHubs = [for ns in eventHubNameSpaces : {
  2. desc: ns.desc
  3. sku: ns.sku
  4. properties: ns.properties
  5. networkRuleSets: ns.networkRuleSets
  6. eventHubs: [for eh in ns.eventHubs : {
  7. desc: '${location_short[location]}-${environment}${envNumber}-${purpose}-${eh.desc}-ev'
  8. messageRetentionInDays: eh.messageRetentionInDays
  9. partitionCount: eh.partitionCount
  10. status: eh.status
  11. authorizationRulesRights: eh.authorizationRulesRights
  12. consumerGroups: eh.consumerGroups
  13. }
  14. ]
  15. }
  16. ]

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:

  1. module eventHubs 'br:myacr.azurecr.io/bicep/modules/eventhubs/eventhubnamespace/eventhubnamespace.bicep:v2.2.0' = [for (eh, i) in updatedEventHubs: {
  2. name: 'EventHub${location_short[location]}-${environment}${envNumber}-${purpose}-${la.desc}-deploy'
  3. scope: resourceGroup(resourceGroupsMonitoring[i].name)
  4. params: {
  5. name: 'my-evhns'
  6. location: location
  7. sku: eh.sku
  8. disableLocalAuth: eh.properties.disableLocalAuth
  9. isAutoInflateEnabled: eh.properties.isAutoInflateEnabled
  10. kafkaEnabled: eh.properties.kafkaEnabled
  11. maximumThroughputUnits: eh.properties.maximumThroughputUnits
  12. minimumTlsVersion: eh.properties.minimumTlsVersion
  13. publicNetworkAccess: eh.properties.publicNetworkAccess
  14. zoneRedundant: eh.properties.zoneRedundant
  15. networkRuleSets: eh.networkRuleSets
  16. tags: tags
  17. privateEndPoints: [{
  18. peName: 'my-evhns-pe'
  19. peRG: resourceGroupsMonitoring[i].name
  20. peSubId: subscription().subscriptionId
  21. resource: 'namespace'
  22. virtualNetworkRG: 'my-network-rg'
  23. virtualNetworkName: 'my-lan-vnet'
  24. virtualNetworkSubId: subscription().subscriptionId
  25. subnetName: 'endpoints-sn'
  26. indexValue: 0
  27. }]
  28. eventHubs: eh.eventHubs
  29. }
  30. }]

答案1

得分: 0

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

  1. module eventHubs 'br:myacr.azurecr.io/bicep/modules/eventhubs/eventhubnamespace/eventhubnamespace.bicep:v2.2.1' = [for (eh, i) in eventHubNameSpaces: {
  2. name: 'EventHub-deploy'
  3. scope: resourceGroup(resourceGroupsSiem[i].name)
  4. params: {
  5. name: 'my-evhns'
  6. location: location
  7. sku: eh.sku
  8. disableLocalAuth: eh.properties.disableLocalAuth
  9. isAutoInflateEnabled: eh.properties.isAutoInflateEnabled
  10. kafkaEnabled: eh.properties.kafkaEnabled
  11. maximumThroughputUnits: eh.properties.maximumThroughputUnits
  12. minimumTlsVersion: eh.properties.minimumTlsVersion
  13. publicNetworkAccess: eh.properties.publicNetworkAccess
  14. zoneRedundant: eh.properties.zoneRedundant
  15. networkRuleSets: eh.networkRuleSets
  16. tags: tags
  17. privateEndPoints: [{
  18. peName: 'my-evhns-pe'
  19. peRG: resourceGroupsSiem[i].name
  20. peSubId: subscription().subscriptionId
  21. resource: 'namespace'
  22. virtualNetworkRG: 'my-network-rg'
  23. virtualNetworkName: 'my-lan-vnet'
  24. virtualNetworkSubId: subscription().subscriptionId
  25. subnetName: 'endpoints-sn'
  26. indexValue: 0
  27. }]
  28. eventHubs: [for ehs in eh.eventHubs : {
  29. name: '${location_short[location]}-${environment}${envNumber}-${purpose}-${eh.desc}'
  30. messageRetentionInDays: ehs.messageRetentionInDays
  31. partitionCount: ehs.partitionCount
  32. status: ehs.status
  33. authorizationRulesRights: ehs.authorizationRulesRights
  34. consumerGroups: ehs.consumerGroups
  35. }
  36. ]
  37. }
  38. }]
英文:

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

  1. module eventHubs 'br:myacr.azurecr.io/bicep/modules/eventhubs/eventhubnamespace/eventhubnamespace.bicep:v2.2.1' = [for (eh, i) in eventHubNameSpaces: {
  2. name: 'EventHub-deploy'
  3. scope: resourceGroup(resourceGroupsSiem[i].name)
  4. params: {
  5. name: 'my-evhns'
  6. location: location
  7. sku: eh.sku
  8. disableLocalAuth: eh.properties.disableLocalAuth
  9. isAutoInflateEnabled: eh.properties.isAutoInflateEnabled
  10. kafkaEnabled: eh.properties.kafkaEnabled
  11. maximumThroughputUnits: eh.properties.maximumThroughputUnits
  12. minimumTlsVersion: eh.properties.minimumTlsVersion
  13. publicNetworkAccess: eh.properties.publicNetworkAccess
  14. zoneRedundant: eh.properties.zoneRedundant
  15. networkRuleSets: eh.networkRuleSets
  16. tags: tags
  17. privateEndPoints: [{
  18. peName: 'my-evhns-pe'
  19. peRG: resourceGroupsSiem[i].name
  20. peSubId: subscription().subscriptionId
  21. resource: 'namespace'
  22. virtualNetworkRG: 'my-network-rg'
  23. virtualNetworkName: 'my-lan-vnet'
  24. virtualNetworkSubId: subscription().subscriptionId
  25. subnetName: 'endpoints-sn'
  26. indexValue: 0
  27. }]
  28. eventHubs: [for ehs in eh.eventHubs : {
  29. name: '${location_short[location]}-${environment}${envNumber}-${purpose}-${eh.desc}'
  30. messageRetentionInDays: ehs.messageRetentionInDays
  31. partitionCount: ehs.partitionCount
  32. status: ehs.status
  33. authorizationRulesRights: ehs.authorizationRulesRights
  34. consumerGroups: ehs.consumerGroups
  35. }
  36. ]
  37. }
  38. }]

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:

确定