zone-update 0.11.0

A library of CRUD-like operations on DNS zones for multiple providers
Documentation
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::{fmt::Display, net::Ipv4Addr};

use serde::{de::DeserializeOwned, Serialize};

use crate::{errors::Result, RecordType};


#[cfg(feature = "bunny")]
pub mod bunny;
#[cfg(feature = "cloudflare")]
pub mod cloudflare;
#[cfg(feature = "desec")]
pub mod desec;
#[cfg(feature = "digitalocean")]
pub mod digitalocean;
#[cfg(feature = "gandi")]
pub mod gandi;
#[cfg(feature = "linode")]
pub mod linode;
#[cfg(feature = "dnsmadeeasy")]
pub mod dnsmadeeasy;
#[cfg(feature = "dnsimple")]
pub mod dnsimple;
#[cfg(feature = "porkbun")]
pub mod porkbun;


/// Asynchronous DNS provider trait.
///
/// Mirrors `DnsProvider` with async methods that can be implemented by
/// async wrappers around synchronous providers or native async clients.
#[async_trait::async_trait]
pub trait AsyncDnsProvider: Send + Sync {

    async fn get_record<T>(&self, rtype: RecordType, host: &String) -> Result<Option<T>>
    where
        T: DeserializeOwned + Send + Sync + 'static,
        Self: Sized;

    async fn create_record<T>(&self, rtype: RecordType, host: &String, record: &T) -> Result<()>
    where
        T: Serialize + DeserializeOwned + Display + Clone + Send + Sync + 'static,
        Self: Sized;

    async fn update_record<T>(&self, rtype: RecordType, host: &String, record: &T) -> Result<()>
    where
        T: Serialize + DeserializeOwned + Display + Clone + Send + Sync + 'static,
        Self: Sized;

    async fn delete_record(&self, rtype: RecordType, host: &String) -> Result<()>
    where Self: Sized;

    async fn get_txt_record(&self, host: &String) -> Result<Option<String>>;

    async fn create_txt_record(&self, host: &String, record: &String) -> Result<()>;

    async fn update_txt_record(&self, host: &String, record: &String) -> Result<()>;

    async fn delete_txt_record(&self, host: &String) -> Result<()>;

    async fn get_a_record(&self, host: &String) -> Result<Option<Ipv4Addr>>;

    async fn create_a_record(&self, host: &String, record: &Ipv4Addr) -> Result<()>;

    async fn update_a_record(&self, host: &String, record: &Ipv4Addr) -> Result<()>;

    async fn delete_a_record(&self, host: &String) -> Result<()>;
}

#[macro_export]
macro_rules! async_provider_impl {
    ($i:ident) => {
        #[async_trait::async_trait]
        impl AsyncDnsProvider for $i {

            async fn get_record<T>(&self, rtype: RecordType, host: &String) -> Result<Option<T>>
            where
                T: DeserializeOwned + Send + Sync + 'static
            {
                let provider = self.inner.clone();
                let host = host.clone();
                unblock(move || provider.get_record(rtype, &host)).await
            }

            async fn create_record<T>(&self, rtype: RecordType, host: &String, record: &T) -> Result<()>
            where
                T: Serialize + DeserializeOwned + Display + Clone + Send + Sync + 'static
            {
                let provider = self.inner.clone();
                let host = host.clone();
                let record = record.clone();
                unblock(move || provider.create_record(rtype, &host, &record)).await
            }

            async fn update_record<T>(&self, rtype: RecordType, host: &String, record: &T) -> Result<()>
            where
                T: Serialize + DeserializeOwned + Display + Clone + Send + Sync + 'static
            {
                let provider = self.inner.clone();
                let host = host.clone();
                let record = record.clone();
                unblock(move || provider.update_record(rtype, &host, &record)).await
            }

            async fn delete_record(&self, rtype: RecordType, host: &String) -> Result<()>
            {
                let provider = self.inner.clone();
                let host = host.clone();
                unblock(move || provider.delete_record(rtype, &host)).await
            }

            async fn get_txt_record(&self, host: &String) -> Result<Option<String>>
            {
                self.get_record::<String>(RecordType::TXT, host).await
                    .map(|opt| opt.map(|s| crate::strip_quotes(&s)))
            }

            async fn create_txt_record(&self, host: &String, record: &String) -> Result<()>
            {
                self.create_record(RecordType::TXT, host, &crate::ensure_quotes(record)).await
            }

            async fn update_txt_record(&self, host: &String, record: &String) -> Result<()>
            {
                self.update_record(RecordType::TXT, host, &crate::ensure_quotes(record)).await
            }

            async fn delete_txt_record(&self, host: &String) -> Result<()>
            {
                self.delete_record(RecordType::TXT, host).await
            }

            async fn get_a_record(&self, host: &String) -> Result<Option<std::net::Ipv4Addr>>
            {
                self.get_record(RecordType::A, host).await
            }

            async fn create_a_record(&self, host: &String, record: &std::net::Ipv4Addr) -> Result<()>
            {
                self.create_record(RecordType::A, host, record).await
            }

            async fn update_a_record(&self, host: &String, record: &std::net::Ipv4Addr) -> Result<()>
            {
                self.update_record(RecordType::A, host, record).await
            }

            async fn delete_a_record(&self, host: &String) -> Result<()>
            {
                self.delete_record(RecordType::A, host).await
            }

        }

    };
}
pub use async_provider_impl;


