gpt4 book ai didi

用于具有多个订阅主题的服务总线的 Azure ARM 模板

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

您好,我有一个 ARM 模板,用于创建 ServiceBus 及其主题和订阅。但我只能完成 1 个主题 - 1 个订阅,因为我无法创建嵌套循环来为每个主题创建多个订阅。

我希望我可以执行这样的模板:

参数:

{
"serviceBusName": "mybus",
"topics":
[
{
"topicName": "mytopic1",
"subscriptions": [ "mysubscription1", "mysubscription2"]
},
{
"topicName": "mytopic2",
"subscriptions": [ "mysubscription1"]
}
]
}

这是我的实际模板:

{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"ServiceBusNamespaceName": {
"type": "string"
},
"ServiceBusSku": {
"type": "string",
"allowedValues": [
"Basic",
"Standard"
],
"defaultValue": "Standard"
},
"ServiceBusSmallSizeTopicInMb": {
"type": "int",
"defaultValue": 1024
},
"ServiceBusMaxSizeTopicInMb": {
"type": "int",
"defaultValue": 1024
},
"Topics": {
"type": "array"
}
},
"variables": {
"DefaultSASKeyName": "RootManageSharedAccessKey",
"DefaultAuthRuleResourceId": "[resourceId('Microsoft.ServiceBus/namespaces/authorizationRules', parameters('ServiceBusNamespaceName'), variables('DefaultSASKeyName'))]",
"SbVersion": "2017-04-01"
},
"resources": [
{
"apiVersion": "2017-04-01",
"name": "[parameters('ServiceBusNamespaceName')]",
"type": "Microsoft.ServiceBus/namespaces",
"location": "[resourceGroup().location]",
"sku": {
"name": "[parameters('ServiceBusSku')]"
},
"tags": {
"displayName": "ServiceBus"
}
},
{
"copy": {
"name": "topics",
"count": "[length(parameters('Topics'))]"
},
"type": "Microsoft.ServiceBus/namespaces/topics",
"name": "[concat(parameters('ServiceBusNamespaceName'), '/', parameters('Topics')[copyIndex()].topic)]",
"apiVersion": "2017-04-01",
"location": "[resourceGroup().location]",
"scale": null,
"properties": {
"defaultMessageTimeToLive": "P1D",
"maxSizeInMegabytes": "[parameters('ServiceBusMaxSizeTopicInMb')]",
"requiresDuplicateDetection": false,
"duplicateDetectionHistoryTimeWindow": "PT10M",
"enableBatchedOperations": true,
"status": "Active",
"supportOrdering": true,
"autoDeleteOnIdle": "P10675199DT2H48M5.4775807S",
"enablePartitioning": false,
"enableExpress": false
},
"dependsOn": [
"[resourceId('Microsoft.ServiceBus/namespaces', parameters('ServiceBusNamespaceName'))]"
],
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('Topics')[copyIndex()].subscription]",
"type": "Subscriptions",
"dependsOn": [
"[parameters('Topics')[copyIndex()].topic]"
],
"properties": {
"lockDuration": "PT1M",
"requiresSession": "false",
"defaultMessageTimeToLive": "P7D",
"deadLetteringOnMessageExpiration": "false",
"maxDeliveryCount": "2",
"enableBatchedOperations": "true",
"autoDeleteOnIdle": "P7D"
}
}
]
}
],
"outputs": {
"NamespaceDefaultConnectionString": {
"type": "string",
"value": "[listkeys(variables('DefaultAuthRuleResourceId'), variables('SbVersion')).primaryConnectionString]"
},
"DefaultSharedAccessPolicyPrimaryKey": {
"type": "string",
"value": "[listkeys(variables('DefaultAuthRuleResourceId'), variables('SbVersion')).primaryKey]"
}
}
}

以及实际模板的 params json 示例:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"ServiceBusNamespaceName": {
"value": "mybus"
},
"ServiceBusSku": {
"value": "Standard"
},
"Topics ": {
"value": [
{
"topic": "mytopic1",
"subscription": "mysubscription1"
},
{
"topic": "mytopic2",
"subscription": "mysubscription1"
}
]
}
}
}

