我想添加一个条件,仅当给出时才允许我添加自定义速率限制(
local.limit == true
),否则不要将其添加到政策中。当提供速率限制时,它工作正常,但如果没有速率限制,则会出现错误
“calls”属性无效 - 值“”不在允许范围内 值范围。
根据 https://learn.microsoft.com/en-us/azure/api-management/rate-limit-policy 调用是必填字段。
variable "rate_limit" {
type = string
}
locals {
j = "context.Request.Headers.GetValueOrDefault(\"Authorization\", \"\").AsJwt()?.Subject"
s = "context.Subscription.Id"
limit = var.rate_limit != ""
}
<choose>
<when condition="@((${local.j} != null || ${local.s} != null) && (${local.limit == true}))">
<rate-limit-by-key calls="${var.rate_limit}" renewal-period="60" counter-key="@(${local.j} ?? ${local.s})" />
</when>
<when condition="@(context.Request.Headers.GetValueOrDefault("ApiKey") == "Default")">
<rate-limit-by-key calls="100" renewal-period="60" counter-key="@(context.Request.IpAddress)" />
</when>
</choose>
计划看起来像这样
<when condition="@((context.Request.Headers.GetValueOrDefault("Authorization", "").AsJwt()?.Subject != null || context.Subscription.Id != null) && (false))">
<rate-limit-by-key calls="" renewal-period="60" counter-key="@(context.Request.Headers.GetValueOrDefault("Authorization", "").AsJwt()?.Subject ?? context.Subscription.Id)" />
</when>
我成功修复了它
locals {
j = "context.Request.Headers.GetValueOrDefault(\"Authorization\", \"\").AsJwt()?.Subject"
s = "context.Subscription.Id"
limit = var.rate_limit != ""
rate_limit_config = <<XML
<when condition="@(${local.j} != null || ${local.s} != null && ${local.limit})">
<rate-limit-by-key calls="${var.rate_limit}" renewal-period="60" counter-key="@(${local.j} ?? ${local.s})" />
</when>
XML
}
<choose>
${var.api.rate_limit != "" ? local.rate_limit_config : var.api.rate_limit}
<when condition="@(context.Request.Headers.GetValueOrDefault("ApiKey") == "Default")">
<rate-limit-by-key calls="10" renewal-period="1" counter-key="@(context.Request.IpAddress)" />
</when>