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