datadog_api_client/datadogV2/model/
model_monitor_notification_rule_attributes.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/// Attributes of the monitor notification rule.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct MonitorNotificationRuleAttributes {
14    /// Filter used to associate the notification rule with monitors.
15    #[serde(rename = "filter")]
16    pub filter: Option<crate::datadogV2::model::MonitorNotificationRuleFilter>,
17    /// The name of the monitor notification rule.
18    #[serde(rename = "name")]
19    pub name: String,
20    /// A list of recipients to notify. Uses the same format as the monitor `message` field. Must not start with an '@'.
21    #[serde(rename = "recipients")]
22    pub recipients: Vec<String>,
23    #[serde(skip)]
24    #[serde(default)]
25    pub(crate) _unparsed: bool,
26}
27
28impl MonitorNotificationRuleAttributes {
29    pub fn new(name: String, recipients: Vec<String>) -> MonitorNotificationRuleAttributes {
30        MonitorNotificationRuleAttributes {
31            filter: None,
32            name,
33            recipients,
34            _unparsed: false,
35        }
36    }
37
38    pub fn filter(mut self, value: crate::datadogV2::model::MonitorNotificationRuleFilter) -> Self {
39        self.filter = Some(value);
40        self
41    }
42}
43
44impl<'de> Deserialize<'de> for MonitorNotificationRuleAttributes {
45    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
46    where
47        D: Deserializer<'de>,
48    {
49        struct MonitorNotificationRuleAttributesVisitor;
50        impl<'a> Visitor<'a> for MonitorNotificationRuleAttributesVisitor {
51            type Value = MonitorNotificationRuleAttributes;
52
53            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
54                f.write_str("a mapping")
55            }
56
57            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
58            where
59                M: MapAccess<'a>,
60            {
61                let mut filter: Option<crate::datadogV2::model::MonitorNotificationRuleFilter> =
62                    None;
63                let mut name: Option<String> = None;
64                let mut recipients: Option<Vec<String>> = None;
65                let mut _unparsed = false;
66
67                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
68                    match k.as_str() {
69                        "filter" => {
70                            if v.is_null() {
71                                continue;
72                            }
73                            filter = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
74                            if let Some(ref _filter) = filter {
75                                match _filter {
76                                    crate::datadogV2::model::MonitorNotificationRuleFilter::UnparsedObject(_filter) => {
77                                        _unparsed = true;
78                                    },
79                                    _ => {}
80                                }
81                            }
82                        }
83                        "name" => {
84                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
85                        }
86                        "recipients" => {
87                            recipients = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
88                        }
89                        &_ => {
90                            return Err(serde::de::Error::custom(
91                                "Additional properties not allowed",
92                            ));
93                        }
94                    }
95                }
96                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
97                let recipients = recipients.ok_or_else(|| M::Error::missing_field("recipients"))?;
98
99                let content = MonitorNotificationRuleAttributes {
100                    filter,
101                    name,
102                    recipients,
103                    _unparsed,
104                };
105
106                Ok(content)
107            }
108        }
109
110        deserializer.deserialize_any(MonitorNotificationRuleAttributesVisitor)
111    }
112}