manydns 1.0.0

Provider-agnostic DNS zone and record management, inspired by the Go libdns project
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Hetzner Cloud DNS provider implementation.
//!
//! This provider uses the Hetzner Cloud API with Bearer token authentication.
//!
//! # Authentication
//!
//! Requires a Hetzner Cloud API token:
//! - Create a token at: <https://console.hetzner.cloud/projects/*/security/tokens>
//! - The token must have Read & Write permissions for DNS
//!
//! # Example
//!
//! ```no_run
//! use manydns::hetzner::HetznerProvider;
//! use manydns::{Provider, Zone, CreateZone, DeleteZone};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let provider = HetznerProvider::new("your_api_token")?;
//!
//! // List all zones
//! let zones = provider.list_zones().await?;
//! for zone in zones {
//!     println!("Zone: {} (ID: {})", zone.domain(), zone.id());
//! }
//!
//! // Create a new zone
//! let new_zone = provider.create_zone("example.com").await?;
//! println!("Created zone: {}", new_zone.domain());
//!
//! // Delete a zone
//! provider.delete_zone(new_zone.id()).await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Supported Record Types
//!
//! - A (IPv4 address)
//! - AAAA (IPv6 address)
//! - CNAME (Canonical name)
//! - MX (Mail exchange)
//! - NS (Name server)
//! - TXT (Text record)
//! - SRV (Service record)
//! - SOA (Start of authority)
//! - CAA (Certification Authority Authorization)
//! - TLSA (TLS Authentication)
//! - DS (Delegation Signer)
//! - RP (Responsible Person)
//! - HINFO (Host Information)
//! - PTR (Pointer record)
//! - HTTPS (HTTPS Service Binding)
//! - SVCB (Service Binding)
//!
//! # Zone Management
//!
//! Unlike some providers, Hetzner supports creating and deleting zones
//! through the API. See [`CreateZone`] and [`DeleteZone`] traits.
//!
//! # API Reference
//!
//! - [Hetzner Cloud API Documentation](https://docs.hetzner.cloud/)
//! - [DNS Zones](https://docs.hetzner.cloud/reference/cloud#zones)
//! - [DNS RRSets](https://docs.hetzner.cloud/reference/cloud#zone-rrsets)

pub mod api;

use std::error::Error as StdErr;
use std::sync::Arc;

use crate::{
    CreateRecord, CreateRecordError, CreateZone, CreateZoneError, DeleteRecord, DeleteRecordError,
    DeleteZone, DeleteZoneError, Provider, Record, RecordData, RetrieveRecordError,
    RetrieveZoneError, Zone,
};

/// Supported record types for Hetzner Cloud DNS.
const SUPPORTED_RECORD_TYPES: &[&str; 16] = &[
    "A", "AAAA", "NS", "MX", "CNAME", "RP", "TXT", "SOA", "HINFO", "SRV", "TLSA", "DS", "CAA",
    "PTR", "HTTPS", "SVCB",
];

/// Hetzner Cloud DNS provider.
///
/// Uses the Hetzner Cloud API with Bearer token authentication.
#[derive(Debug)]
pub struct HetznerProvider {
    api_client: Arc<api::Client>,
}

impl Clone for HetznerProvider {
    fn clone(&self) -> Self {
        HetznerProvider {
            api_client: Arc::clone(&self.api_client),
        }
    }
}

impl HetznerProvider {
    /// Creates a new Hetzner Cloud DNS provider.
    ///
    /// # Arguments
    ///
    /// * `api_key` - Hetzner Cloud API token (Bearer token)
    ///
    /// # Example
    ///
    /// ```no_run
    /// use manydns::hetzner::HetznerProvider;
    ///
    /// let provider = HetznerProvider::new("your_api_token").unwrap();
    /// ```
    pub fn new(api_key: &str) -> Result<Self, Box<dyn StdErr>> {
        let api_client = api::Client::new(api_key)?;
        Ok(Self {
            api_client: Arc::new(api_client),
        })
    }

    /// Creates a new Hetzner Cloud DNS provider with a custom API base URL.
    ///
    /// This is primarily useful for testing with mock servers.
    ///
    /// # Arguments
    ///
    /// * `api_key` - Hetzner Cloud API token
    /// * `base_url` - Custom base URL for the API
    pub fn with_base_url(api_key: &str, base_url: &str) -> Result<Self, Box<dyn StdErr>> {
        let api_client = api::Client::with_base_url(api_key, base_url)?;
        Ok(Self {
            api_client: Arc::new(api_client),
        })
    }
}

