英文:
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
}
]
}
}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论