datadog_api_client/datadogV2/model/
model_datadog_api_key.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 definition of the `DatadogAPIKey` object.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct DatadogAPIKey {
14    /// The `DatadogAPIKey` `api_key`.
15    #[serde(rename = "api_key")]
16    pub api_key: String,
17    /// The `DatadogAPIKey` `app_key`.
18    #[serde(rename = "app_key")]
19    pub app_key: String,
20    /// The `DatadogAPIKey` `datacenter`.
21    #[serde(rename = "datacenter")]
22    pub datacenter: String,
23    /// Custom subdomain used for Datadog URLs generated with this Connection. For example, if this org uses `<https://acme.datadoghq.com`> to access Datadog, set this field to `acme`. If this field is omitted, generated URLs will use the default site URL for its datacenter (see [<https://docs.datadoghq.com/getting_started/site]>(<https://docs.datadoghq.com/getting_started/site>)).
24    #[serde(rename = "subdomain")]
25    pub subdomain: Option<String>,
26    /// The definition of the `DatadogAPIKey` object.
27    #[serde(rename = "type")]
28    pub type_: crate::datadogV2::model::DatadogAPIKeyType,
29    #[serde(flatten)]
30    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
31    #[serde(skip)]
32    #[serde(default)]
33    pub(crate) _unparsed: bool,
34}
35
36impl DatadogAPIKey {
37    pub fn new(
38        api_key: String,
39        app_key: String,
40        datacenter: String,
41        type_: crate::datadogV2::model::DatadogAPIKeyType,
42    ) -> DatadogAPIKey {
43        DatadogAPIKey {
44            api_key,
45            app_key,
46            datacenter,
47            subdomain: None,
48            type_,
49            additional_properties: std::collections::BTreeMap::new(),
50            _unparsed: false,
51        }
52    }
53
54    pub fn subdomain(mut self, value: String) -> Self {
55        self.subdomain = Some(value);
56        self
57    }
58
59    pub fn additional_properties(
60        mut self,
61        value: std::collections::BTreeMap<String, serde_json::Value>,
62    ) -> Self {
63        self.additional_properties = value;
64        self
65    }
66}
67
68impl<'de> Deserialize<'de> for DatadogAPIKey {
69    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
70    where
71        D: Deserializer<'de>,
72    {
73        struct DatadogAPIKeyVisitor;
74        impl<'a> Visitor<'a> for DatadogAPIKeyVisitor {
75            type Value = DatadogAPIKey;
76
77            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
78                f.write_str("a mapping")
79            }
80
81            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
82            where
83                M: MapAccess<'a>,
84            {
85                let mut api_key: Option<String> = None;
86                let mut app_key: Option<String> = None;
87                let mut datacenter: Option<String> = None;
88                let mut subdomain: Option<String> = None;
89                let mut type_: Option<crate::datadogV2::model::DatadogAPIKeyType> = None;
90                let mut additional_properties: std::collections::BTreeMap<
91                    String,
92                    serde_json::Value,
93                > = std::collections::BTreeMap::new();
94                let mut _unparsed = false;
95
96                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
97                    match k.as_str() {
98                        "api_key" => {
99                            api_key = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
100                        }
101                        "app_key" => {
102                            app_key = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
103                        }
104                        "datacenter" => {
105                            datacenter = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
106                        }
107                        "subdomain" => {
108                            if v.is_null() {
109                                continue;
110                            }
111                            subdomain = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
112                        }
113                        "type" => {
114                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
115                            if let Some(ref _type_) = type_ {
116                                match _type_ {
117                                    crate::datadogV2::model::DatadogAPIKeyType::UnparsedObject(
118                                        _type_,
119                                    ) => {
120                                        _unparsed = true;
121                                    }
122                                    _ => {}
123                                }
124                            }
125                        }
126                        &_ => {
127                            if let Ok(value) = serde_json::from_value(v.clone()) {
128                                additional_properties.insert(k, value);
129                            }
130                        }
131                    }
132                }
133                let api_key = api_key.ok_or_else(|| M::Error::missing_field("api_key"))?;
134                let app_key = app_key.ok_or_else(|| M::Error::missing_field("app_key"))?;
135                let datacenter = datacenter.ok_or_else(|| M::Error::missing_field("datacenter"))?;
136                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
137
138                let content = DatadogAPIKey {
139                    api_key,
140                    app_key,
141                    datacenter,
142                    subdomain,
143                    type_,
144                    additional_properties,
145                    _unparsed,
146                };
147
148                Ok(content)
149            }
150        }
151
152        deserializer.deserialize_any(DatadogAPIKeyVisitor)
153    }
154}