我正在尝试使用 json 配置和 Bicep 创建一个 Azure 事件中心。
Event Hub 命名空间部署得很好,但是当我尝试将代码扩展到
namespaces/eventhubs
和 namespaces/eventhubs/authorizationRules
然后我遇到了一个问题,因为我有嵌套循环,从错误的外观来看这是不可能的:
A nested resource cannot appear inside of a resource with a for-expression.bicep(BCP160)
我的代码:
param eventHubs array
resource eventHubNS 'Microsoft.EventHub/namespaces/eventhubs@2022-01-01-preview' = [for ns in eventHubs: {
parent: eventHub
name: ns.name
properties: {
messageRetentionInDays: ns.properties.messageRetentionInDays
partitionCount: ns.properties.partitionCount
status: ns.properties.status
}
resource eventHubAuthRules 'authorizationRules@2022-01-01-preview' = [for ar in ns.authorizationRulesRights: {
name: ar.name
properties: {
rights: ar.rights
}
}]
}]
Json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"eventHubNameSpace": {
"value": [{
"resourceGroup": "my-rg",
"name": "my-evhns",
"location": "uksouth",
"sku": {
"capacity": 1,
"name": "standard",
"tier": "standard"
},
"properties": {
"minimumTlsVersion": "1.2",
"publicNetworkAccess": "Enabled",
"disableLocalAuth": false,
"zoneRedundant": true,
"isAutoInflateEnabled": false,
"maximumThroughputUnits": 1,
"kafkaEnabled": true
},
"eventHubs": [
{
"name": "my-evh",
"properties": {
"messageRetentionInDays": 1,
"partitionCount": 1,
"status": "Active"
},
"authorizationRulesRights": [
{
"name": "Rule1",
"rights": ["Listen"]
},
{
"name": "Rule2",
"rights": ["Listen"]
}
]
}
]
}
]
}
}
}
你总是可以创建一个模块来创建授权规则:
// authorizationRules.bicep
param eventHubNSName string
param authorizationRulesRights array
resource eventHubNS 'Microsoft.EventHub/namespaces/eventhubs@2022-01-01-preview' existing = {
name: eventHubNSName
}
resource eventHubAuthRules 'Microsoft.EventHub/namespaces/eventhubs/authorizationRules@2022-01-01-preview' = [for ar in authorizationRulesRights: {
parent: eventHubNS
name: ar.name
properties: {
rights: ar.rights
}
}]
然后像那样调用它:
...
param eventHubs array
resource eventHubNS 'Microsoft.EventHub/namespaces/eventhubs@2022-01-01-preview' = [for ns in eventHubs: {
parent: eventHub
name: ns.name
properties: {
messageRetentionInDays: ns.properties.messageRetentionInDays
partitionCount: ns.properties.partitionCount
status: ns.properties.status
}
}]
module eventHubAuthRules 'authorizationRules.bicep' = [for (ns, i) in eventHubs: {
name: 'authorizationRules-${ns.name}'
params: {
eventHubNSName: eventHubNS[i].name
authorizationRulesRights: ns.authorizationRulesRights
}
}]