最佳答案

一般来说,有两种方法可以完成这样的事情。您可以重组代码,使订阅成为顶级资源。或者您使用 copyIndex 的命名变体来实现嵌套循环。这两种变体都可以在这篇博文及其评论中看到。

Azure ARM Templates – Are nested loops possible?

但是,对于您的情况,唯一的选择是将订阅设为顶级资源。请参阅 aka.ms/arm-copy/#looping-on-a-nested-resource了解更多详情。

这是取自上述博客文章的完整示例。

{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"serviceBusNamespaceName": {
"type": "string",
"metadata": {
"description": "Name of the Service Bus namespace"
}
},
"topics":{
"type": "array",
"metadata": {
"description": "List of topics"
}
},
"subscriptions":{
"type": "array",
"metadata": {
"description": "List of subscriptions"
}
}

},
"variables": {},
"resources": [
{
"type": "Microsoft.ServiceBus/namespaces",
"sku": {
"name": "Standard"
},
"name": "[parameters('serviceBusNamespaceName')]",
"apiVersion": "2017-04-01",
"location": "[resourceGroup().location]",
"properties": {}
},
{
"type": "Microsoft.ServiceBus/namespaces/topics",
"name": "[concat(parameters('serviceBusNamespaceName'), '/', parameters('topics')[copyIndex()])]",
"apiVersion": "2017-04-01",
"location": "[resourceGroup().location]",
"copy": {
"name": "topicLoop",
"count": "[length(parameters('topics'))]"
},
"properties": {},
"dependsOn": [
"[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
]
},
{
"type": "Microsoft.ServiceBus/namespaces/topics/subscriptions",
"name": "[concat(parameters('serviceBusNamespaceName'), '/', parameters('subscriptions')[copyIndex()].topic, '/', parameters('subscriptions')[copyIndex()].subscription)]",
"apiVersion": "2017-04-01",
"location": "[resourceGroup().location]",
"copy": {
"name": "subscriptionLoop",
"count": "[length(parameters('subscriptions'))]"
},
"properties": {},
"dependsOn": [
"topicLoop"
]
}
]
}
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"serviceBusNamespaceName": {
"value": "rjtestsbnmspace"
},
"topics": {
"value": ["topic1", "topic2"]
},
"subscriptions": {
"value": [{
"topic": "topic1",
"subscription": "subscription1"
},
{
"topic": "topic1",
"subscription": "subscription2"
},
{
"topic": "topic2",
"subscription": "subscription3"
}
]
}
}
}

此虚拟机示例使用名为 copyIndex - 当嵌套循环不适用于资源本身时,它会起作用。

{
"name": "[concat(parameters('vmName'), padLeft(copyIndex(1), 2, '0'))]",
"type": "Microsoft.Compute/virtualMachines",
"copy": {
"name": "vmLoop",
"count": "[parameters('vmCount')]"
},
"properties": {
"osProfile": {
"computerName": "[concat(parameters('vmName'), padLeft(copyIndex(1), 2, '0'))]"
},
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"storageProfile": {
"osDisk": {
"name": "[concat(parameters('vmName'), padLeft(copyIndex(1), 2, '0'),'_OSDisk')]",
"createOption": "FromImage",
"managedDisk": {
"storageAccountType": "[parameters('vmDiskType')]"
}
},
"copy": [
{
"name": "dataDisks",
"count": "[parameters('dataDiskCount')]",
"input": {
"caching": "[parameters('dataDiskCaching')]",
"name": "[concat(parameters('vmName'), padLeft(copyIndex('vmLoop', 1), 2, '0'), '-dataDisk', padLeft(copyIndex('dataDisks'), 2, '0'))]",
"lun": "[copyIndex('dataDisks')]",
"createOption": "Empty",
"diskSizeGB": "[parameters('dataDiskSize')]",
"managedDisk": {
"storageAccountType": "[parameters('vmDiskType')]"
}
}
}
]
}
}
}

关于用于具有多个订阅主题的服务总线的 Azure ARM 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57690355/

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