英文:
Unable to process multiple events in Azure Logic App with 'For each' loop and 'items()' function
问题
我正在开发一个Azure Logic App,用于处理Azure Key Vaults上的多个事件(临近到期、已过期)的通知。我的设置涉及为每个Key Vault 都创建一个 Event Grid,并将它们的事件发送到同一个 Logic App。
当 Event Grid 将事件发送到 Logic App 时,它们会以事件数组的形式到达。我正在尝试在我的 Logic App 中使用 'For each' 循环来逐个处理这些事件。以下是我的 Logic App 的相关部分:
- 我有一个 '当收到 HTTP 请求' 触发器,
- 然后是一个 'For each' 循环,使用 triggerBody() 作为输入,
- 在循环内部,我有一个 '发送电子邮件(V2)' 操作。
- 在 '发送电子邮件(V2)' 操作中,我正在尝试使用 items() 函数访问每个事件的属性,就像这样:
事件类型:@{items()?['eventType']}
Key Vault 名称:@{items()?['data']?['VaultName']}
对象类型:@{items()?['data']?['ObjectType']}
对象名称:@{items()?['data']?['ObjectName']}
到期日期:@{items()?['data']?['EXP']}
但当我尝试保存 Logic App 时,我收到以下错误消息:
无法保存逻辑应用。模板验证失败:“Send_an_email_(V2)”行“1”,列“1679”无效:“模板语言函数“items”必须至少有一个参数。”。
我已经查阅了文档并了解到,当在 'For each' 循环内使用 items() 时,它不需要参数,因为它会自动引用 'For each' 循环正在循环遍历的数组的当前项目。
我做错了什么?如何设置我的 Logic App 以正确使用 'For each' 循环和 items() 函数来逐个处理每个事件?
英文:
I am developing an Azure Logic App to handle notifications for several events (near expiry, expired) on Azure Key Vaults. My setup involves an Event Grid for each Key Vault, all sending their events to the same Logic App.
When Event Grid sends events to the Logic App, they come as an array of events. I am trying to use a 'For each' loop in my Logic App to process each of these events individually. Here's the relevant portion of my Logic App:
I have a 'When a HTTP request is received' trigger,
Followed by a 'For each' loop with triggerBody() as the input,
Inside the loop, I have a 'Send an email (V2)' action.
In the 'Send an email (V2)' action, I'm trying to access the properties of each event using the items() function like this:
Event Type: @{items()?['eventType']}
Key Vault Name: @{items()?['data']?['VaultName']}
Object Type: @{items()?['data']?['ObjectType']}
Object Name: @{items()?['data']?['ObjectName']}
Expiry Date: @{items()?['data']?['EXP']}
But when I try to save the Logic App, I get this error message:
Failed to save logic app. The template validation failed: 'The template action 'Send_an_email_(V2)' at line '1' and column '1679' is not valid: "The template language function 'items' must have at least one parameter.".'.
I have reviewed the documentation and understand that items() doesn't require a parameter when used inside a 'For each' loop, as it automatically refers to the current item of the array that 'For each' is looping through.
What am I doing wrong? How can I set up my Logic App to correctly process each event individually using a 'For each' loop and the items() function?
Below is my schema input:
{
"schema": {
"properties": {
"$schema": {
"type": "string"
},
"properties": {
"properties": {
"data": {
"properties": {
"properties": {
"properties": {
"EXP": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"Id": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"NBF": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"ObjectName": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"ObjectType": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"VaultName": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"Version": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"required": {
"items": {
"type": "string"
},
"type": "array"
},
"type": {
"type": "string"
}
},
"type": "object"
},
"dataVersion": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"eventTime": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"eventType": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"id": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"metadataVersion": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"subject": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
},
"topic": {
"properties": {
"type": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"required": {
"items": {
"type": "string"
},
"type": "array"
},
"type": {
"type": "string"
}
},
"type": "object"
}
}
sample of the output:
[
{
"id": "5a",
"topic": "/subscriptions/8",
"subject": "kv",
"eventType": "Microsoft.KeyVault.SecretNearExpiry",
"data": {
"Id": "https://kv",
"VaultName": "kv-v",
"ObjectType": "Secret",
"ObjectName": "kv",
"Version": "",
"NBF": null,
"EXP": "2023-06-13T00:00:00Z"
},
"dataVersion": "1",
"metadataVersion": "1",
"eventTime": "2023-06-12T00:00:00Z"
}
]
答案1
得分: 1
这两个密钥保管库分别订阅多个事件类型,发布事件到单个逻辑应用-**
这两个密钥保管库发布事件到单个逻辑应用-
在“从前面的步骤中选择输出”中,我正在使用 triggerBody()
。
事件 ID: @{items('For_each')?['id']},
事件类型: @{items('For_each')?['eventType']},
主题: @{items('For_each')?['subject']},
密钥保管库名称: @{items('For_each')?['data']?['VaultName']},
对象类型: @{items('For_each')?['data']?['ObjectType']},
对象名称: @{items('For_each')?['data']?['ObjectName']}
请求体-
{
"items": {
"properties": {
"data": {
"properties": {
"EXP": {
"type": "string"
},
"Id": {
"type": "string"
},
"NBF": {
"type": "string"
},
"ObjectName": {
"type": "string"
},
"ObjectType": {
"type": "string"
},
"VaultName": {
"type": "string"
},
"Version": {
"type": "string"
}
},
"type": "object"
},
"dataVersion": {
"type": "string"
},
"eventTime": {
"type": "string"
},
"eventType": {
"type": "string"
},
"id": {
"type": "string"
},
"metadataVersion": {
"type": "string"
},
"subject": {
"type": "string"
},
"topic": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
我得到的输出是-
[
{
"id": "0XXXXXXXXXXXXXXXX",
"topic": "/subscriptions/xxxxxxxxx/resourceGroups/iafrin-xxxx/providers/Microsoft.KeyVault/vaults/afreen-key-002",
"subject": "afreenSecret001",
"eventType": "Microsoft.KeyVault.SecretNearExpiry",
"data": {
"Id": "https://afreen-key-002.vault.azure.net/secrets/afreenSecret001/xxxxxxxxxx",
"VaultName": "afreen-key-002",
"ObjectType": "Secret",
"ObjectName": "afreenSecret001",
"Version": "yyyyyyyyyyy",
"NBF": null,
"EXP": "2023-06-14T05:20:00Z"
},
"dataVersion": "1",
"metadataVersion": "1",
"eventTime": "2023-06-14T05:19:56.9159226Z"
}
]
我会收到所有订阅事件类型的电子邮件通知-
英文:
I have tried to reproduce the issue by using one logic app for 2 key vaults and individual event grids for each key vaults subscribing multiple event types-
These two key vaults publishing events to single logic app-
Here in "Select an output from Previous steps", I am taking triggerBody()
.
Event Id: @{items('For_each')?['id']},
Event Type: @{items('For_each')?['eventType']},
Subject: @{items('For_each')?['subject']},
Key Vault Name: @{items('For_each')?['data']?['VaultName']},
Object Type: @{items('For_each')?['data']?['ObjectType']},
Object Name: @{items('For_each')?['data']?['ObjectName']}
Request Body-
{
"items": {
"properties": {
"data": {
"properties": {
"EXP": {
"type": "string"
},
"Id": {
"type": "string"
},
"NBF": {
"type": "string"
},
"ObjectName": {
"type": "string"
},
"ObjectType": {
"type": "string"
},
"VaultName": {
"type": "string"
},
"Version": {
"type": "string"
}
},
"type": "object"
},
"dataVersion": {
"type": "string"
},
"eventTime": {
"type": "string"
},
"eventType": {
"type": "string"
},
"id": {
"type": "string"
},
"metadataVersion": {
"type": "string"
},
"subject": {
"type": "string"
},
"topic": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
Output which I am getting is-
[
{
"id": "0XXXXXXXXXXXXXXXX",
"topic": "/subscriptions/xxxxxxxxx/resourceGroups/iafrin-xxxx/providers/Microsoft.KeyVault/vaults/afreen-key-002",
"subject": "afreenSecret001",
"eventType": "Microsoft.KeyVault.SecretNearExpiry",
"data": {
"Id": "https://afreen-key-002.vault.azure.net/secrets/afreenSecret001/xxxxxxxxxx",
"VaultName": "afreen-key-002",
"ObjectType": "Secret",
"ObjectName": "afreenSecret001",
"Version": "yyyyyyyyyyy",
"NBF": null,
"EXP": "2023-06-14T05:20:00Z"
},
"dataVersion": "1",
"metadataVersion": "1",
"eventTime": "2023-06-14T05:19:56.9159226Z"
}
]
I am getting email notification for all the subscribed event types-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论