datadog_api_client/datadogV1/model/
model_service_level_objective.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/// A service level objective object includes a service level indicator, thresholds
10/// for one or more timeframes, and metadata (`name`, `description`, `tags`, etc.).
11#[non_exhaustive]
12#[skip_serializing_none]
13#[derive(Clone, Debug, PartialEq, Serialize)]
14pub struct ServiceLevelObjective {
15    /// Creation timestamp (UNIX time in seconds)
16    ///
17    /// Always included in service level objective responses.
18    #[serde(rename = "created_at")]
19    pub created_at: Option<i64>,
20    /// Object describing the creator of the shared element.
21    #[serde(rename = "creator")]
22    pub creator: Option<crate::datadogV1::model::Creator>,
23    /// A user-defined description of the service level objective.
24    ///
25    /// Always included in service level objective responses (but may be `null`).
26    /// Optional in create/update requests.
27    #[serde(
28        rename = "description",
29        default,
30        with = "::serde_with::rust::double_option"
31    )]
32    pub description: Option<Option<String>>,
33    /// A list of (up to 100) monitor groups that narrow the scope of a monitor service level objective.
34    ///
35    /// Included in service level objective responses if it is not empty. Optional in
36    /// create/update requests for monitor service level objectives, but may only be
37    /// used when then length of the `monitor_ids` field is one.
38    #[serde(rename = "groups")]
39    pub groups: Option<Vec<String>>,
40    /// A unique identifier for the service level objective object.
41    ///
42    /// Always included in service level objective responses.
43    #[serde(rename = "id")]
44    pub id: Option<String>,
45    /// Modification timestamp (UNIX time in seconds)
46    ///
47    /// Always included in service level objective responses.
48    #[serde(rename = "modified_at")]
49    pub modified_at: Option<i64>,
50    /// A list of monitor ids that defines the scope of a monitor service level
51    /// objective. **Required if type is `monitor`**.
52    #[serde(rename = "monitor_ids")]
53    pub monitor_ids: Option<Vec<i64>>,
54    /// The union of monitor tags for all monitors referenced by the `monitor_ids`
55    /// field.
56    /// Always included in service level objective responses for monitor-based service level
57    /// objectives (but may be empty). Ignored in create/update requests. Does not
58    /// affect which monitors are included in the service level objective (that is
59    /// determined entirely by the `monitor_ids` field).
60    #[serde(rename = "monitor_tags")]
61    pub monitor_tags: Option<Vec<String>>,
62    /// The name of the service level objective object.
63    #[serde(rename = "name")]
64    pub name: String,
65    /// A metric-based SLO. **Required if type is `metric`**. Note that Datadog only allows the sum by aggregator
66    /// to be used because this will sum up all request counts instead of averaging them, or taking the max or
67    /// min of all of those requests.
68    #[serde(rename = "query")]
69    pub query: Option<crate::datadogV1::model::ServiceLevelObjectiveQuery>,
70    /// A generic SLI specification. This is currently used for time-slice SLOs only.
71    #[serde(rename = "sli_specification")]
72    pub sli_specification: Option<crate::datadogV1::model::SLOSliSpec>,
73    /// A list of tags associated with this service level objective.
74    /// Always included in service level objective responses (but may be empty).
75    /// Optional in create/update requests.
76    #[serde(rename = "tags")]
77    pub tags: Option<Vec<String>>,
78    /// The target threshold such that when the service level indicator is above this
79    /// threshold over the given timeframe, the objective is being met.
80    #[serde(rename = "target_threshold")]
81    pub target_threshold: Option<f64>,
82    /// The thresholds (timeframes and associated targets) for this service level
83    /// objective object.
84    #[serde(rename = "thresholds")]
85    pub thresholds: Vec<crate::datadogV1::model::SLOThreshold>,
86    /// The SLO time window options. Note that "custom" is not a valid option for creating
87    /// or updating SLOs. It is only used when querying SLO history over custom timeframes.
88    #[serde(rename = "timeframe")]
89    pub timeframe: Option<crate::datadogV1::model::SLOTimeframe>,
90    /// The type of the service level objective.
91    #[serde(rename = "type")]
92    pub type_: crate::datadogV1::model::SLOType,
93    /// The optional warning threshold such that when the service level indicator is
94    /// below this value for the given threshold, but above the target threshold, the
95    /// objective appears in a "warning" state. This value must be greater than the target
96    /// threshold.
97    #[serde(rename = "warning_threshold")]
98    pub warning_threshold: Option<f64>,
99    #[serde(flatten)]
100    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
101    #[serde(skip)]
102    #[serde(default)]
103    pub(crate) _unparsed: bool,
104}
105
106impl ServiceLevelObjective {
107    pub fn new(
108        name: String,
109        thresholds: Vec<crate::datadogV1::model::SLOThreshold>,
110        type_: crate::datadogV1::model::SLOType,
111    ) -> ServiceLevelObjective {
112        ServiceLevelObjective {
113            created_at: None,
114            creator: None,
115            description: None,
116            groups: None,
117            id: None,
118            modified_at: None,
119            monitor_ids: None,
120            monitor_tags: None,
121            name,
122            query: None,
123            sli_specification: None,
124            tags: None,
125            target_threshold: None,
126            thresholds,
127            timeframe: None,
128            type_,
129            warning_threshold: None,
130            additional_properties: std::collections::BTreeMap::new(),
131            _unparsed: false,
132        }
133    }
134
135    pub fn created_at(mut self, value: i64) -> Self {
136        self.created_at = Some(value);
137        self
138    }
139
140    pub fn creator(mut self, value: crate::datadogV1::model::Creator) -> Self {
141        self.creator = Some(value);
142        self
143    }
144
145    pub fn description(mut self, value: Option<String>) -> Self {
146        self.description = Some(value);
147        self
148    }
149
150    pub fn groups(mut self, value: Vec<String>) -> Self {
151        self.groups = Some(value);
152        self
153    }
154
155    pub fn id(mut self, value: String) -> Self {
156        self.id = Some(value);
157        self
158    }
159
160    pub fn modified_at(mut self, value: i64) -> Self {
161        self.modified_at = Some(value);
162        self
163    }
164
165    pub fn monitor_ids(mut self, value: Vec<i64>) -> Self {
166        self.monitor_ids = Some(value);
167        self
168    }
169
170    pub fn monitor_tags(mut self, value: Vec<String>) -> Self {
171        self.monitor_tags = Some(value);
172        self
173    }
174
175    pub fn query(mut self, value: crate::datadogV1::model::ServiceLevelObjectiveQuery) -> Self {
176        self.query = Some(value);
177        self
178    }
179
180    pub fn sli_specification(mut self, value: crate::datadogV1::model::SLOSliSpec) -> Self {
181        self.sli_specification = Some(value);
182        self
183    }
184
185    pub fn tags(mut self, value: Vec<String>) -> Self {
186        self.tags = Some(value);
187        self
188    }
189
190    pub fn target_threshold(mut self, value: f64) -> Self {
191        self.target_threshold = Some(value);
192        self
193    }
194
195    pub fn timeframe(mut self, value: crate::datadogV1::model::SLOTimeframe) -> Self {
196        self.timeframe = Some(value);
197        self
198    }
199
200    pub fn warning_threshold(mut self, value: f64) -> Self {
201        self.warning_threshold = Some(value);
202        self
203    }
204
205    pub fn additional_properties(
206        mut self,
207        value: std::collections::BTreeMap<String, serde_json::Value>,
208    ) -> Self {
209        self.additional_properties = value;
210        self
211    }
212}
213
214impl<'de> Deserialize<'de> for ServiceLevelObjective {
215    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
216    where
217        D: Deserializer<'de>,
218    {
219        struct ServiceLevelObjectiveVisitor;
220        impl<'a> Visitor<'a> for ServiceLevelObjectiveVisitor {
221            type Value = ServiceLevelObjective;
222
223            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
224                f.write_str("a mapping")
225            }
226
227            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
228            where
229                M: MapAccess<'a>,
230            {
231                let mut created_at: Option<i64> = None;
232                let mut creator: Option<crate::datadogV1::model::Creator> = None;
233                let mut description: Option<Option<String>> = None;
234                let mut groups: Option<Vec<String>> = None;
235                let mut id: Option<String> = None;
236                let mut modified_at: Option<i64> = None;
237                let mut monitor_ids: Option<Vec<i64>> = None;
238                let mut monitor_tags: Option<Vec<String>> = None;
239                let mut name: Option<String> = None;
240                let mut query: Option<crate::datadogV1::model::ServiceLevelObjectiveQuery> = None;
241                let mut sli_specification: Option<crate::datadogV1::model::SLOSliSpec> = None;
242                let mut tags: Option<Vec<String>> = None;
243                let mut target_threshold: Option<f64> = None;
244                let mut thresholds: Option<Vec<crate::datadogV1::model::SLOThreshold>> = None;
245                let mut timeframe: Option<crate::datadogV1::model::SLOTimeframe> = None;
246                let mut type_: Option<crate::datadogV1::model::SLOType> = None;
247                let mut warning_threshold: Option<f64> = None;
248                let mut additional_properties: std::collections::BTreeMap<
249                    String,
250                    serde_json::Value,
251                > = std::collections::BTreeMap::new();
252                let mut _unparsed = false;
253
254                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
255                    match k.as_str() {
256                        "created_at" => {
257                            if v.is_null() {
258                                continue;
259                            }
260                            created_at = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
261                        }
262                        "creator" => {
263                            if v.is_null() {
264                                continue;
265                            }
266                            creator = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
267                        }
268                        "description" => {
269                            description =
270                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
271                        }
272                        "groups" => {
273                            if v.is_null() {
274                                continue;
275                            }
276                            groups = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
277                        }
278                        "id" => {
279                            if v.is_null() {
280                                continue;
281                            }
282                            id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
283                        }
284                        "modified_at" => {
285                            if v.is_null() {
286                                continue;
287                            }
288                            modified_at =
289                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
290                        }
291                        "monitor_ids" => {
292                            if v.is_null() {
293                                continue;
294                            }
295                            monitor_ids =
296                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
297                        }
298                        "monitor_tags" => {
299                            if v.is_null() {
300                                continue;
301                            }
302                            monitor_tags =
303                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
304                        }
305                        "name" => {
306                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
307                        }
308                        "query" => {
309                            if v.is_null() {
310                                continue;
311                            }
312                            query = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
313                        }
314                        "sli_specification" => {
315                            if v.is_null() {
316                                continue;
317                            }
318                            sli_specification =
319                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
320                            if let Some(ref _sli_specification) = sli_specification {
321                                match _sli_specification {
322                                    crate::datadogV1::model::SLOSliSpec::UnparsedObject(
323                                        _sli_specification,
324                                    ) => {
325                                        _unparsed = true;
326                                    }
327                                    _ => {}
328                                }
329                            }
330                        }
331                        "tags" => {
332                            if v.is_null() {
333                                continue;
334                            }
335                            tags = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
336                        }
337                        "target_threshold" => {
338                            if v.is_null() {
339                                continue;
340                            }
341                            target_threshold =
342                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
343                        }
344                        "thresholds" => {
345                            thresholds = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
346                        }
347                        "timeframe" => {
348                            if v.is_null() {
349                                continue;
350                            }
351                            timeframe = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
352                            if let Some(ref _timeframe) = timeframe {
353                                match _timeframe {
354                                    crate::datadogV1::model::SLOTimeframe::UnparsedObject(
355                                        _timeframe,
356                                    ) => {
357                                        _unparsed = true;
358                                    }
359                                    _ => {}
360                                }
361                            }
362                        }
363                        "type" => {
364                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
365                            if let Some(ref _type_) = type_ {
366                                match _type_ {
367                                    crate::datadogV1::model::SLOType::UnparsedObject(_type_) => {
368                                        _unparsed = true;
369                                    }
370                                    _ => {}
371                                }
372                            }
373                        }
374                        "warning_threshold" => {
375                            if v.is_null() {
376                                continue;
377                            }
378                            warning_threshold =
379                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
380                        }
381                        &_ => {
382                            if let Ok(value) = serde_json::from_value(v.clone()) {
383                                additional_properties.insert(k, value);
384                            }
385                        }
386                    }
387                }
388                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
389                let thresholds = thresholds.ok_or_else(|| M::Error::missing_field("thresholds"))?;
390                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
391
392                let content = ServiceLevelObjective {
393                    created_at,
394                    creator,
395                    description,
396                    groups,
397                    id,
398                    modified_at,
399                    monitor_ids,
400                    monitor_tags,
401                    name,
402                    query,
403                    sli_specification,
404                    tags,
405                    target_threshold,
406                    thresholds,
407                    timeframe,
408                    type_,
409                    warning_threshold,
410                    additional_properties,
411                    _unparsed,
412                };
413
414                Ok(content)
415            }
416        }
417
418        deserializer.deserialize_any(ServiceLevelObjectiveVisitor)
419    }
420}