datadog_api_client/datadogV2/model/
model_service_now_basic_auth.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 `ServiceNowBasicAuth` object.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct ServiceNowBasicAuth {
14    /// The `ServiceNowBasicAuth` `instance`.
15    #[serde(rename = "instance")]
16    pub instance: String,
17    /// The `ServiceNowBasicAuth` `password`.
18    #[serde(rename = "password")]
19    pub password: String,
20    /// The definition of the `ServiceNowBasicAuth` object.
21    #[serde(rename = "type")]
22    pub type_: crate::datadogV2::model::ServiceNowBasicAuthType,
23    /// The `ServiceNowBasicAuth` `username`.
24    #[serde(rename = "username")]
25    pub username: String,
26    #[serde(flatten)]
27    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
28    #[serde(skip)]
29    #[serde(default)]
30    pub(crate) _unparsed: bool,
31}
32
33impl ServiceNowBasicAuth {
34    pub fn new(
35        instance: String,
36        password: String,
37        type_: crate::datadogV2::model::ServiceNowBasicAuthType,
38        username: String,
39    ) -> ServiceNowBasicAuth {
40        ServiceNowBasicAuth {
41            instance,
42            password,
43            type_,
44            username,
45            additional_properties: std::collections::BTreeMap::new(),
46            _unparsed: false,
47        }
48    }
49
50    pub fn additional_properties(
51        mut self,
52        value: std::collections::BTreeMap<String, serde_json::Value>,
53    ) -> Self {
54        self.additional_properties = value;
55        self
56    }
57}
58
59impl<'de> Deserialize<'de> for ServiceNowBasicAuth {
60    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
61    where
62        D: Deserializer<'de>,
63    {
64        struct ServiceNowBasicAuthVisitor;
65        impl<'a> Visitor<'a> for ServiceNowBasicAuthVisitor {
66            type Value = ServiceNowBasicAuth;
67
68            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
69                f.write_str("a mapping")
70            }
71
72            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
73            where
74                M: MapAccess<'a>,
75            {
76                let mut instance: Option<String> = None;
77                let mut password: Option<String> = None;
78                let mut type_: Option<crate::datadogV2::model::ServiceNowBasicAuthType> = None;
79                let mut username: Option<String> = None;
80                let mut additional_properties: std::collections::BTreeMap<
81                    String,
82                    serde_json::Value,
83                > = std::collections::BTreeMap::new();
84                let mut _unparsed = false;
85
86                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
87                    match k.as_str() {
88                        "instance" => {
89                            instance = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
90                        }
91                        "password" => {
92                            password = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
93                        }
94                        "type" => {
95                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
96                            if let Some(ref _type_) = type_ {
97                                match _type_ {
98                                    crate::datadogV2::model::ServiceNowBasicAuthType::UnparsedObject(_type_) => {
99                                        _unparsed = true;
100                                    },
101                                    _ => {}
102                                }
103                            }
104                        }
105                        "username" => {
106                            username = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
107                        }
108                        &_ => {
109                            if let Ok(value) = serde_json::from_value(v.clone()) {
110                                additional_properties.insert(k, value);
111                            }
112                        }
113                    }
114                }
115                let instance = instance.ok_or_else(|| M::Error::missing_field("instance"))?;
116                let password = password.ok_or_else(|| M::Error::missing_field("password"))?;
117                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
118                let username = username.ok_or_else(|| M::Error::missing_field("username"))?;
119
120                let content = ServiceNowBasicAuth {
121                    instance,
122                    password,
123                    type_,
124                    username,
125                    additional_properties,
126                    _unparsed,
127                };
128
129                Ok(content)
130            }
131        }
132
133        deserializer.deserialize_any(ServiceNowBasicAuthVisitor)
134    }
135}