tencent_sdk/services/dns/
modify.rs

1use crate::{
2    client::{TencentCloudAsync, TencentCloudBlocking},
3    core::{Endpoint, TencentCloudResult},
4    services::dns::{RecordLine, RecordType},
5};
6use serde::Deserialize;
7use serde_json::{json, Value};
8use std::borrow::Cow;
9
10#[derive(Debug, Deserialize)]
11pub struct ModifyTXTRecordResponse {
12    #[serde(rename = "Response")]
13    pub response: ModifyTXTRecordResult,
14}
15
16#[derive(Debug, Deserialize)]
17pub struct ModifyTXTRecordResult {
18    #[serde(rename = "RecordId")]
19    pub record_id: Option<u64>,
20    #[serde(rename = "RequestId")]
21    pub request_id: String,
22}
23
24/// Request parameters for modifying a TXT record.
25pub struct ModifyTXTRecord<'a> {
26    pub domain: &'a str,
27    pub sub_domain: &'a str,
28    pub record_line: RecordLine<'a>,
29    pub value: &'a str,
30    pub record_type: RecordType<'a>,
31    pub record_id: u64,
32    pub domain_id: Option<u64>,
33    pub record_line_id: Option<&'a str>,
34    pub ttl: Option<u32>,
35    pub status: Option<&'a str>,
36    pub remark: Option<&'a str>,
37}
38
39impl<'a> ModifyTXTRecord<'a> {
40    /// Create a TXT record update request (defaults `RecordType` to `"TXT"`).
41    pub fn new(
42        domain: &'a str,
43        sub_domain: &'a str,
44        record_line: &'a str,
45        value: &'a str,
46        record_id: u64,
47    ) -> Self {
48        Self {
49            domain,
50            sub_domain,
51            record_line: record_line.into(),
52            value,
53            record_type: RecordType::Txt,
54            record_id,
55            domain_id: None,
56            record_line_id: None,
57            ttl: None,
58            status: None,
59            remark: None,
60        }
61    }
62
63    pub fn with_domain_id(mut self, domain_id: u64) -> Self {
64        self.domain_id = Some(domain_id);
65        self
66    }
67
68    pub fn with_record_line_id(mut self, record_line_id: &'a str) -> Self {
69        self.record_line_id = Some(record_line_id);
70        self
71    }
72
73    pub fn with_ttl(mut self, ttl: u32) -> Self {
74        self.ttl = Some(ttl);
75        self
76    }
77
78    pub fn with_status(mut self, status: &'a str) -> Self {
79        self.status = Some(status);
80        self
81    }
82
83    pub fn with_remark(mut self, remark: &'a str) -> Self {
84        self.remark = Some(remark);
85        self
86    }
87
88    pub fn with_sub_domain(mut self, sub_domain: &'a str) -> Self {
89        self.sub_domain = sub_domain;
90        self
91    }
92
93    pub fn with_record_type(mut self, record_type: RecordType<'a>) -> Self {
94        self.record_type = record_type;
95        self
96    }
97
98    pub fn with_record_line(mut self, record_line: RecordLine<'a>) -> Self {
99        self.record_line = record_line;
100        self
101    }
102}
103
104impl<'a> Endpoint for ModifyTXTRecord<'a> {
105    type Output = ModifyTXTRecordResponse;
106
107    fn service(&self) -> Cow<'static, str> {
108        Cow::Borrowed("dnspod")
109    }
110
111    fn action(&self) -> Cow<'static, str> {
112        Cow::Borrowed("ModifyRecord")
113    }
114
115    fn version(&self) -> Cow<'static, str> {
116        Cow::Borrowed("2021-03-23")
117    }
118
119    fn region(&self) -> Option<Cow<'_, str>> {
120        None
121    }
122
123    fn payload(&self) -> Value {
124        let mut payload = json!({
125            "Domain": self.domain,
126            "SubDomain": self.sub_domain,
127            "RecordType": self.record_type,
128            "RecordLine": self.record_line,
129            "Value": self.value,
130            "RecordId": self.record_id,
131        });
132
133        if let Some(domain_id) = self.domain_id {
134            payload["DomainId"] = json!(domain_id);
135        }
136        if let Some(record_line_id) = self.record_line_id {
137            payload["RecordLineId"] = json!(record_line_id);
138        }
139        if let Some(ttl) = self.ttl {
140            payload["TTL"] = json!(ttl);
141        }
142        if let Some(status) = self.status {
143            payload["Status"] = json!(status);
144        }
145        if let Some(remark) = self.remark {
146            payload["Remark"] = json!(remark);
147        }
148
149        payload
150    }
151}
152
153/// Call DNSPod `ModifyRecord` with the async client.
154pub async fn modify_txt_record_async(
155    client: &TencentCloudAsync,
156    request: &ModifyTXTRecord<'_>,
157) -> TencentCloudResult<ModifyTXTRecordResponse> {
158    client.request(request).await
159}
160
161/// Call DNSPod `ModifyRecord` with the blocking client.
162pub fn modify_txt_record_blocking(
163    client: &TencentCloudBlocking,
164    request: &ModifyTXTRecord<'_>,
165) -> TencentCloudResult<ModifyTXTRecordResponse> {
166    client.request(request)
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172    use serde_json::json;
173
174    #[test]
175    fn test_modify_txt_record_payload() {
176        let request = ModifyTXTRecord::new("example.com", "www", "default", "new-value", 123)
177            .with_ttl(300)
178            .with_remark("updated record");
179
180        let payload = request.payload();
181        assert_eq!(payload["Domain"], json!("example.com"));
182        assert_eq!(payload["SubDomain"], json!("www"));
183        assert_eq!(payload["RecordType"], json!("TXT"));
184        assert_eq!(payload["RecordLine"], json!(RecordLine::Default.as_str()));
185        assert_eq!(payload["Value"], json!("new-value"));
186        assert_eq!(payload["RecordId"], json!(123));
187        assert_eq!(payload["TTL"], json!(300));
188        assert_eq!(payload["Remark"], json!("updated record"));
189    }
190
191    #[test]
192    fn test_deserialize_modify_response() {
193        let json = r#"{
194            "Response": {
195                "RecordId": 456,
196                "RequestId": "req-345678"
197            }
198        }"#;
199
200        let response: ModifyTXTRecordResponse =
201            serde_json::from_str(json).expect("deserialize ModifyTXTRecordResponse");
202        assert_eq!(response.response.record_id, Some(456));
203        assert_eq!(response.response.request_id, "req-345678");
204    }
205
206    #[test]
207    fn test_endpoint_implementation() {
208        let modify_request =
209            ModifyTXTRecord::new("test.com", "_acme-challenge", "default", "value", 123);
210        assert_eq!(modify_request.service().as_ref(), "dnspod");
211        assert_eq!(modify_request.action().as_ref(), "ModifyRecord");
212        assert_eq!(modify_request.version().as_ref(), "2021-03-23");
213        assert!(modify_request.region().is_none());
214    }
215}