zone_edit/dnsimple/
mod.rs

1
2mod types;
3
4use std::{fmt::Display, sync::{LazyLock, Mutex, OnceLock}};
5
6use serde::de::DeserializeOwned;
7use tracing::{error, info, warn};
8use ureq::{
9    config::ConfigBuilder,
10    http::{
11        header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
12        Response,
13        StatusCode
14    },
15    Agent,
16    Body,
17    ResponseExt,
18};
19
20use crate::http::{self, ResponseToOption, WithHeaders};
21
22
23use crate::{
24    dnsimple::types::{
25        Accounts,
26        CreateRecord,
27        GetRecord,
28        Records,
29        UpdateRecord
30    },
31    errors::{Error, Result},
32    Config,
33    DnsProvider,
34    RecordType
35};
36
37
38pub(crate) const API_BASE: &str = "https://api.dnsimple.com/v2";
39
40pub struct Auth {
41    pub(crate) key: String,
42}
43
44impl Auth {
45    fn get_header(&self) -> String {
46        format!("Bearer {}", self.key)
47    }
48}
49
50pub(crate) struct DnSimple {
51    config: Config,
52    endpoint: &'static str,
53    auth: Auth,
54    acc_id: Mutex<Option<u32>>,
55}
56
57impl DnSimple {
58    pub fn new(config: Config, auth: Auth, acc: Option<u32>) -> Self {
59        Self::new_with_endpoint(config, auth, acc, API_BASE)
60    }
61
62    pub fn new_with_endpoint(config: Config, auth: Auth, acc: Option<u32>, endpoint: &'static str) -> Self {
63        let acc_id = Mutex::new(acc);
64        DnSimple {
65            config,
66            endpoint,
67            auth,
68            acc_id,
69        }
70    }
71
72    fn get_upstream_id(&self) -> Result<u32> {
73        info!("Fetching account ID from upstream");
74        let url = format!("{}/accounts", self.endpoint);
75
76        let accounts_p = http::client().get(url)
77            .with_auth(self.auth.get_header())
78            .call()?
79            .to_option::<Accounts>()?;
80
81        match accounts_p {
82            Some(accounts) if accounts.accounts.len() == 1 => {
83                Ok(accounts.accounts[0].id)
84            }
85            Some(accounts) if accounts.accounts.len() > 1 => {
86                Err(Error::ApiError("More than one account returned; you must specify the account ID to use".to_string()))
87            }
88            // None or 0 accounts => {
89            _ => {
90                Err(Error::ApiError("No accounts returned from upstream".to_string()))
91            }
92        }
93    }
94
95    fn get_id(&self) -> Result<u32> {
96        // This is roughly equivalent to OnceLock.get_or_init(), but
97        // is simpler than dealing with closure->Result and is more
98        // portable.
99        let mut id_p = self.acc_id.lock()
100            .map_err(|e| Error::LockingError(e.to_string()))?;
101
102        if let Some(id) = *id_p {
103            return Ok(id);
104        }
105
106        let id = self.get_upstream_id()?;
107        *id_p = Some(id);
108
109        Ok(id)
110    }
111
112    fn get_upstream_record<T>(&self, rtype: RecordType, host: &str) -> Result<Option<GetRecord<T>>>
113    where
114        T: DeserializeOwned
115    {
116        let acc_id = self.get_id()?;
117        let url = format!("{}/{acc_id}/zones/{}/records?name={host}&type={rtype}", self.endpoint, self.config.domain);
118
119        let response = http::client().get(url)
120            .with_json_headers()
121            .with_auth(self.auth.get_header())
122            .call()?
123            .to_option::<Records<T>>()?;
124        let mut recs: Records<T> = match response {
125            Some(rec) => rec,
126            None => return Ok(None)
127        };
128
129        // FIXME: Assumes no or single address (which probably makes
130        // sense for DDNS and DNS-01, but may cause issues with
131        // malformed zones).
132        let nr = recs.records.len();
133        if nr > 1 {
134            error!("Returned number of IPs is {}, should be 1", nr);
135            return Err(Error::UnexpectedRecord(format!("Returned number of records is {nr}, should be 1")));
136        } else if nr == 0 {
137            warn!("No IP returned for {host}, continuing");
138            return Ok(None);
139        }
140
141        Ok(Some(recs.records.remove(0)))
142    }
143}
144
145
146impl DnsProvider for DnSimple {
147
148    fn get_record<T>(&self, rtype: RecordType, host: &str) -> Result<Option<T> >
149    where
150        T: DeserializeOwned
151    {
152        let rec: GetRecord<T> = match self.get_upstream_record(rtype, host)? {
153            Some(recs) => recs,
154            None => return Ok(None)
155        };
156
157
158        Ok(Some(rec.content))
159    }
160
161    fn create_record<T>(&self, rtype: RecordType, host: &str, record: &T) -> Result<()>
162    where
163        T: Display,
164    {
165        let acc_id = self.get_id()?;
166
167        let url = format!("{}/{acc_id}/zones/{}/records", self.endpoint, self.config.domain);
168
169        let rec = CreateRecord {
170            name: host.to_string(),
171            rtype,
172            content: record.to_string(),
173            ttl: 300,
174        };
175
176        if self.config.dry_run {
177            info!("DRY-RUN: Would have sent {rec:?} to {url}");
178            return Ok(())
179        }
180
181        let body = serde_json::to_string(&rec)?;
182        http::client().post(url)
183            .with_json_headers()
184            .with_auth(self.auth.get_header())
185            .send(body)?;
186
187        Ok(())
188    }
189
190    fn update_record<T>(&self, rtype: RecordType, host: &str, urec: &T) -> Result<()>
191    where
192        T: DeserializeOwned + Display,
193    {
194        let rec: GetRecord<T> = match self.get_upstream_record(rtype, host)? {
195            Some(rec) => rec,
196            None => {
197                warn!("DELETE: Record {host} doesn't exist");
198                return Ok(());
199            }
200        };
201
202        let acc_id = self.get_id()?;
203        let rid = rec.id;
204
205        let update = UpdateRecord {
206            content: urec.to_string(),
207        };
208
209        let url = format!("{}/{acc_id}/zones/{}/records/{rid}", self.endpoint, self.config.domain);
210        if self.config.dry_run {
211            info!("DRY-RUN: Would have sent PATCH to {url}");
212            return Ok(())
213        }
214
215
216        let body = serde_json::to_string(&update)?;
217        http::client().patch(url)
218            .with_json_headers()
219            .with_auth(self.auth.get_header())
220            .send(body)?;
221
222        Ok(())
223    }
224
225    fn delete_record(&self, rtype: RecordType, host: &str) -> Result<()> {
226        let rec: GetRecord<String> = match self.get_upstream_record(rtype, host)? {
227            Some(rec) => rec,
228            None => {
229                warn!("DELETE: Record {host} doesn't exist");
230                return Ok(());
231            }
232        };
233
234        let acc_id = self.get_id()?;
235        let rid = rec.id;
236
237        let url = format!("{}/{acc_id}/zones/{}/records/{rid}", self.endpoint, self.config.domain);
238        if self.config.dry_run {
239            info!("DRY-RUN: Would have sent DELETE to {url}");
240            return Ok(())
241        }
242
243        http::client().delete(url)
244            .with_json_headers()
245            .with_auth(self.auth.get_header())
246            .call()?;
247
248        Ok(())
249    }
250}
251
252
253
254#[cfg(test)]
255mod tests {
256    use super::*;
257    use crate::{generate_tests, tests::*};
258    use std::env;
259
260    const TEST_API: &str = "https://api.sandbox.dnsimple.com/v2";
261
262    fn get_client() -> DnSimple {
263        let auth = Auth { key: env::var("DNSIMPLE_TOKEN").unwrap() };
264        let config = Config {
265            domain: env::var("DNSIMPLE_TEST_DOMAIN").unwrap(),
266            dry_run: false,
267        };
268        DnSimple::new_with_endpoint(config, auth, None, TEST_API)
269    }
270
271    #[test_log::test]
272    #[cfg_attr(not(feature = "test_dnsimple"), ignore = "DnSimple API test")]
273    fn test_id_fetch() -> Result<()> {
274        let client = get_client();
275
276        let id = client.get_upstream_id()?;
277        assert_eq!(2602, id);
278
279        Ok(())
280    }
281
282    generate_tests!("test_dnsimple");
283}
284