impl Provider for HetznerProvider {
    type Zone = HetznerZone;
    type CustomRetrieveError = reqwest::Error;

    async fn get_zone(
        &self,
        zone_id: &str,
    ) -> Result<Self::Zone, RetrieveZoneError<Self::CustomRetrieveError>> {
        let response = self
            .api_client
            .retrieve_zone(zone_id)
            .await
            .map_err(|err| {
                if err.is_status() {
                    return match err.status().unwrap() {
                        reqwest::StatusCode::NOT_FOUND => RetrieveZoneError::NotFound,
                        reqwest::StatusCode::UNAUTHORIZED => RetrieveZoneError::Unauthorized,
                        _ => RetrieveZoneError::Custom(err),
                    };
                }
                RetrieveZoneError::Custom(err)
            })?;

        Ok(HetznerZone::from_api(
            self.api_client.clone(),
            response.zone,
        ))
    }

    async fn list_zones(
        &self,
    ) -> Result<Vec<Self::Zone>, RetrieveZoneError<Self::CustomRetrieveError>> {
        let mut zones = Vec::new();
        let mut total: Option<usize> = None;
        let mut page = 1;

        loop {
            let result =
                self.api_client
                    .retrieve_zones(page, 100)
                    .await
                    .map_err(|err| {
                        if err.is_status() {
                            return match err.status().unwrap() {
                                reqwest::StatusCode::NOT_FOUND => RetrieveZoneError::NotFound,
                                reqwest::StatusCode::UNAUTHORIZED
                                | reqwest::StatusCode::FORBIDDEN => RetrieveZoneError::Unauthorized,
                                _ => RetrieveZoneError::Custom(err),
                            };
                        }
                        RetrieveZoneError::Custom(err)
                    });

            match result {
                Ok(response) => {
                    if total.is_none() {
                        total = Some(response.meta.pagination.total_entries as usize);
                    }

                    zones.extend(
                        response
                            .zones
                            .into_iter()
                            .map(|zone| HetznerZone::from_api(self.api_client.clone(), zone)),
                    );
                }
                Err(err) => {
                    if let RetrieveZoneError::NotFound = err {
                        break;
                    }
                    return Err(err);
                }
            }

            if total.is_some_and(|t| zones.len() == t) {
                break;
            }

            page += 1;
        }

        Ok(zones)
    }
}

impl CreateZone for HetznerProvider {
    type CustomCreateError = reqwest::Error;

    async fn create_zone(
        &self,
        domain: &str,
    ) -> Result<Self::Zone, CreateZoneError<Self::CustomCreateError>> {
        let response = self
            .api_client
            .create_zone(domain, None)
            .await
            .map_err(|err| {
                if err.is_status() {
                    return match err.status().unwrap() {
                        reqwest::StatusCode::UNAUTHORIZED => CreateZoneError::Unauthorized,
                        reqwest::StatusCode::UNPROCESSABLE_ENTITY => {
                            CreateZoneError::InvalidDomainName
                        }
                        _ => CreateZoneError::Custom(err),
                    };
                }
                CreateZoneError::Custom(err)
            })?;

        Ok(HetznerZone::from_api(
            self.api_client.clone(),
            response.zone,
        ))
    }
}

impl DeleteZone for HetznerProvider {
    type CustomDeleteError = reqwest::Error;

    async fn delete_zone(
        &self,
        zone_id: &str,
    ) -> Result<(), DeleteZoneError<Self::CustomDeleteError>> {
        self.api_client.delete_zone(zone_id).await.map_err(|err| {
            if err.is_status() {
                return match err.status().unwrap() {
                    reqwest::StatusCode::NOT_FOUND => DeleteZoneError::NotFound,
                    reqwest::StatusCode::UNAUTHORIZED => DeleteZoneError::Unauthorized,
                    _ => DeleteZoneError::Custom(err),
                };
            }
            DeleteZoneError::Custom(err)
        })
    }
}

