I've been able to figure out how to setup an Azure ARM Template that creates/manages an Azure Service Bus Namespace, Topic and Subscription to receive all messages. However, the Microsoft documentation is extremely lacking still on ARM Tempates, and I am unable to figure out how to define a SqlFilter for the Subscription within the template that you can manage using the .NET SDK.
Does anyone know how to add a Sql Filter to a Service Bus Topic Subscription within an ARM Template?
Here's a link to the ARM Template I have for creating the Service Bus Topic and Subscription without Sql filter:
Also, here's the source of the ARM Template I'm referring to:
{
"$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"
}
},
"serviceBusTopicName": {
"type": "string",
"metadata": {
"description": "Name of the Service Bus Topic"
}
},
"serviceBusTopicSubscriptionName": {
"type": "string",
"metadata": {
"description": "Name of the Service Bus Topic Subscription"
}
}
},
"variables": {
"sbVersion": "2015-08-01"
},
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusNamespaceName')]",
"type": "Microsoft.ServiceBus/namespaces",
"location": "[resourceGroup().location]",
"properties": {
},
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusTopicName')]",
"type": "Topics",
"dependsOn": [
"[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
],
"properties": {
"path": "[parameters('serviceBusTopicName')]"
},
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusTopicSubscriptionName')]",
"type": "Subscriptions",
"dependsOn": [
"[parameters('serviceBusTopicName')]"
],
"properties": {
},
"resources": [
]
}
]
}
]
}
],
"outputs": {
}
}
A Sql Filter should be inside a Rule, so we should create a rule within the Service Bus Topic Subscription. For example:
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusTopicSubscriptionName')]",
"type": "Subscriptions",
"dependsOn": [
"[parameters('serviceBusTopicName')]"
],
"properties": {
},
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusTopicSubscriptionRuleName')]",
"type": "Rules",
"dependsOn": [
"[parameters('serviceBusTopicSubscriptionName')]"
],
"properties": {
},
"resources": [
]
}
]
}
]
I have tried to deploy this template, but I get the following error:
New-AzureRmResourceGroupDeployment : InvalidTemplate: Deployment template validation failed: 'The template resource 'Microsoft.ServiceBus/namespaces/<serviceBusNamespaceName>/Topics/<serviceBusTopicName>/Subscriptions/<serviceBusTopicSubscriptionName>' cannot reference itself. Please see http://aka.ms/arm-template-expressions/#reference for usage details.'.
At line:1 char:1
+ New-AzureRmResourceGroupDeployment -Name ServiceBusTest -ResourceGrou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmResourceGroupDeployment], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureResourceGroupDeploymentCommand
From the error message, "'The template resource cannot reference itself", I am guessing that creating Sql Filter for a Topic Subscription is not yet implemented in ARM template.
After some more diggings, I believe that Topic Subscription Rule is not manageable by Resource Manager yet. Here is the things I tried.
I use this PowerShell script to create a Topic Subscription with a rule. I have done some modification to the script by adding a name to the rule,
$RuleDescription.Name = "rule1"
.The Topic Subscription is successfully created, and I can use the following PowerShell command to get the Topic Subscription.
Get-AzureRmResource -ResourceGroupName Default-ServiceBus-EastUS ` -ResourceType Microsoft.ServiceBus/namespaces/topics/Subscriptions ` -ResourceName <namespace>/<topic>/<subscription> ` -ApiVersion 2014-09-01
When I try to get the Topic Subscription Rule with a similar PowerShell command:
Get-AzureRmResource -ResourceGroupName Default-ServiceBus-EastUS ` -ResourceType Microsoft.ServiceBus/namespaces/topics/Subscriptions/Rules ` -ResourceName <namespace>/<topic>/<subscription>/rule1 ` -ApiVersion 2014-09-01
I get the following error:
No HTTP resource was found that matches the request URI 'https://sbgm.windows.net/subscriptions/<subscriptionid>/resourceGroups/Default-ServiceBus-EastUS/providers/Microsoft.ServiceBus/namespaces/<namespace>/topics/<topic>/Subscriptions/<subscription>/Rules/rule1?api-version=2014-09-01'
However, if I use
$NamespaceManager.GetRules($TopicPath,$Name)
, I do get the above rule successfully. That means the rule is created successfully.
Just add following into your subscription resource to create SQL Filter and Action:
,"resources": [{
"apiVersion": "[variables('sbVersion')]",
"name": "$Default",
"type": "Rules",
"dependsOn": ["[parameters('serviceBusSubscriptionName')]"],
"properties": {
"filterType": "SqlFilter",
"sqlFilter": {
"sqlExpression": "1=1",
"requiresPreprocessing": false
},
"action": {
"sqlExpression": "set something = 'something'"
}
}
}]
Currently, ARM Templates do not support creating/managing Azure Service Bus Topic Subscription Filters.
This is now possible as per the following quick start template which illustrates adding a SQL Filter :
Also if you are looking to add a Correlation Filter via ARM, I've been able to do so by setting the Rules
resource as follows:
"resources": [
{
"apiVersion": "[variables('sbVersion')]",
"name": "$Default",
"type": "Rules",
"dependsOn": [
"[parameters('serviceBusSubscriptionName')]"
],
"properties": {
"filter": {
"correlationId": "[parameters('correlationId')]"
}
}
}
]
The Subscription syntax to add a Sql filter has changed recently.
<snip>
"apiVersion": "2017-04-01",
"name": "[parameters('serviceBusSubscriptionName')]",
<snip>
"resources": [
{
"apiVersion": "2017-04-01",
"name": "[parameters('serviceBusRuleName')]",
"type": "Rules",
"dependsOn": [
"[parameters('serviceBusSubscriptionName')]"
],
"properties": {
"filterType": "SqlFilter",
"sqlFilter": {
"sqlExpression": "FilterTag = 'true'",
"requiresPreprocessing": "false"
},
"action": {
"sqlExpression": "set FilterTag = 'true'"
}
}
}
]
You can find the latest examples in this ARM template:
https://github.com/Azure/azure-quickstart-templates/blob/master/201-servicebus-create-topic-subscription-rule/azuredeploy.json
Use the Service Bus Explorer app. If you already know what SQL filter that you want to create, I suggest you to download and use this application. Download it from here
- You only need to connect to you service bus, the add rules to the subscriptions.
- Then, go to Azure portal and retrieve the ARM template for that particular service bus.
- You'll be able to see how the SQL filters are built.
This is how you Add Rules to a subscription:
This is how you view the Rules that you've created:
It's pretty simple to use the Service Bus Explorer app. Since it's user interactive, you can always configure your service bus then go to Azure Portal to retrieve the ARM template.
来源:https://stackoverflow.com/questions/36269935/use-azure-arm-template-to-create-service-bus-topic-subscription-with-sql-filter