gpt4 book ai didi

azure - 如何根据二头肌文件中的条件创建嵌套资源?

转载 作者:行者123 更新时间:2023-12-05 04:24:51 25 4
gpt4 key购买 nike

我有一个二头肌文件,它接受一个 param topic object 对象,如下所示:

name: 'topic name'
subscriptions: [
{
name: 'subscriptionName'
ruleName: 'name of the rule'
}
]

接下来我将创建资源:

resource servicebus 'Microsoft.ServiceBus/namespaces@2021-11-01' existing = {
name: 'products'
}

resource topicResource 'Microsoft.ServiceBus/namespaces/topics@2021-11-01' = {
parent: servicebus

name: topic.topicName
}

resource subscriptions 'Microsoft.ServiceBus/namespaces/topics/subscriptions@2021-11-01' = [for obj in topic.subscriptions: {
parent: topicResource

name: obj.name
}]

resource rules 'Microsoft.ServiceBus/namespaces/topics/subscriptions/rules@2021-11-01' = [for (obj, i) in topic.subscriptions: {
parent: subscriptions[i]

name: obj.ruleName
properties: {
filterType: 'CorrelationFilter'
correlationFilter: {
label: obj.ruleName
}
}
}]

在某些情况下,我不想创建规则资源。这基于 ruleName 属性。如果此属性为空,我不想创建资源。

但是当我尝试应用此条件时,我收到一条错误消息:

{"code": "InvalidTemplate", "target": "[...]", "message": "Deployment template validation failed: 'The template resource '[..]' for type 'Microsoft.ServiceBus/namespaces/topics/subscriptions/rules' at line '1' and column '1763' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-template/#resources for usage details.'.", "additionalInfo": [{"type": "TemplateViolation", "info": {"lineNumber": 1, "linePosition": 1763, "path": "properties.template.resources[2].type"}}]}

我认为这个错误消息告诉我的是,您需要与父级相同的迭代次数,在本例中是 subscriptions 并且由于“子级”的循环条件资源,现在数量减少了 1(这对我来说很奇怪,因为为什么不继续数组中的下一项并将索引增加 1...但是没关系)。

所以我的问题是,当ruleName为空时如何跳过资源创建?

这是我尝试过的方法,并且出现了上述错误:

资源规则 'Microsoft.ServiceBus/namespaces/topics/subscriptions/rules@2021-11-01' = [for (obj, i) in topic.subscriptions: if (!empty(obj.ruleName)) {

最佳答案

条件 if (!empty(obj.ruleName)) 将转换为 ARM condition 属性。
即使资源不会被部署,它仍然存在于生成的 ARM 中。
这意味着生成的 ARM 中的 name 属性缺少一个段:

topic-name/subscription-name/empty-rule-name

您还必须在 name 属性上添加相同的条件(作为解决方法):

name: !empty(obj.ruleName) ? obj.ruleName : 'whatever'

在您的代码中,它应该如下所示:

resource rules 'Microsoft.ServiceBus/namespaces/topics/subscriptions/rules@2021-11-01' = [for (obj, i) in topic.subscriptions: if (!empty(obj.ruleName)) {
parent: subscriptions[i]
name: !empty(obj.ruleName) ? obj.ruleName : 'whatever'
properties: {
filterType: 'CorrelationFilter'
correlationFilter: {
label: obj.ruleName
}
}
}]

关于azure - 如何根据二头肌文件中的条件创建嵌套资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73416749/

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