use std::net::IpAddr;
use instant_xml::ToXml;
use super::{serialize_host_addrs_option, Status, XMLNS};
use crate::common::{NoExtension, EPP_XMLNS};
use crate::request::{Command, Transaction};
impl<'a> Transaction<NoExtension> for HostUpdate<'a> {}
impl<'a> Command for HostUpdate<'a> {
type Response = ();
const COMMAND: &'static str = "update";
}
impl<'a> HostUpdate<'a> {
pub fn new(name: &'a str) -> Self {
Self {
host: HostUpdateRequest {
name,
add: None,
remove: None,
change_info: None,
},
}
}
pub fn info(&mut self, info: HostChangeInfo<'a>) {
self.host.change_info = Some(info);
}
pub fn add(&mut self, add: HostAdd<'a>) {
self.host.add = Some(add);
}
pub fn remove(&mut self, remove: HostRemove<'a>) {
self.host.remove = Some(remove);
}
}
#[derive(Debug, ToXml)]
#[xml(rename = "chg", ns(XMLNS))]
pub struct HostChangeInfo<'a> {
pub name: &'a str,
}
#[derive(Debug, ToXml)]
#[xml(rename = "add", ns(XMLNS))]
pub struct HostAdd<'a> {
#[xml(rename = "host:addr", serialize_with = "serialize_host_addrs_option")]
pub addresses: Option<&'a [IpAddr]>,
#[xml(rename = "host:status")]
pub statuses: Option<&'a [Status]>,
}
#[derive(Debug, ToXml)]
#[xml(rename = "rem", ns(XMLNS))]
pub struct HostRemove<'a> {
#[xml(rename = "host:addr", serialize_with = "serialize_host_addrs_option")]
pub addresses: Option<&'a [IpAddr]>,
#[xml(rename = "host:status")]
pub statuses: Option<&'a [Status]>,
}
#[derive(Debug, ToXml)]
#[xml(rename = "update", ns(XMLNS))]
pub struct HostUpdateRequest<'a> {
name: &'a str,
#[xml(rename = "host:add")]
add: Option<HostAdd<'a>>,
#[xml(rename = "host:rem")]
remove: Option<HostRemove<'a>>,
#[xml(rename = "host:chg")]
change_info: Option<HostChangeInfo<'a>>,
}
#[derive(Debug, ToXml)]
#[xml(rename = "update", ns(EPP_XMLNS))]
pub struct HostUpdate<'a> {
host: HostUpdateRequest<'a>,
}
#[cfg(test)]
mod tests {
use super::IpAddr;
use super::{HostAdd, HostChangeInfo, HostRemove, HostUpdate, Status};
use crate::response::ResultCode;
use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
#[test]
fn command() {
let addr = &[IpAddr::from([
0x2404, 0x6800, 0x4001, 0x801, 0, 0, 0, 0x200e,
])];
let add = HostAdd {
addresses: Some(addr),
statuses: None,
};
let remove = HostRemove {
addresses: None,
statuses: Some(&[Status::ClientDeleteProhibited]),
};
let mut object = HostUpdate::new("host1.eppdev-1.com");
object.add(add);
object.remove(remove);
object.info(HostChangeInfo {
name: "host2.eppdev-1.com",
});
assert_serialized("request/host/update.xml", &object);
}
#[test]
fn response() {
let object = response_from_file::<HostUpdate>("response/host/update.xml");
assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
assert_eq!(object.result.message, SUCCESS_MSG);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
assert_eq!(object.tr_ids.server_tr_id, SVTRID);
}
}