#[cfg(test)]
mod tests {
    use crate::strip_quotes;

    use super::*;
    use std::net::Ipv4Addr;
    use random_string::charsets::ALPHA_LOWER;

    #[allow(unused)]
    pub async fn test_create_update_delete_ipv4(client: impl AsyncDnsProvider) -> Result<()> {

        let host = random_string::generate(16, ALPHA_LOWER);

        // Create
        let ip: Ipv4Addr = "10.9.8.7".parse()?;
        client.create_record(RecordType::A, &host, &ip).await?;
        let cur = client.get_record(RecordType::A, &host).await?;
        assert_eq!(Some(ip), cur);

        // Update
        let ip: Ipv4Addr = "10.1.2.3".parse()?;
        client.update_record(RecordType::A, &host, &ip).await?;
        let cur = client.get_record(RecordType::A, &host).await?;
        assert_eq!(Some(ip), cur);

        // Delete
        client.delete_record(RecordType::A, &host).await?;
        let del: Option<Ipv4Addr> = client.get_record(RecordType::A, &host).await?;
        assert!(del.is_none());

        Ok(())
    }

    #[allow(unused)]
    pub async fn test_create_update_delete_txt(client: impl AsyncDnsProvider) -> Result<()> {

        let host = random_string::generate(16, ALPHA_LOWER);

        // Create
        let txt = "\"a text reference\"".to_string();
        client.create_record(RecordType::TXT, &host, &txt).await?;
        let cur: Option<String> = client.get_record(RecordType::TXT, &host).await?;
        assert_eq!(txt, cur.unwrap());

        // Update
        let txt = "\"another text reference\"".to_string();
        client.update_record(RecordType::TXT, &host, &txt).await?;
        let cur: Option<String> = client.get_record(RecordType::TXT, &host).await?;
        assert_eq!(txt, cur.unwrap());

        // Delete
        client.delete_record(RecordType::TXT, &host).await?;
        let del: Option<String> = client.get_record(RecordType::TXT, &host).await?;
        assert!(del.is_none());

        Ok(())
    }

    #[allow(unused)]
    pub async fn test_create_update_delete_txt_default(client: impl AsyncDnsProvider) -> Result<()> {

        let host = random_string::generate(16, ALPHA_LOWER);

        // Create
        let txt = "a text reference".to_string();
        client.create_txt_record(&host, &txt).await?;
        let cur = client.get_txt_record(&host).await?;
        assert_eq!(txt, strip_quotes(&cur.unwrap()));

        // Update
        let txt = "another text reference".to_string();
        client.update_txt_record(&host, &txt).await?;
        let cur = client.get_txt_record(&host).await?;
        assert_eq!(txt, strip_quotes(&cur.unwrap()));

        // Delete
        client.delete_txt_record(&host).await?;
        let del = client.get_txt_record(&host).await?;
        assert!(del.is_none());

        Ok(())
    }