/// Represents a DNS zone in Hetzner Cloud DNS.
///
/// This struct provides methods for managing DNS records within a zone,
/// including listing, creating, and deleting records.
///
/// # Zone Status
///
/// Hetzner zones have a status that can be:
/// - `Ok` - The zone is active and DNS resolution works
/// - `Pending` - The zone is awaiting setup
/// - `Failed` - Zone setup failed
///
/// # RRSet-based API
///
/// The Hetzner Cloud API uses RRSets (Resource Record Sets) instead of
/// individual records. An RRSet is a group of records with the same name
/// and type. This implementation handles the conversion between the
/// manydns record model and Hetzner's RRSet model.
///
/// # Example
///
/// ```rust,no_run
/// use manydns::hetzner::HetznerProvider;
/// use manydns::{Provider, Zone, CreateRecord, DeleteRecord, RecordData};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let provider = HetznerProvider::new("your-api-token")?;
///
/// // Get an existing zone
/// let zone = provider.get_zone("example.com").await?;
/// println!("Zone ID: {}", zone.id());
/// println!("Domain: {}", zone.domain());
///
/// // List all records in the zone
/// let records = zone.list_records().await?;
/// for record in &records {
///     println!("{:?}", record);
/// }
///
/// // Create a new A record
/// let data = RecordData::A("1.2.3.4".parse()?);
/// zone.create_record("www", &data, 300).await?;
///
/// // Delete a record by ID (format: "name/type/value")
/// zone.delete_record("www/A/1.2.3.4").await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct HetznerZone {
    api_client: Arc<api::Client>,
    repr: api::Zone,
    /// Cached zone ID as string for the Zone trait.
    zone_id_str: String,
}

impl HetznerZone {
    /// Creates a new HetznerZone from API response data.
    fn from_api(api_client: Arc<api::Client>, zone: api::Zone) -> Self {
        let zone_id_str = zone.id.to_string();
        Self {
            api_client,
            repr: zone,
            zone_id_str,
        }
    }
}

impl Zone for HetznerZone {
    type CustomRetrieveError = reqwest::Error;

    fn id(&self) -> &str {
        &self.zone_id_str
    }

    fn domain(&self) -> &str {
        &self.repr.name
    }

    async fn list_records(
        &self,
    ) -> Result<Vec<Record>, RetrieveRecordError<Self::CustomRetrieveError>> {
        let mut records = Vec::new();
        let mut total: Option<usize> = None;
        let mut page = 1;

        loop {
            let result = self
                .api_client
                .retrieve_rrsets(&self.zone_id_str, page, 100)
                .await
                .map_err(|err| {
                    if err.is_status() {
                        return match err.status().unwrap() {
                            reqwest::StatusCode::NOT_FOUND => RetrieveRecordError::NotFound,
                            reqwest::StatusCode::UNAUTHORIZED | reqwest::StatusCode::FORBIDDEN => {
                                RetrieveRecordError::Unauthorized
                            }
                            _ => RetrieveRecordError::Custom(err),
                        };
                    }
                    RetrieveRecordError::Custom(err)
                });

            match result {
                Ok(response) => {
                    if total.is_none() {
                        total = Some(response.meta.pagination.total_entries as usize);
                    }

                    let is_last_page =
                        response.meta.pagination.page >= response.meta.pagination.last_page;

                    // Convert RRSets to individual records
                    for rrset in &response.rrsets {
                        let ttl = rrset.ttl.unwrap_or(self.repr.ttl);
                        for record_value in &rrset.records {
                            // Create a unique ID: "name/type/value"
                            let record_id =
                                format!("{}/{}/{}", rrset.name, rrset.typ, record_value.value);
                            records.push(Record {
                                id: record_id,
                                host: rrset.name.clone(),
                                data: RecordData::from_raw(&rrset.typ, &record_value.value),
                                ttl,
                            });
                        }
                    }

                    if is_last_page {
                        break;
                    }
                }
                Err(err) => {
                    if let RetrieveRecordError::NotFound = err {
                        break;
                    }
                    return Err(err);
                }
            }

            page += 1;
        }

        Ok(records)
    }

