如何使用ARM模板创建多个Service Bus主题?

时间:2016-08-25 12:43:58

标签: json azure azureservicebus azure-resource-manager azure-deployment

我查看了https://github.com/Azure/azure-quickstart-templates/tree/master/101-servicebus-topic示例。现在,我试图在使用嵌套资源循环(https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-multiple/#looping-on-a-nested-resource)运行部署脚本时弄清楚如何创建多个主题。

最好在serviceBusTopicName的值中定义一个数组,然后以某种方式通过循环创建主题,而不是手动创建主题。

尝试部署时出现以下错误。

 Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template resource '[parameters('serviceBusTopicName')[copyIndex()]]' at line '72' column '14' is not valid.
 Copying nested resources is not supported. Please see https://aka.ms/arm-copy/#looping-on-a-nested-resource for usage details.'.

这是我试图开始工作的template.json

  "parameters": {
"serviceBusNamespaceName": {
  "type": "string",
  "metadata": {
    "description": "Name of the Service Bus namespace"
  }
},
"serviceBusTopicName": {
  "type": "array",
  "metadata": {
    "description": "Name of the Topic"
  }
},
"serviceBusApiVersion": {
  "type": "string",
  "defaultValue": "2015-08-01",
  "metadata": {
    "description": "Service Bus ApiVersion used by the template"
  },
  "resources": [
{
  "apiVersion": "[variables('sbVersion')]",
  "name": "[parameters('serviceBusNamespaceName')]",
  "type": "Microsoft.ServiceBus/Namespaces",
  "location": "[variables('location')]",
  "kind": "Messaging",
  "sku": {
    "name": "StandardSku",
    "tier": "Standard"
  },
  "resources": [
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[parameters('serviceBusTopicName')]",
      "type": "Topics",
      "dependsOn": [
        "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
      ],
      "properties": {
        "path": "[parameters('serviceBusTopicName')]"
      },
      "copy": {
        "name": "datasetcopy",
        "count": "[length(parameters('serviceBusTopicName'))]"
      }
    }
  ]
},

parameters.json

{"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "serviceBusNamespaceName": {
        "value": "ServiceBus"
    },
  "serviceBusTopicName": {
    "value": [
      "Person",
      "Stadium",
      "Team"
    ]
  },
    "serviceBusApiVersion": {
        "value": "2015-08-01"
    }
}

2 个答案:

答案 0 :(得分:2)

正如@Lain所说,截至目前Check Here尚未支持复制嵌套资源。要部署多个资源,您必须将其移动到root。我刚刚完成了我的POC部署多个主题(假设Namespace已存在,只是主题)这里是代码。我将主题名称作为逗号分隔的字符串传递:

{
"$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"
        }
    },
    "serviceBusTopicName": {
        "type": "string",
        "metadata": {
            "description": "Comma seperated Topic Names"
        }
    },
    "serviceBusApiVersion": {
        "type": "string",
        "defaultValue": "2015-08-01",
        "metadata": {
            "description": "Service Bus ApiVersion used by the template"
        }
    }
},
"variables": {
    "location": "[resourceGroup().location]",
    "sbVersion": "[parameters('serviceBusApiVersion')]",
    "TopicNames": "[split(parameters('serviceBusTopicName'), ',')]"
},
"resources": [{
    "apiVersion": "[variables('sbVersion')]",
    "name": "[concat(parameters('serviceBusNamespaceName'), '/', variables('TopicNames')[copyIndex()])]",
    "type": "Microsoft.ServiceBus/Namespaces/Topics",
    "copy": {
        "name": "TopicNameCopy",
        "count": "[length(variables('TopicNames'))]"
    },
    "properties": {
        "path": "[variables('TopicNames')[copyIndex()]]"
    }
}]
}

答案 1 :(得分:0)

错误消息中的链接非常好,但您不能在嵌套资源中使用循环,需要将资源推送到顶层,然后使用名称将资源链接在一起。此模板将执行您似乎尝试执行的操作:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serviceBusNamespaceName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus Namespace"
      }
    },
    "serviceBusTopicNames": {
      "type": "array",
      "metadata": {
        "description": "Name of the Service Bus Topic"
      }
    }
  },
  "variables": {
    "sbVersion": "2015-08-01"
  },
  "resources": [
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[parameters('serviceBusNamespaceName')]",
      "type": "Microsoft.ServiceBus/namespaces",
      "location": "[resourceGroup().location]",
      "properties": {
      }
    },
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[concat(parameters('serviceBusNamespaceName'), '/',  parameters('serviceBusTopicNames')[copyIndex()])]",
      "type": "Microsoft.ServiceBus/namespaces/Topics",
      "dependsOn": [
        "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
      ],
      "copy": {
        "name": "datasetcopy",
        "count": "[length(parameters('serviceBusTopicNames'))]"
      },
      "properties": {
        "path": "[parameters('serviceBusTopicNames')[copyIndex()]]"
      },
      "resources": [
      ]
    }
  ]
}