datadog_api_client/datadogV2/model/
model_change_event_custom_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/// Change event attributes.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct ChangeEventCustomAttributes {
14    /// The entity that made the change. Optional, if provided it must include `type` and `name`.
15    #[serde(rename = "author")]
16    pub author: Option<crate::datadogV2::model::ChangeEventCustomAttributesAuthor>,
17    /// Free form JSON object with information related to the `change` event. Supports up to 100 properties per object and a maximum nesting depth of 10 levels.
18    #[serde(rename = "change_metadata")]
19    pub change_metadata: Option<std::collections::BTreeMap<String, serde_json::Value>>,
20    /// A uniquely identified resource.
21    #[serde(rename = "changed_resource")]
22    pub changed_resource: crate::datadogV2::model::ChangeEventCustomAttributesChangedResource,
23    /// A list of resources impacted by this change. It is recommended to provide an impacted resource to display
24    /// the change event at the correct location. Only resources of type `service` are supported. Maximum of 100 impacted resources allowed.
25    #[serde(rename = "impacted_resources")]
26    pub impacted_resources:
27        Option<Vec<crate::datadogV2::model::ChangeEventCustomAttributesImpactedResourcesItems>>,
28    /// Free form JSON object representing the new state of the changed resource.
29    #[serde(rename = "new_value")]
30    pub new_value: Option<std::collections::BTreeMap<String, serde_json::Value>>,
31    /// Free form JSON object representing the previous state of the changed resource.
32    #[serde(rename = "prev_value")]
33    pub prev_value: Option<std::collections::BTreeMap<String, serde_json::Value>>,
34    #[serde(skip)]
35    #[serde(default)]
36    pub(crate) _unparsed: bool,
37}
38
39impl ChangeEventCustomAttributes {
40    pub fn new(
41        changed_resource: crate::datadogV2::model::ChangeEventCustomAttributesChangedResource,
42    ) -> ChangeEventCustomAttributes {
43        ChangeEventCustomAttributes {
44            author: None,
45            change_metadata: None,
46            changed_resource,
47            impacted_resources: None,
48            new_value: None,
49            prev_value: None,
50            _unparsed: false,
51        }
52    }
53
54    pub fn author(
55        mut self,
56        value: crate::datadogV2::model::ChangeEventCustomAttributesAuthor,
57    ) -> Self {
58        self.author = Some(value);
59        self
60    }
61
62    pub fn change_metadata(
63        mut self,
64        value: std::collections::BTreeMap<String, serde_json::Value>,
65    ) -> Self {
66        self.change_metadata = Some(value);
67        self
68    }
69
70    pub fn impacted_resources(
71        mut self,
72        value: Vec<crate::datadogV2::model::ChangeEventCustomAttributesImpactedResourcesItems>,
73    ) -> Self {
74        self.impacted_resources = Some(value);
75        self
76    }
77
78    pub fn new_value(
79        mut self,
80        value: std::collections::BTreeMap<String, serde_json::Value>,
81    ) -> Self {
82        self.new_value = Some(value);
83        self
84    }
85
86    pub fn prev_value(
87        mut self,
88        value: std::collections::BTreeMap<String, serde_json::Value>,
89    ) -> Self {
90        self.prev_value = Some(value);
91        self
92    }
93}
94
95impl<'de> Deserialize<'de> for ChangeEventCustomAttributes {
96    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
97    where
98        D: Deserializer<'de>,
99    {
100        struct ChangeEventCustomAttributesVisitor;
101        impl<'a> Visitor<'a> for ChangeEventCustomAttributesVisitor {
102            type Value = ChangeEventCustomAttributes;
103
104            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
105                f.write_str("a mapping")
106            }
107
108            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
109            where
110                M: MapAccess<'a>,
111            {
112                let mut author: Option<crate::datadogV2::model::ChangeEventCustomAttributesAuthor> =
113                    None;
114                let mut change_metadata: Option<
115                    std::collections::BTreeMap<String, serde_json::Value>,
116                > = None;
117                let mut changed_resource: Option<
118                    crate::datadogV2::model::ChangeEventCustomAttributesChangedResource,
119                > = None;
120                let mut impacted_resources: Option<
121                    Vec<crate::datadogV2::model::ChangeEventCustomAttributesImpactedResourcesItems>,
122                > = None;
123                let mut new_value: Option<std::collections::BTreeMap<String, serde_json::Value>> =
124                    None;
125                let mut prev_value: Option<std::collections::BTreeMap<String, serde_json::Value>> =
126                    None;
127                let mut _unparsed = false;
128
129                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
130                    match k.as_str() {
131                        "author" => {
132                            if v.is_null() {
133                                continue;
134                            }
135                            author = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
136                        }
137                        "change_metadata" => {
138                            if v.is_null() {
139                                continue;
140                            }
141                            change_metadata =
142                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
143                        }
144                        "changed_resource" => {
145                            changed_resource =
146                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
147                        }
148                        "impacted_resources" => {
149                            if v.is_null() {
150                                continue;
151                            }
152                            impacted_resources =
153                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
154                        }
155                        "new_value" => {
156                            if v.is_null() {
157                                continue;
158                            }
159                            new_value = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
160                        }
161                        "prev_value" => {
162                            if v.is_null() {
163                                continue;
164                            }
165                            prev_value = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
166                        }
167                        &_ => {
168                            return Err(serde::de::Error::custom(
169                                "Additional properties not allowed",
170                            ));
171                        }
172                    }
173                }
174                let changed_resource =
175                    changed_resource.ok_or_else(|| M::Error::missing_field("changed_resource"))?;
176
177                let content = ChangeEventCustomAttributes {
178                    author,
179                    change_metadata,
180                    changed_resource,
181                    impacted_resources,
182                    new_value,
183                    prev_value,
184                    _unparsed,
185                };
186
187                Ok(content)
188            }
189        }
190
191        deserializer.deserialize_any(ChangeEventCustomAttributesVisitor)
192    }
193}