我想使用terraform向Azure API管理API添加一个策略
resource "azurerm_eventhub" "my-event-hub" {
name = "my-logger"
namespace_name = azurerm_eventhub_namespace.my-events.name
resource_group_name = azurerm_resource_group.myresourcegroup.name
partition_count = 2
message_retention = 1
}
resource "azurerm_api_management_api_policy" "eventhub-policy" {
api_name = azurerm_api_management_api.my-apim.name
api_management_name = azurerm_api_management.my-api.name
resource_group_name = azurerm_resource_group.myresourcegroup.name
xml_content = <<XML
<policies>s
<inbound>
<log-to-eventhub logger-id="my-logger">@{
return new JObject(
#***** json objects here *********#
).ToString();
}</log-to-eventhub>
<base />
<set-backend-service base-url="https://my-stub.getsandbox.com" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
XML
}
我可以通过门户创建此策略。我可以使用terraform创建APIM实例和API,但是当我尝试创建策略时,我得到以下验证错误:
│Error:正在创建或更新API Policy…: apimanagement。APIPolicyClient#CreateOrUpdate: Failure responding to request: StatusCode=400——原始错误:autorest/azure:服务返回一个错误。状态= 400代码="ValidationError"消息="一个或多个字段包含错误的值:"detail =[{"code":"ValidationError","message":"第3行,第11列' Log -to- EventHub '元素错误:Log to- EventHub只接受azureEventHub"的Logger Type,"target"log-to-eventhub"}]
谁能建议我如何将策略与创建的事件中心匹配?
可以使用Set-AzApiManagementPolicy为API Management设置API作用域策略。
Set-AzApiManagementPolicy
-Context <PsApiManagementContext>
[-Format <String>]
-ApiId <String>
[-ApiRevision <String>]
-OperationId <String>
[-Policy <String>]
[-PolicyFilePath <String>]
[-PolicyUrl <String>]
[-PassThru]
[-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
$apimContext = New-AzApiManagementContext -ResourceGroupName "Api-Default-WestUS" -ServiceName "contoso"
Set-AzApiManagementPolicy -Context $apimContext -ApiId "9876543210" -Policy $PolicyString
你可以参考GitHub关闭的问题Set-AzureRmApiManagementPolicy不显示验证错误和打开的问题如何更新API策略?
其他参考:如何在Azure API管理中将事件记录到Azure事件中心,以及如何从powershell更新Azure API入站策略