1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use relay_protocol::{Annotated, Empty, FromValue, IntoValue, Object, Value};

use crate::processor::ProcessValue;

/// Cloud Resource Context.
///
/// This context describes the cloud resource the event originated from.
///
/// Example:
///
/// ```json
/// "cloud_resource": {
///     "cloud.account.id": "499517922981",
///     "cloud.provider": "aws",
///     "cloud.platform": "aws_ec2",
///     "cloud.region": "us-east-1",
///     "cloud.vavailability_zone": "us-east-1e",
///     "host.id": "i-07d3301208fe0a55a",
///     "host.type": "t2.large"
/// }
/// ```
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
pub struct CloudResourceContext {
    /// The cloud account ID the resource is assigned to.
    #[metastructure(pii = "maybe")]
    #[metastructure(field = "cloud.account.id")]
    pub cloud_account_id: Annotated<String>,

    /// Name of the cloud provider.
    #[metastructure(field = "cloud.provider")]
    pub cloud_provider: Annotated<String>,

    /// The cloud platform in use.
    /// The prefix of the service SHOULD match the one specified in cloud_provider.
    #[metastructure(field = "cloud.platform")]
    pub cloud_platform: Annotated<String>,

    /// The geographical region the resource is running.
    #[metastructure(field = "cloud.region")]
    pub cloud_region: Annotated<String>,

    /// The zone where the resource is running.
    #[metastructure(field = "cloud.availability_zone")]
    pub cloud_availability_zone: Annotated<String>,

    /// Unique host ID.
    #[metastructure(pii = "maybe")]
    #[metastructure(field = "host.id")]
    pub host_id: Annotated<String>,

    /// Machine type of the host.
    #[metastructure(field = "host.type")]
    pub host_type: Annotated<String>,

    /// Additional arbitrary fields for forwards compatibility.
    #[metastructure(additional_properties, retain = "true", pii = "maybe")]
    pub other: Object<Value>,
}

impl super::DefaultContext for CloudResourceContext {
    fn default_key() -> &'static str {
        "cloud_resource"
    }

    fn from_context(context: super::Context) -> Option<Self> {
        match context {
            super::Context::CloudResource(c) => Some(*c),
            _ => None,
        }
    }

    fn cast(context: &super::Context) -> Option<&Self> {
        match context {
            super::Context::CloudResource(c) => Some(c),
            _ => None,
        }
    }

    fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
        match context {
            super::Context::CloudResource(c) => Some(c),
            _ => None,
        }
    }

    fn into_context(self) -> super::Context {
        super::Context::CloudResource(Box::new(self))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::Context;

    #[test]
    fn test_cloud_resource_context_roundtrip() {
        let json = r#"{
  "cloud.account.id": "499517922981",
  "cloud.provider": "aws",
  "cloud.platform": "aws_ec2",
  "cloud.region": "us-east-1",
  "cloud.availability_zone": "us-east-1e",
  "host.id": "i-07d3301208fe0a55a",
  "host.type": "t2.large",
  "other": "value",
  "type": "cloudresource"
}"#;
        let context = Annotated::new(Context::CloudResource(Box::new(CloudResourceContext {
            cloud_account_id: Annotated::new("499517922981".into()),
            cloud_provider: Annotated::new("aws".into()),
            cloud_platform: Annotated::new("aws_ec2".into()),
            cloud_region: Annotated::new("us-east-1".into()),
            cloud_availability_zone: Annotated::new("us-east-1e".into()),
            host_id: Annotated::new("i-07d3301208fe0a55a".into()),
            host_type: Annotated::new("t2.large".into()),
            other: {
                let mut map = Object::new();
                map.insert(
                    "other".to_string(),
                    Annotated::new(Value::String("value".to_string())),
                );
                map
            },
        })));

        assert_eq!(context, Annotated::from_json(json).unwrap());
        assert_eq!(json, context.to_json_pretty().unwrap());
    }
}