instant_epp/host/
create.rs1use std::net::IpAddr;
4
5use chrono::{DateTime, Utc};
6use instant_xml::{FromXml, ToXml};
7
8use super::{serialize_host_addrs_option, XMLNS};
9use crate::common::{NoExtension, EPP_XMLNS};
10use crate::request::{Command, Transaction};
11
12impl<'a> Transaction<NoExtension> for HostCreate<'a> {}
13
14impl<'a> Command for HostCreate<'a> {
15 type Response = CreateData;
16 const COMMAND: &'static str = "create";
17}
18
19impl<'a> HostCreate<'a> {
20 pub fn new(name: &'a str, addresses: Option<&'a [IpAddr]>) -> Self {
21 Self {
22 host: HostCreateRequest { name, addresses },
23 }
24 }
25}
26
27#[derive(Debug, ToXml)]
31#[xml(rename = "create", ns(XMLNS))]
32pub struct HostCreateRequest<'a> {
33 pub name: &'a str,
35 #[xml(serialize_with = "serialize_host_addrs_option")]
37 pub addresses: Option<&'a [IpAddr]>,
38}
39
40#[derive(Debug, ToXml)]
42#[xml(rename = "create", ns(EPP_XMLNS))]
43pub struct HostCreate<'a> {
44 host: HostCreateRequest<'a>,
46}
47
48#[derive(Debug, FromXml)]
52#[xml(rename = "creData", ns(XMLNS))]
53pub struct CreateData {
54 pub name: String,
56 #[xml(rename = "crDate")]
58 pub created_at: DateTime<Utc>,
59}
60
61#[cfg(test)]
62mod tests {
63 use chrono::{TimeZone, Utc};
64
65 use super::{HostCreate, IpAddr};
66 use crate::response::ResultCode;
67 use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
68
69 #[test]
70 fn command() {
71 let addresses = &[
72 IpAddr::from([29, 245, 122, 14]),
73 IpAddr::from([0x2404, 0x6800, 0x4001, 0x801, 0, 0, 0, 0x200e]),
74 ];
75
76 let object = HostCreate::new("host1.eppdev-1.com", Some(addresses));
77 assert_serialized("request/host/create.xml", &object);
78 }
79
80 #[test]
81 fn response() {
82 let object = response_from_file::<HostCreate>("response/host/create.xml");
83 let result = object.res_data().unwrap();
84
85 assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
86 assert_eq!(object.result.message, SUCCESS_MSG);
87 assert_eq!(result.name, "host2.eppdev-1.com");
88 assert_eq!(
89 result.created_at,
90 Utc.with_ymd_and_hms(2021, 7, 26, 5, 28, 55).unwrap()
91 );
92 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
93 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
94 }
95}