epp_client/host/
create.rs

1//! Types for EPP host create request
2
3use std::net::IpAddr;
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8use super::XMLNS;
9use crate::common::{serialize_host_addrs_option, NoExtension, StringValue};
10use crate::request::{Command, Transaction};
11
12impl<'a> Transaction<NoExtension> for HostCreate<'a> {}
13
14impl<'a> Command for HostCreate<'a> {
15    type Response = HostCreateResponse;
16    const COMMAND: &'static str = "create";
17}
18
19impl<'a> HostCreate<'a> {
20    pub fn new(host: &'a str, addresses: Option<&'a [IpAddr]>) -> Self {
21        Self {
22            host: HostCreateRequestData {
23                xmlns: XMLNS,
24                name: host.into(),
25                addresses,
26            },
27        }
28    }
29}
30
31// Request
32
33/// Type for data under the host &lt;create&gt; tag
34#[derive(Serialize, Debug)]
35pub struct HostCreateRequestData<'a> {
36    /// XML namespace for host commands
37    #[serde(rename = "xmlns:host")]
38    xmlns: &'a str,
39    /// The name of the host to be created
40    #[serde(rename = "host:name")]
41    pub name: StringValue<'a>,
42    /// The list of IP addresses for the host
43    #[serde(rename = "host:addr", serialize_with = "serialize_host_addrs_option")]
44    pub addresses: Option<&'a [IpAddr]>,
45}
46
47#[derive(Serialize, Debug)]
48/// Type for EPP XML &lt;create&gt; command for hosts
49pub struct HostCreate<'a> {
50    /// The instance holding the data for the host to be created
51    #[serde(rename = "host:create")]
52    host: HostCreateRequestData<'a>,
53}
54
55// Response
56
57/// Type that represents the &lt;creData&gt; tag for host create response
58#[derive(Deserialize, Debug)]
59pub struct HostCreateData {
60    /// The host name
61    pub name: StringValue<'static>,
62    /// The host creation date
63    #[serde(rename = "crDate")]
64    pub created_at: DateTime<Utc>,
65}
66
67/// Type that represents the &lt;resData&gt; tag for host check response
68#[derive(Deserialize, Debug)]
69pub struct HostCreateResponse {
70    /// Data under the &lt;creData&gt; tag
71    #[serde(rename = "creData")]
72    pub create_data: HostCreateData,
73}
74
75#[cfg(test)]
76mod tests {
77    use chrono::{TimeZone, Utc};
78
79    use super::{HostCreate, IpAddr};
80    use crate::response::ResultCode;
81    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
82
83    #[test]
84    fn command() {
85        let addresses = &[
86            IpAddr::from([29, 245, 122, 14]),
87            IpAddr::from([0x2404, 0x6800, 0x4001, 0x801, 0, 0, 0, 0x200e]),
88        ];
89
90        let object = HostCreate::new("host1.eppdev-1.com", Some(addresses));
91        assert_serialized("request/host/create.xml", &object);
92    }
93
94    #[test]
95    fn response() {
96        let object = response_from_file::<HostCreate>("response/host/create.xml");
97        let result = object.res_data().unwrap();
98
99        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
100        assert_eq!(object.result.message, SUCCESS_MSG.into());
101        assert_eq!(result.create_data.name, "host2.eppdev-1.com".into());
102        assert_eq!(
103            result.create_data.created_at,
104            Utc.with_ymd_and_hms(2021, 7, 26, 5, 28, 55).unwrap()
105        );
106        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into());
107        assert_eq!(object.tr_ids.server_tr_id, SVTRID.into());
108    }
109}