无法使用“For each”循环和“items()”函数在Azure Logic应用程序中处理多个事件

eyh26e7m  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在开发一个Azure逻辑应用程序来处理Azure Key Vault上的几个事件(即将到期,过期)的通知。我的设置涉及到每个密钥库的事件网格,所有事件都发送到同一个逻辑应用程序。
当事件网格将事件发送到逻辑应用程序时,它们以事件数组的形式出现。我试图在我的Logic应用程序中使用'For each'循环来单独处理这些事件。以下是我的Logic应用程序的相关部分:
我有一个“当收到HTTP请求时”触发器,然后是一个“For each”循环,其中triggerBody()作为输入,在循环中,我有一个“发送电子邮件(V2)”动作。在'Send an email(V2)'操作中,我尝试使用items()函数访问每个事件的属性,如下所示:

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']}

但是当我尝试保存Logic应用程序时,我收到以下错误消息:
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.".'.
我已经查看了文档,并了解到items()在'For each'循环中使用时不需要参数,因为它会自动引用'For each'循环遍历的数组的当前项。
我做错了什么?如何设置我的Logic应用程序,使用“For each”循环和items()函数分别正确处理每个事件?
下面是我的schema输入:

{
    "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"
    }
}

输出示例:

[
    {
        "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"
    }
]
slmsl1lt

slmsl1lt1#

我尝试通过对2个密钥库使用一个逻辑应用程序,并为每个密钥库使用单独的事件网格来再现该问题,订阅多个事件类型-

这两个密钥存储库向单个逻辑应用发布事件-

在“从前面的步骤中选择一个输出”中,我取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']}

请求正文-

{
"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"
}
]

我收到所有订阅事件类型的电子邮件通知-

相关问题