digitalocean_api/api/
domain_record.rs

1use super::domain::Domain;
2use super::{ApiLinks, ApiMeta};
3use super::{HasPagination, HasResponse, HasValue};
4use crate::method::{Create, Delete, Get, List, Update};
5use crate::request::{DomainRecordRequest, DomainRequest};
6use crate::STATIC_URL_ERROR;
7use getset::{Getters, Setters};
8use serde::Deserialize;
9use serde::Serialize;
10use std::fmt::Display;
11use url::Url;
12
13const DOMAIN_RECORDS_SEGMENT: &str = "records";
14
15/// Domain record resources are used to set or retrieve information about the
16/// individual DNS records configured for a domain. This allows you to build
17/// and manage DNS zone files by adding and modifying individual records for a
18/// domain.
19///
20/// Requests with this output this type are accessed via [`Domain::get(..).records()`](../request/type.DomainRequest.html#method.records).
21/// Make sure to check the functions in [`DomainRecordRequest`](../request/type.DomainRecordRequest.html)
22///
23/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
24#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
25#[get = "pub"]
26pub struct DomainRecord {
27    /// A unique identifier for each domain record.
28    id: usize,
29
30    /// The type of the DNS record (ex: A, CNAME, TXT, ...).
31    ///
32    /// *Note:* Since `type` is a keyword in Rust `kind` is used instead.
33    #[serde(rename = "type")]
34    kind: String,
35    // 'type' is reserved in Rust.
36    /// The name to use for the DNS record.
37    name: String,
38
39    /// The value to use for the DNS record.
40    data: String,
41
42    /// The priority for SRV and MX records.
43    priority: Option<usize>,
44
45    /// The port for SRV records.
46    port: Option<usize>,
47
48    /// This value is the time to live for the record, in seconds. This defines
49    /// the time frame that clients can cache queried information before a refresh
50    /// should be requested.
51    ttl: usize,
52
53    /// The weight for SRV records.
54    weight: Option<usize>,
55}
56
57impl DomainRequest<Get, Domain> {
58    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-domain-records)
59    pub fn records(mut self) -> DomainRecordRequest<List, Vec<DomainRecord>> {
60        self.url_mut()
61            .path_segments_mut()
62            .expect(STATIC_URL_ERROR)
63            .push(DOMAIN_RECORDS_SEGMENT);
64
65        self.transmute()
66    }
67}
68
69impl DomainRecordRequest<List, Vec<DomainRecord>> {
70    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-new-domain-record)
71    pub fn create<S: AsRef<str> + Display + Serialize>(
72        mut self,
73        kind: S,
74        name: S,
75        data: S,
76    ) -> DomainRecordRequest<Create, DomainRecord> {
77        self.url_mut().path_segments_mut().expect(STATIC_URL_ERROR);
78
79        self.set_body(json!({
80            "type": kind,
81            "name": name,
82            "data": data
83        }));
84
85        self.transmute()
86    }
87
88    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-domain-record)
89    pub fn get(mut self, id: usize) -> DomainRecordRequest<Get, DomainRecord> {
90        self.url_mut()
91            .path_segments_mut()
92            .expect(STATIC_URL_ERROR)
93            .push(&id.to_string());
94
95        self.transmute()
96    }
97
98    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#update-a-domain-record)
99    pub fn update(mut self, id: usize) -> DomainRecordRequest<Update, DomainRecord> {
100        self.url_mut()
101            .path_segments_mut()
102            .expect(STATIC_URL_ERROR)
103            .push(&id.to_string());
104
105        self.transmute()
106    }
107
108    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#delete-a-domain-record)
109    pub fn delete(mut self, id: usize) -> DomainRecordRequest<Delete, ()> {
110        self.url_mut()
111            .path_segments_mut()
112            .expect(STATIC_URL_ERROR)
113            .push(&id.to_string());
114
115        self.transmute()
116    }
117}
118
119impl DomainRecordRequest<Create, DomainRecord> {
120    /// The priority for SRV and MX records.
121    ///
122    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
123    pub fn priority(mut self, val: Option<usize>) -> Self {
124        self.body_mut()["priority"] = json!(val);
125        self
126    }
127
128    /// The port for SRV records.
129    ///
130    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
131    pub fn port(mut self, val: Option<usize>) -> Self {
132        self.body_mut()["port"] = json!(val);
133        self
134    }
135
136    /// This value is the time to live for the record, in seconds. This defines
137    /// the time frame that clients can cache queried information before a
138    /// refresh should be requested.
139    ///
140    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
141    pub fn ttl(mut self, val: usize) -> Self {
142        self.body_mut()["ttl"] = json!(val);
143        self
144    }
145
146    /// The weight for SRV records.
147    ///
148    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
149    pub fn weight(mut self, val: Option<usize>) -> Self {
150        self.body_mut()["weight"] = json!(val);
151        self
152    }
153}
154
155impl DomainRecordRequest<Update, DomainRecord> {
156    /// The record type (A, MX, CNAME, etc).
157    ///
158    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
159    pub fn kind<S: AsRef<str> + Display + Serialize>(mut self, val: S) -> Self {
160        self.body_mut()["type"] = json!(val);
161        self
162    }
163
164    /// The host name, alias, or service being defined by the record.
165    ///
166    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
167    pub fn name<S: AsRef<str> + Display + Serialize>(mut self, val: S) -> Self {
168        self.body_mut()["name"] = json!(val);
169        self
170    }
171
172    /// Variable data depending on record type. See the Domain Records section
173    /// for more detail on each record type.
174    ///
175    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
176    pub fn data<S: AsRef<str> + Display + Serialize>(mut self, val: S) -> Self {
177        self.body_mut()["data"] = json!(val);
178        self
179    }
180
181    /// The priority for SRV and MX records.
182    ///
183    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
184    pub fn priority(mut self, val: Option<usize>) -> Self {
185        self.body_mut()["priority"] = json!(val);
186        self
187    }
188
189    /// The port for SRV records.
190    ///
191    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
192    pub fn port(mut self, val: Option<usize>) -> Self {
193        self.body_mut()["port"] = json!(val);
194        self
195    }
196
197    /// This value is the time to live for the record, in seconds. This defines
198    /// the time frame that clients can cache queried information before a
199    /// refresh should be requested.
200    ///
201    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
202    pub fn ttl(mut self, val: usize) -> Self {
203        self.body_mut()["ttl"] = json!(val);
204        self
205    }
206
207    /// The weight for SRV records.
208    ///
209    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
210    pub fn weight(mut self, val: Option<usize>) -> Self {
211        self.body_mut()["weight"] = json!(val);
212        self
213    }
214}
215
216/// Response type returned from Digital Ocean.
217#[derive(Deserialize, Serialize, Debug, Clone)]
218pub struct DomainRecordResponse {
219    domain_record: DomainRecord,
220}
221
222impl HasValue for DomainRecordResponse {
223    type Value = DomainRecord;
224
225    fn value(self) -> DomainRecord {
226        self.domain_record
227    }
228}
229
230impl HasResponse for DomainRecord {
231    type Response = DomainRecordResponse;
232}
233
234/// Response type returned from Digital Ocean.
235#[derive(Deserialize, Serialize, Debug, Clone)]
236pub struct DomainRecordListResponse {
237    domain_records: Vec<DomainRecord>,
238    links: ApiLinks,
239    meta: ApiMeta,
240}
241
242impl HasResponse for Vec<DomainRecord> {
243    type Response = DomainRecordListResponse;
244}
245
246impl HasPagination for DomainRecordListResponse {
247    fn next_page(&self) -> Option<Url> {
248        self.links.next()
249    }
250}
251
252impl HasValue for DomainRecordListResponse {
253    type Value = Vec<DomainRecord>;
254
255    fn value(self) -> Vec<DomainRecord> {
256        self.domain_records
257    }
258}