datadog_api_client/datadogV2/model/
model_asset_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/// The JSON:API attributes of the asset.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct AssetAttributes {
14    /// Asset architecture.
15    #[serde(rename = "arch")]
16    pub arch: Option<String>,
17    /// List of environments where the asset is deployed.
18    #[serde(rename = "environments")]
19    pub environments: Vec<String>,
20    /// Asset name.
21    #[serde(rename = "name")]
22    pub name: String,
23    /// Asset operating system.
24    #[serde(rename = "operating_system")]
25    pub operating_system: Option<crate::datadogV2::model::AssetOperatingSystem>,
26    /// Asset risks.
27    #[serde(rename = "risks")]
28    pub risks: crate::datadogV2::model::AssetRisks,
29    /// List of teams that own the asset.
30    #[serde(rename = "teams")]
31    pub teams: Option<Vec<String>>,
32    /// The asset type
33    #[serde(rename = "type")]
34    pub type_: crate::datadogV2::model::AssetType,
35    /// Asset version.
36    #[serde(rename = "version")]
37    pub version: Option<crate::datadogV2::model::AssetVersion>,
38    #[serde(flatten)]
39    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
40    #[serde(skip)]
41    #[serde(default)]
42    pub(crate) _unparsed: bool,
43}
44
45impl AssetAttributes {
46    pub fn new(
47        environments: Vec<String>,
48        name: String,
49        risks: crate::datadogV2::model::AssetRisks,
50        type_: crate::datadogV2::model::AssetType,
51    ) -> AssetAttributes {
52        AssetAttributes {
53            arch: None,
54            environments,
55            name,
56            operating_system: None,
57            risks,
58            teams: None,
59            type_,
60            version: None,
61            additional_properties: std::collections::BTreeMap::new(),
62            _unparsed: false,
63        }
64    }
65
66    pub fn arch(mut self, value: String) -> Self {
67        self.arch = Some(value);
68        self
69    }
70
71    pub fn operating_system(
72        mut self,
73        value: crate::datadogV2::model::AssetOperatingSystem,
74    ) -> Self {
75        self.operating_system = Some(value);
76        self
77    }
78
79    pub fn teams(mut self, value: Vec<String>) -> Self {
80        self.teams = Some(value);
81        self
82    }
83
84    pub fn version(mut self, value: crate::datadogV2::model::AssetVersion) -> Self {
85        self.version = Some(value);
86        self
87    }
88
89    pub fn additional_properties(
90        mut self,
91        value: std::collections::BTreeMap<String, serde_json::Value>,
92    ) -> Self {
93        self.additional_properties = value;
94        self
95    }
96}
97
98impl<'de> Deserialize<'de> for AssetAttributes {
99    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
100    where
101        D: Deserializer<'de>,
102    {
103        struct AssetAttributesVisitor;
104        impl<'a> Visitor<'a> for AssetAttributesVisitor {
105            type Value = AssetAttributes;
106
107            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
108                f.write_str("a mapping")
109            }
110
111            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
112            where
113                M: MapAccess<'a>,
114            {
115                let mut arch: Option<String> = None;
116                let mut environments: Option<Vec<String>> = None;
117                let mut name: Option<String> = None;
118                let mut operating_system: Option<crate::datadogV2::model::AssetOperatingSystem> =
119                    None;
120                let mut risks: Option<crate::datadogV2::model::AssetRisks> = None;
121                let mut teams: Option<Vec<String>> = None;
122                let mut type_: Option<crate::datadogV2::model::AssetType> = None;
123                let mut version: Option<crate::datadogV2::model::AssetVersion> = None;
124                let mut additional_properties: std::collections::BTreeMap<
125                    String,
126                    serde_json::Value,
127                > = std::collections::BTreeMap::new();
128                let mut _unparsed = false;
129
130                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
131                    match k.as_str() {
132                        "arch" => {
133                            if v.is_null() {
134                                continue;
135                            }
136                            arch = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
137                        }
138                        "environments" => {
139                            environments =
140                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
141                        }
142                        "name" => {
143                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
144                        }
145                        "operating_system" => {
146                            if v.is_null() {
147                                continue;
148                            }
149                            operating_system =
150                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
151                        }
152                        "risks" => {
153                            risks = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
154                        }
155                        "teams" => {
156                            if v.is_null() {
157                                continue;
158                            }
159                            teams = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
160                        }
161                        "type" => {
162                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
163                            if let Some(ref _type_) = type_ {
164                                match _type_ {
165                                    crate::datadogV2::model::AssetType::UnparsedObject(_type_) => {
166                                        _unparsed = true;
167                                    }
168                                    _ => {}
169                                }
170                            }
171                        }
172                        "version" => {
173                            if v.is_null() {
174                                continue;
175                            }
176                            version = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
177                        }
178                        &_ => {
179                            if let Ok(value) = serde_json::from_value(v.clone()) {
180                                additional_properties.insert(k, value);
181                            }
182                        }
183                    }
184                }
185                let environments =
186                    environments.ok_or_else(|| M::Error::missing_field("environments"))?;
187                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
188                let risks = risks.ok_or_else(|| M::Error::missing_field("risks"))?;
189                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
190
191                let content = AssetAttributes {
192                    arch,
193                    environments,
194                    name,
195                    operating_system,
196                    risks,
197                    teams,
198                    type_,
199                    version,
200                    additional_properties,
201                    _unparsed,
202                };
203
204                Ok(content)
205            }
206        }
207
208        deserializer.deserialize_any(AssetAttributesVisitor)
209    }
210}