1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Types for EPP host create request

use std::net::IpAddr;

use chrono::{DateTime, Utc};
use instant_xml::{FromXml, ToXml};

use super::{serialize_host_addrs_option, XMLNS};
use crate::common::{NoExtension, EPP_XMLNS};
use crate::request::{Command, Transaction};

impl<'a> Transaction<NoExtension> for HostCreate<'a> {}

impl<'a> Command for HostCreate<'a> {
    type Response = CreateData;
    const COMMAND: &'static str = "create";
}

impl<'a> HostCreate<'a> {
    pub fn new(name: &'a str, addresses: Option<&'a [IpAddr]>) -> Self {
        Self {
            host: HostCreateRequest { name, addresses },
        }
    }
}

// Request

/// Type for data under the host `<create>` tag
#[derive(Debug, ToXml)]
#[xml(rename = "create", ns(XMLNS))]
pub struct HostCreateRequest<'a> {
    /// The name of the host to be created
    pub name: &'a str,
    /// The list of IP addresses for the host
    #[xml(serialize_with = "serialize_host_addrs_option")]
    pub addresses: Option<&'a [IpAddr]>,
}

/// Type for EPP XML `<create>` command for hosts
#[derive(Debug, ToXml)]
#[xml(rename = "create", ns(EPP_XMLNS))]
pub struct HostCreate<'a> {
    /// The instance holding the data for the host to be created
    host: HostCreateRequest<'a>,
}

// Response

/// Type that represents the `<creData>` tag for host create response
#[derive(Debug, FromXml)]
#[xml(rename = "creData", ns(XMLNS))]
pub struct CreateData {
    /// The host name
    pub name: String,
    /// The host creation date
    #[xml(rename = "crDate")]
    pub created_at: DateTime<Utc>,
}

#[cfg(test)]
mod tests {
    use chrono::{TimeZone, Utc};

    use super::{HostCreate, IpAddr};
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};

    #[test]
    fn command() {
        let addresses = &[
            IpAddr::from([29, 245, 122, 14]),
            IpAddr::from([0x2404, 0x6800, 0x4001, 0x801, 0, 0, 0, 0x200e]),
        ];

        let object = HostCreate::new("host1.eppdev-1.com", Some(addresses));
        assert_serialized("request/host/create.xml", &object);
    }

    #[test]
    fn response() {
        let object = response_from_file::<HostCreate>("response/host/create.xml");
        let result = object.res_data().unwrap();

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(result.name, "host2.eppdev-1.com");
        assert_eq!(
            result.created_at,
            Utc.with_ymd_and_hms(2021, 7, 26, 5, 28, 55).unwrap()
        );
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }
}