azure 条件对象构造不起作用ARM模板

xfyts7mz  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(86)

我正在尝试构造一个Azure ServiceBus订阅规则,该规则将根据参数的subjectFilter数组属性的长度来使用相关过滤器或SQL过滤器。我尝试使用基于this answerifunion来实现这一点。但是,看起来该对象是用所有属性构造的。例如,sql筛选器错误为“必须为相关性筛选器设置至少一个系统或用户属性”
这是我的代码(简化版)。

  • copy变量包含用于每个订阅名称和filterProperties的属性,其中定义了filterType,并用作基本properties对象。
  • 它还分别为sqlFilterPropertiescorrelationFilterProperties声明了一个单独的对象。
  • sqlFilterPropertiescorrelationFilterProperties对象应与filterProperties联合以构造完整的properties
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "resourceName": {
            "type": "string"
        },
        "topics": {
            "type": "array"
        },
        "subscriptionName": {
            "type": "string"
        }
    },
    "variables": {
        "apiVersion": "2018-01-01-preview",
        "copy": [
            {
                "name": "subscriptions",
                "count": "[length(parameters('topics'))]",
                "input": {
                    "name": "[concat(parameters('resourceName'), '/', parameters('topics')[copyIndex('subscriptions')].name, '/', parameters('subscriptionName'))]",
                    "filterProperties": {
                        "filterType": "[if(greater(length(parameters('topics')[copyIndex('subscriptions')].subjectFilter),1),'sqlFilter','correlationFilter')]"
                    },
                    "sqlFilterProperties": {
                        "sqlFilter": {
                            "sqlExpression": "[concat('sys.subject in (''',join(parameters('topics')[copyIndex('subscriptions')].subjectFilter,''','''), ''')')]",
                            "requiresPreprocessing": false
                        }
                    },
                    "correlationFilterProperties": {
                        "correlationFilter": {
                            "subject": "[parameters('topics')[copyIndex('subscriptions')].subjectFilter[0]]",
                            "requiresPreprocessing": false
                        }
                    }
                }
            }
        ]
    },
    "resources": [
        {
            "type": "Microsoft.ServiceBus/namespaces/topics/subscriptions/rules",
            "apiVersion": "[variables('apiVersion')]",
            "copy": {
                "name": "subscriptionFilters",
                "count": "[length(parameters('topics'))]"
            },
            "name": "[concat(variables('subscriptions')[copyIndex()].name, '/subject')]",
            "properties": "[union(variables('subscriptions')[copyIndex()].filterProperties, if(startsWith(variables('subscriptions')[copyIndex()].filterProperties.filterType, 'sql'), variables('subscriptions')[copyIndex()].sqlFilterProperties, variables('subscriptions')[copyIndex()].correlationFilterProperties) )]"
        }
    ]
}

字符串

jtjikinw

jtjikinw1#

您可以根据filter类型有条件地分配筛选器属性,而不是直接使用union方法。使用equals函数将滤波器类型与'correlationFilter''sqlFilter'进行比较。

  • 您可以对代码进行下面列出的更改,然后再次尝试部署 *。
"correlationFilter":  "[if(equals(variables('subscriptions')[copyIndex()].filterProperties.filterType, 'correlationFilter'), variables('subscriptions')[copyIndex()].correlationFilterProperties.correlationFilter)]",  
"sqlFilter":  "[if(equals(variables('subscriptions')[copyIndex()].filterProperties.filterType, 'sqlFilter'), variables('subscriptions')[copyIndex()].sqlFilterProperties.sqlFilter)]"

字符串
或者,在使用CorrelationRuleFilter默认构造函数时,还可以使用系统属性(如(ContentType、Label、MessageId、ReplyTo)和用户定义的值进行筛选。使用IDictionary string, object >属性Properties为相关性过滤器提供用户定义的属性,如详细的here

omqzjyyz

omqzjyyz2#

我通过一张支持票从微软得到了这个答案。问题是subject元数据属性在创建过滤器时实际上被称为label。(在C#中,它被称为Subject。)因此,只需将sqlExpression更改为使用sys.Label,并将correlationFilter属性更改为使用Label就可以解决这个问题。
参见文档
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messages-payloads
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.servicebus.message.label?view=azure-dotnet

相关问题