datadog_api_client/datadogV1/model/
model_event_create_request.rs

1// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2// This product includes software developed at Datadog (https://www.datadoghq.com/).
3// Copyright 2019-Present Datadog, Inc.
4use serde::de::{Error, MapAccess, Visitor};
5use serde::{Deserialize, Deserializer, Serialize};
6use serde_with::skip_serializing_none;
7use std::fmt::{self, Formatter};
8
9/// Object representing an event.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct EventCreateRequest {
14    /// An arbitrary string to use for aggregation. Limited to 100 characters.
15    /// If you specify a key, all events using that key are grouped together in the Event Stream.
16    #[serde(rename = "aggregation_key")]
17    pub aggregation_key: Option<String>,
18    /// If an alert event is enabled, set its type.
19    /// For example, `error`, `warning`, `info`, `success`, `user_update`,
20    /// `recommendation`, and `snapshot`.
21    #[serde(rename = "alert_type")]
22    pub alert_type: Option<crate::datadogV1::model::EventAlertType>,
23    /// POSIX timestamp of the event. Must be sent as an integer (that is no quotes).
24    /// Limited to events no older than 18 hours
25    #[serde(rename = "date_happened")]
26    pub date_happened: Option<i64>,
27    /// A device name.
28    #[serde(rename = "device_name")]
29    pub device_name: Option<String>,
30    /// Host name to associate with the event.
31    /// Any tags associated with the host are also applied to this event.
32    #[serde(rename = "host")]
33    pub host: Option<String>,
34    /// The priority of the event. For example, `normal` or `low`.
35    #[serde(
36        rename = "priority",
37        default,
38        with = "::serde_with::rust::double_option"
39    )]
40    pub priority: Option<Option<crate::datadogV1::model::EventPriority>>,
41    /// ID of the parent event. Must be sent as an integer (that is no quotes).
42    #[serde(rename = "related_event_id")]
43    pub related_event_id: Option<i64>,
44    /// The type of event being posted. Option examples include nagios, hudson, jenkins, my_apps, chef, puppet, git, bitbucket, etc.
45    /// A complete list of source attribute values [available here](<https://docs.datadoghq.com/integrations/faq/list-of-api-source-attribute-value>).
46    #[serde(rename = "source_type_name")]
47    pub source_type_name: Option<String>,
48    /// A list of tags to apply to the event.
49    #[serde(rename = "tags")]
50    pub tags: Option<Vec<String>>,
51    /// The body of the event. Limited to 4000 characters. The text supports markdown.
52    /// To use markdown in the event text, start the text block with `%%% \n` and end the text block with `\n %%%`.
53    /// Use `msg_text` with the Datadog Ruby library.
54    #[serde(rename = "text")]
55    pub text: String,
56    /// The event title.
57    #[serde(rename = "title")]
58    pub title: String,
59    #[serde(flatten)]
60    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
61    #[serde(skip)]
62    #[serde(default)]
63    pub(crate) _unparsed: bool,
64}
65
66impl EventCreateRequest {
67    pub fn new(text: String, title: String) -> EventCreateRequest {
68        EventCreateRequest {
69            aggregation_key: None,
70            alert_type: None,
71            date_happened: None,
72            device_name: None,
73            host: None,
74            priority: None,
75            related_event_id: None,
76            source_type_name: None,
77            tags: None,
78            text,
79            title,
80            additional_properties: std::collections::BTreeMap::new(),
81            _unparsed: false,
82        }
83    }
84
85    pub fn aggregation_key(mut self, value: String) -> Self {
86        self.aggregation_key = Some(value);
87        self
88    }
89
90    pub fn alert_type(mut self, value: crate::datadogV1::model::EventAlertType) -> Self {
91        self.alert_type = Some(value);
92        self
93    }
94
95    pub fn date_happened(mut self, value: i64) -> Self {
96        self.date_happened = Some(value);
97        self
98    }
99
100    pub fn device_name(mut self, value: String) -> Self {
101        self.device_name = Some(value);
102        self
103    }
104
105    pub fn host(mut self, value: String) -> Self {
106        self.host = Some(value);
107        self
108    }
109
110    pub fn priority(mut self, value: Option<crate::datadogV1::model::EventPriority>) -> Self {
111        self.priority = Some(value);
112        self
113    }
114
115    pub fn related_event_id(mut self, value: i64) -> Self {
116        self.related_event_id = Some(value);
117        self
118    }
119
120    pub fn source_type_name(mut self, value: String) -> Self {
121        self.source_type_name = Some(value);
122        self
123    }
124
125    pub fn tags(mut self, value: Vec<String>) -> Self {
126        self.tags = Some(value);
127        self
128    }
129
130    pub fn additional_properties(
131        mut self,
132        value: std::collections::BTreeMap<String, serde_json::Value>,
133    ) -> Self {
134        self.additional_properties = value;
135        self
136    }
137}
138
139impl<'de> Deserialize<'de> for EventCreateRequest {
140    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
141    where
142        D: Deserializer<'de>,
143    {
144        struct EventCreateRequestVisitor;
145        impl<'a> Visitor<'a> for EventCreateRequestVisitor {
146            type Value = EventCreateRequest;
147
148            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
149                f.write_str("a mapping")
150            }
151
152            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
153            where
154                M: MapAccess<'a>,
155            {
156                let mut aggregation_key: Option<String> = None;
157                let mut alert_type: Option<crate::datadogV1::model::EventAlertType> = None;
158                let mut date_happened: Option<i64> = None;
159                let mut device_name: Option<String> = None;
160                let mut host: Option<String> = None;
161                let mut priority: Option<Option<crate::datadogV1::model::EventPriority>> = None;
162                let mut related_event_id: Option<i64> = None;
163                let mut source_type_name: Option<String> = None;
164                let mut tags: Option<Vec<String>> = None;
165                let mut text: Option<String> = None;
166                let mut title: Option<String> = None;
167                let mut additional_properties: std::collections::BTreeMap<
168                    String,
169                    serde_json::Value,
170                > = std::collections::BTreeMap::new();
171                let mut _unparsed = false;
172
173                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
174                    match k.as_str() {
175                        "aggregation_key" => {
176                            if v.is_null() {
177                                continue;
178                            }
179                            aggregation_key =
180                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
181                        }
182                        "alert_type" => {
183                            if v.is_null() {
184                                continue;
185                            }
186                            alert_type = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
187                            if let Some(ref _alert_type) = alert_type {
188                                match _alert_type {
189                                    crate::datadogV1::model::EventAlertType::UnparsedObject(
190                                        _alert_type,
191                                    ) => {
192                                        _unparsed = true;
193                                    }
194                                    _ => {}
195                                }
196                            }
197                        }
198                        "date_happened" => {
199                            if v.is_null() {
200                                continue;
201                            }
202                            date_happened =
203                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
204                        }
205                        "device_name" => {
206                            if v.is_null() {
207                                continue;
208                            }
209                            device_name =
210                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
211                        }
212                        "host" => {
213                            if v.is_null() {
214                                continue;
215                            }
216                            host = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
217                        }
218                        "priority" => {
219                            priority = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
220                            if let Some(ref _priority) = priority {
221                                match _priority {
222                                    Some(
223                                        crate::datadogV1::model::EventPriority::UnparsedObject(
224                                            _priority,
225                                        ),
226                                    ) => {
227                                        _unparsed = true;
228                                    }
229                                    _ => {}
230                                }
231                            }
232                        }
233                        "related_event_id" => {
234                            if v.is_null() {
235                                continue;
236                            }
237                            related_event_id =
238                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
239                        }
240                        "source_type_name" => {
241                            if v.is_null() {
242                                continue;
243                            }
244                            source_type_name =
245                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
246                        }
247                        "tags" => {
248                            if v.is_null() {
249                                continue;
250                            }
251                            tags = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
252                        }
253                        "text" => {
254                            text = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
255                        }
256                        "title" => {
257                            title = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
258                        }
259                        &_ => {
260                            if let Ok(value) = serde_json::from_value(v.clone()) {
261                                additional_properties.insert(k, value);
262                            }
263                        }
264                    }
265                }
266                let text = text.ok_or_else(|| M::Error::missing_field("text"))?;
267                let title = title.ok_or_else(|| M::Error::missing_field("title"))?;
268
269                let content = EventCreateRequest {
270                    aggregation_key,
271                    alert_type,
272                    date_happened,
273                    device_name,
274                    host,
275                    priority,
276                    related_event_id,
277                    source_type_name,
278                    tags,
279                    text,
280                    title,
281                    additional_properties,
282                    _unparsed,
283                };
284
285                Ok(content)
286            }
287        }
288
289        deserializer.deserialize_any(EventCreateRequestVisitor)
290    }
291}