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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Types for EPP domain check request

use instant_xml::ToXml;

use super::{DomainAuthInfo, DomainContact, NameServers, Status, XMLNS};
use crate::{
    common::{NoExtension, EPP_XMLNS},
    request::{Command, Transaction},
};

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

impl<'a> Command for DomainUpdate<'a> {
    type Response = ();
    const COMMAND: &'static str = "update";
}

impl<'a> DomainUpdate<'a> {
    pub fn new(name: &'a str) -> Self {
        Self {
            domain: DomainUpdateRequestData {
                name,
                add: None,
                remove: None,
                change_info: None,
            },
        }
    }

    /// Sets the data for the `<chg>` tag
    pub fn info(&mut self, info: DomainChangeInfo<'a>) {
        self.domain.change_info = Some(info);
    }

    /// Sets the data for the `<add>` tag
    pub fn add(&mut self, add: DomainAdd<'a>) {
        self.domain.add = Some(add);
    }

    /// Sets the data for the `<rem>` tag
    pub fn remove(&mut self, remove: DomainRemove<'a>) {
        self.domain.remove = Some(remove);
    }
}

/// Type for elements under the `<chg>` tag for domain update
#[derive(Debug, ToXml)]
#[xml(rename = "chg", ns(XMLNS))]
pub struct DomainChangeInfo<'a> {
    /// The new registrant contact for the domain
    pub registrant: Option<&'a str>,
    /// The new auth info for the domain
    pub auth_info: Option<DomainAuthInfo<'a>>,
}

/// Type for elements under the `<add>` and `<rem>` tags for domain update
#[derive(Debug, ToXml)]
#[xml(rename = "add", ns(XMLNS))]
pub struct DomainAdd<'a> {
    /// The list of nameservers to add or remove
    /// Type T can be either a `HostObjList` or `HostAttrList`
    pub ns: Option<NameServers<'a>>,
    /// The list of contacts to add to or remove from the domain
    pub contacts: Option<&'a [DomainContact<'a>]>,
    /// The list of statuses to add to or remove from the domain
    pub statuses: Option<&'a [Status]>,
}

/// Type for elements under the `<add>` and `<rem>` tags for domain update
#[derive(Debug, ToXml)]
#[xml(rename = "rem", ns(XMLNS))]
pub struct DomainRemove<'a> {
    /// The list of nameservers to add or remove
    /// Type T can be either a `HostObjList` or `HostAttrList`
    pub ns: Option<NameServers<'a>>,
    /// The list of contacts to add to or remove from the domain
    pub contacts: Option<&'a [DomainContact<'a>]>,
    /// The list of statuses to add to or remove from the domain
    pub statuses: Option<&'a [Status]>,
}

/// Type for elements under the `<update>` tag for domain update
#[derive(Debug, ToXml)]
#[xml(rename = "update", ns(XMLNS))]
pub struct DomainUpdateRequestData<'a> {
    /// The name of the domain to update
    pub name: &'a str,
    /// `DomainAddRemove` Object containing the list of elements to be added
    /// to the domain
    pub add: Option<DomainAdd<'a>>,
    /// `DomainAddRemove` Object containing the list of elements to be removed
    /// from the domain
    pub remove: Option<DomainRemove<'a>>,
    /// The data under the `<chg>` tag for domain update
    #[xml(rename = "domain:chg")]
    pub change_info: Option<DomainChangeInfo<'a>>,
}

/// Type for EPP XML `<update>` command for domains
#[derive(Debug, ToXml)]
#[xml(rename = "update", ns(EPP_XMLNS))]
pub struct DomainUpdate<'a> {
    pub domain: DomainUpdateRequestData<'a>,
}

#[cfg(test)]
mod tests {
    use super::{
        DomainAdd, DomainAuthInfo, DomainChangeInfo, DomainContact, DomainRemove, DomainUpdate,
    };
    use crate::domain::Status;
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};

    #[test]
    fn command() {
        let mut object = DomainUpdate::new("eppdev.com");

        let add = DomainAdd {
            ns: None,
            contacts: None,
            statuses: Some(&[Status::ClientDeleteProhibited]),
        };

        let contacts = &[DomainContact {
            contact_type: "billing".into(),
            id: "eppdev-contact-2".into(),
        }];

        let remove = DomainRemove {
            ns: None,
            contacts: Some(contacts),
            statuses: None,
        };

        let change_info = DomainChangeInfo {
            registrant: None,
            auth_info: Some(DomainAuthInfo::new("epP5uthd#v")),
        };

        object.add(add);
        object.remove(remove);
        object.info(change_info);
        assert_serialized("request/domain/update.xml", &object);
    }

    #[test]
    fn response() {
        let object = response_from_file::<DomainUpdate>("response/domain/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);
    }
}