gpt4 book ai didi

Azure 二头肌嵌套循环

转载 作者:行者123 更新时间:2023-12-03 01:13:58 24 4
gpt4 key购买 nike

我正在尝试使用 json 配置和 Bicep 创建 Azure 事件中心。

事件中心命名空间部署正常,但是当我尝试将代码扩展到 namespaces/eventhubsnamespaces/eventhubs/authorizationRules 时,我遇到了一个问题,因为我有从错误的外观来看,嵌套循环是不可能的:

嵌套资源不能出现在带有 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
}
}]

关于Azure 二头肌嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75697220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com