azure_functions/bindings/
event_grid_event.rs

1use crate::{
2    rpc::{typed_data::Data, TypedData},
3    util::deserialize_datetime,
4};
5use chrono::{DateTime, Utc};
6use serde::Deserialize;
7use serde_json::from_str;
8use std::collections::HashMap;
9
10/// Represents an Event Grid trigger binding.
11///
12/// The following binding attributes are supported:
13///
14/// | Name   | Description                            |
15/// |--------|----------------------------------------|
16/// | `name` | The name of the parameter being bound. |
17///
18/// # Examples
19///
20/// ```rust
21/// use azure_functions::{
22///     bindings::EventGridEvent,
23///     func,
24/// };
25/// use log::info;
26///
27/// #[func]
28/// pub fn log_event(event: EventGridEvent) {
29///     log::info!("Event Data: {}", event.data);
30/// }
31/// ```
32#[derive(Debug, Deserialize)]
33#[serde(rename_all = "camelCase")]
34pub struct EventGridEvent {
35    /// Full resource path to the event source.
36    pub topic: String,
37    /// Publisher-defined path to the event subject.
38    pub subject: String,
39    /// One of the registered event types for this event source.
40    pub event_type: String,
41    /// The time the event is generated based on the provider's UTC time.
42    #[serde(deserialize_with = "deserialize_datetime")]
43    pub event_time: DateTime<Utc>,
44    /// Unique identifier for the event.
45    pub id: String,
46    /// Event data specific to the resource provider.
47    pub data: serde_json::Value,
48    /// The schema version of the data object.
49    pub data_version: String,
50    /// The schema version of the event metadata.
51    pub metadata_version: String,
52}
53
54impl EventGridEvent {
55    #[doc(hidden)]
56    pub fn new(data: TypedData, _: HashMap<String, TypedData>) -> Self {
57        match data.data {
58            Some(Data::Json(s)) => from_str(&s).expect("failed to parse Event Grid JSON"),
59            _ => panic!("expected JSON data for Event Grid trigger binding"),
60        }
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn it_constructs() {
70        const EVENT: &'static str = r#"{"topic":"/subscriptions/{subscription-id}/resourceGroups/Storage/providers/Microsoft.Storage/storageAccounts/xstoretestaccount","subject":"/blobServices/default/containers/oc2d2817345i200097container/blobs/oc2d2817345i20002296blob","eventType":"Microsoft.Storage.BlobCreated","eventTime":"2017-06-26T18:41:00.9584103Z","id":"831e1650-001e-001b-66ab-eeb76e069631","data":{"api":"PutBlockList","clientRequestId":"6d79dbfb-0e37-4fc4-981f-442c9ca65760","requestId":"831e1650-001e-001b-66ab-eeb76e000000","eTag":"0x8D4BCC2E4835CD0","contentType":"application/octet-stream","contentLength":524288,"blobType":"BlockBlob","url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob","sequencer":"00000000000004420000000000028963","storageDiagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}},"dataVersion":"1","metadataVersion":"1"}"#;
71
72        let data = TypedData {
73            data: Some(Data::Json(EVENT.to_string())),
74        };
75
76        let event = EventGridEvent::new(data, HashMap::new());
77        assert_eq!(event.topic, "/subscriptions/{subscription-id}/resourceGroups/Storage/providers/Microsoft.Storage/storageAccounts/xstoretestaccount");
78        assert_eq!(event.subject, "/blobServices/default/containers/oc2d2817345i200097container/blobs/oc2d2817345i20002296blob");
79        assert_eq!(event.event_type, "Microsoft.Storage.BlobCreated");
80        assert_eq!(
81            event.event_time.to_rfc3339(),
82            "2017-06-26T18:41:00.958410300+00:00"
83        );
84        assert_eq!(event.id, "831e1650-001e-001b-66ab-eeb76e069631");
85        assert_eq!(
86            event.data.to_string(),
87            r#"{"api":"PutBlockList","blobType":"BlockBlob","clientRequestId":"6d79dbfb-0e37-4fc4-981f-442c9ca65760","contentLength":524288,"contentType":"application/octet-stream","eTag":"0x8D4BCC2E4835CD0","requestId":"831e1650-001e-001b-66ab-eeb76e000000","sequencer":"00000000000004420000000000028963","storageDiagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"},"url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob"}"#
88        );
89        assert_eq!(event.data_version, "1");
90        assert_eq!(event.metadata_version, "1");
91    }
92}