    async fn get_record(
        &self,
        record_id: &str,
    ) -> Result<Record, RetrieveRecordError<Self::CustomRetrieveError>> {
        // Parse record ID format: "name/type/value"
        let parts: Vec<&str> = record_id.splitn(3, '/').collect();
        if parts.len() != 3 {
            return Err(RetrieveRecordError::NotFound);
        }
        let (name, typ, value) = (parts[0], parts[1], parts[2]);

        let response = self
            .api_client
            .retrieve_rrset(&self.zone_id_str, name, typ)
            .await
            .map_err(|err| {
                if err.is_status() {
                    return match err.status().unwrap() {
                        reqwest::StatusCode::NOT_FOUND => RetrieveRecordError::NotFound,
                        reqwest::StatusCode::UNAUTHORIZED => RetrieveRecordError::Unauthorized,
                        _ => RetrieveRecordError::Custom(err),
                    };
                }
                RetrieveRecordError::Custom(err)
            })?;

        // Find the specific record value in the RRSet
        let rrset = &response.rrset;
        let record_value = rrset
            .records
            .iter()
            .find(|r| r.value == value)
            .ok_or(RetrieveRecordError::NotFound)?;

        Ok(Record {
            id: record_id.to_string(),
            host: rrset.name.clone(),
            data: RecordData::from_raw(&rrset.typ, &record_value.value),
            ttl: rrset.ttl.unwrap_or(self.repr.ttl),
        })
    }
}

/// Format a record value for the Hetzner Cloud API.
///
/// TXT records must be wrapped in double quotes per Hetzner's requirements.
fn format_value_for_api(data: &RecordData) -> String {
    match data {
        RecordData::TXT(val) => {
            // Hetzner requires TXT values to be double-quoted
            if val.starts_with('"') && val.ends_with('"') {
                val.clone()
            } else {
                format!("\"{}\"", val)
            }
        }
        _ => data.get_value(),
    }
}

impl CreateRecord for HetznerZone {
    type CustomCreateError = reqwest::Error;

    async fn create_record(
        &self,
        host: &str,
        data: &RecordData,
        ttl: u64,
    ) -> Result<Record, CreateRecordError<Self::CustomCreateError>> {
        let typ = data.get_type();
        if !SUPPORTED_RECORD_TYPES.contains(&typ) {
            return Err(CreateRecordError::UnsupportedType);
        }

        let value = format_value_for_api(data);
        let record_value = api::RecordValue::new(&value);

        let opt_ttl = if ttl != self.repr.ttl {
            Some(ttl)
        } else {
            None
        };

        // Try to add to existing RRSet first (this creates if it doesn't exist)
        let _response = self
            .api_client
            .add_records_to_rrset(&self.zone_id_str, host, typ, vec![record_value], opt_ttl)
            .await
            .map_err(|err| {
                if err.is_status() {
                    return match err.status().unwrap() {
                        reqwest::StatusCode::UNAUTHORIZED => CreateRecordError::Unauthorized,
                        reqwest::StatusCode::UNPROCESSABLE_ENTITY => {
                            CreateRecordError::InvalidRecord
                        }
                        _ => CreateRecordError::Custom(err),
                    };
                }
                CreateRecordError::Custom(err)
            })?;

        // Create the record ID in our format (use API value for consistency)
        let record_id = format!("{}/{}/{}", host, typ, value);

        Ok(Record {
            id: record_id,
            host: host.to_string(),
            data: data.clone(),
            ttl,
        })
    }
}

impl DeleteRecord for HetznerZone {
    type CustomDeleteError = reqwest::Error;

    async fn delete_record(
        &self,
        record_id: &str,
    ) -> Result<(), DeleteRecordError<Self::CustomDeleteError>> {
        // Parse record ID format: "name/type/value"
        let parts: Vec<&str> = record_id.splitn(3, '/').collect();
        if parts.len() != 3 {
            return Err(DeleteRecordError::NotFound);
        }
        let (name, typ, value) = (parts[0], parts[1], parts[2]);

        // Remove this specific record from the RRSet
        let record_value = api::RecordValue::new(value);

        self.api_client
            .remove_records_from_rrset(&self.zone_id_str, name, typ, vec![record_value])
            .await
            .map_err(|err| {
                if err.is_status() {
                    return match err.status().unwrap() {
                        reqwest::StatusCode::NOT_FOUND => DeleteRecordError::NotFound,
                        reqwest::StatusCode::UNAUTHORIZED => DeleteRecordError::Unauthorized,
                        _ => DeleteRecordError::Custom(err),
                    };
                }
                DeleteRecordError::Custom(err)
            })?;

        Ok(())
    }
}