    /// A macro to generate a standard set of tests for an async DNS provider.
    ///
    /// This macro generates a suite of tests that are run against two different async runtimes: `smol` and `tokio`.
    ///
    /// For each runtime, it generates three tests:
    /// - `create_update_v4`: tests creating, updating, and deleting an A record.
    /// - `create_update_txt`: tests creating, updating, and deleting a TXT record.
    /// - `create_update_default`: tests creating, updating, and deleting a TXT record using the default provider methods.
    ///
    /// The tests are conditionally compiled based on the feature flag passed as an argument, and the
    /// `test_smol` and `test_tokio` features, which enable the tests for the respective runtimes.
    ///
    /// # Requirements
    ///
    /// The module that uses this macro must define a `get_client()` function that returns a type
    /// that implements the `AsyncDnsProvider` trait. This function is used by the tests to get a client
    /// for the DNS provider.
    ///
    /// # Arguments
    ///
    /// * `$feat` - A string literal representing the feature flag that enables these tests.
    ///
    /// # Example
    ///
    /// ```
    /// // In your test module
    /// use zone_update::async_impl::{generate_tests, AsyncDnsProvider};
    ///
    /// fn get_client() -> impl AsyncDnsProvider {
    ///     // ... your client implementation
    /// }
    ///
    /// // This will generate the tests, but they will only run if the \"my_provider\" feature is enabled,
    /// // and \"test_smol\" and/or \"test_tokio\" is enabled.
    /// generate_tests!(\"my_provider\");
    /// ```
    #[macro_export]
    macro_rules! generate_async_tests {
        ($feat:literal) => {

            #[cfg(feature = "test_smol")]
            mod smol_tests {
                use super::*;
                use crate::async_impl::tests::*;
                use macro_rules_attribute::apply;
                use smol_macros::test;

                #[apply(test!)]
                #[test_log::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_v4() -> Result<()> {
                    test_create_update_delete_ipv4(get_client()).await?;
                    Ok(())
                }

                #[apply(test!)]
                #[test_log::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_txt() -> Result<()> {
                    test_create_update_delete_txt(get_client()).await?;
                    Ok(())
                }

                #[apply(test!)]
                #[test_log::test]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_default() -> Result<()> {
                    test_create_update_delete_txt_default(get_client()).await?;
                    Ok(())
                }
            }

            #[cfg(feature = "test_tokio")]
            mod tokio_tests {
                use super::*;
                use crate::async_impl::tests::*;

                #[tokio::test]
                #[test_log::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_v4() -> Result<()> {
                    test_create_update_delete_ipv4(get_client()).await?;
                    Ok(())
                }

                #[tokio::test]
                #[test_log::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_txt() -> Result<()> {
                    test_create_update_delete_txt(get_client()).await?;
                    Ok(())
                }

                #[tokio::test]
                #[test_log::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_default() -> Result<()> {
                    test_create_update_delete_txt_default(get_client()).await?;
                    Ok(())
                }
            }

            #[cfg(feature = "test_compio")]
            mod compio_tests {
                use super::*;
                use crate::async_impl::tests::*;

                #[compio::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_v4() -> Result<()> {
                    test_create_update_delete_ipv4(get_client()).await?;
                    Ok(())
                }

                #[compio::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_txt() -> Result<()> {
                    test_create_update_delete_txt(get_client()).await?;
                    Ok(())
                }

                #[compio::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_default() -> Result<()> {
                    test_create_update_delete_txt_default(get_client()).await?;
                    Ok(())
                }
            }

            #[cfg(feature = "test_monoio")]
            mod monoio_tests {
                use super::*;
                use crate::async_impl::tests::*;

                #[monoio::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_v4() -> Result<()> {
                    test_create_update_delete_ipv4(get_client()).await?;
                    Ok(())
                }

                #[monoio::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_txt() -> Result<()> {
                    test_create_update_delete_txt(get_client()).await?;
                    Ok(())
                }

                #[monoio::test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                async fn create_update_default() -> Result<()> {
                    test_create_update_delete_txt_default(get_client()).await?;
                    Ok(())
                }
            }

            #[cfg(feature = "test_glommio")]
            mod glommio_tests {
                use super::*;
                use crate::async_impl::tests::*;

                #[test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                fn create_update_v4() -> Result<()> {
                    let ex = glommio::LocalExecutorBuilder::new(glommio::Placement::Fixed(0)).make().unwrap();
                    ex.run(async move {
                        test_create_update_delete_ipv4(get_client()).await
                    })?;
                    Ok(())
                }

                #[test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                fn create_update_txt() -> Result<()> {
                    let ex = glommio::LocalExecutorBuilder::new(glommio::Placement::Fixed(0)).make().unwrap();
                    ex.run(async move {
                        test_create_update_delete_txt(get_client()).await
                    })?;
                    Ok(())
                }

                #[test]
                #[serial_test::serial]
                #[cfg_attr(not(feature = $feat), ignore = "API test")]
                fn create_update_default() -> Result<()> {
                    let ex = glommio::LocalExecutorBuilder::new(glommio::Placement::Fixed(0)).make().unwrap();
                    ex.run(async move {
                        test_create_update_delete_txt_default(get_client()).await
                    })?;
                    Ok(())
                }
            }
        }
    }
}