dnsync 0.2.1

DNS Sync and Control with MCP
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
//! UniFi Network Integration API DNS-policy DTOs.
//!
//! These mirror the `IntegrationDnsPolicyDto` / `IntegrationDnsPolicyPageDto`
//! schemas published in the UniFi Network OpenAPI spec (v10.3.58). Only the
//! fields needed by dnsync are captured; unknown fields are tolerated so we
//! survive minor UniFi schema additions.

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// UniFi DNS policy types as published by the OpenAPI spec.
///
/// `FORWARD_DOMAIN` is intentionally kept distinct from RR types so dnsync
/// can treat it as a resolver forward rule rather than a DNS record.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum UnifiDnsPolicyType {
    #[serde(rename = "A_RECORD")]
    ARecord,
    #[serde(rename = "AAAA_RECORD")]
    AaaaRecord,
    #[serde(rename = "CNAME_RECORD")]
    CnameRecord,
    #[serde(rename = "MX_RECORD")]
    MxRecord,
    #[serde(rename = "TXT_RECORD")]
    TxtRecord,
    #[serde(rename = "SRV_RECORD")]
    SrvRecord,
    #[serde(rename = "FORWARD_DOMAIN")]
    ForwardDomain,
}

impl UnifiDnsPolicyType {
    /// UniFi wire string for this policy type.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::ARecord => "A_RECORD",
            Self::AaaaRecord => "AAAA_RECORD",
            Self::CnameRecord => "CNAME_RECORD",
            Self::MxRecord => "MX_RECORD",
            Self::TxtRecord => "TXT_RECORD",
            Self::SrvRecord => "SRV_RECORD",
            Self::ForwardDomain => "FORWARD_DOMAIN",
        }
    }

    /// dnsync record_type label for this policy type. Returns `"FORWARD_DOMAIN"`
    /// for forward rules — callers can branch on this to skip them where DNS
    /// RRsets are expected.
    pub fn dnsync_record_type(&self) -> &'static str {
        match self {
            Self::ARecord => "A",
            Self::AaaaRecord => "AAAA",
            Self::CnameRecord => "CNAME",
            Self::MxRecord => "MX",
            Self::TxtRecord => "TXT",
            Self::SrvRecord => "SRV",
            Self::ForwardDomain => "FORWARD_DOMAIN",
        }
    }
}

/// One DNS policy returned by `GET /sites/{siteId}/dns/policies/...`.
///
/// All `Option` fields are populated only for the policy types they apply to
/// — e.g. `ipv4Address` is set for `A_RECORD` only. Extra fields not modelled
/// here are accepted and ignored.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnifiDnsPolicy {
    pub id: String,
    #[serde(rename = "type")]
    pub policy_type: UnifiDnsPolicyType,
    pub enabled: bool,
    pub domain: String,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ipv4_address: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ipv6_address: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_domain: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mail_server_domain: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub server_domain: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ip_address: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ttl_seconds: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub priority: Option<u16>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub service: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub protocol: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub port: Option<u16>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub weight: Option<u16>,
}

/// Paginated wrapper as returned by `GET /sites/{siteId}/dns/policies`.
///
/// UniFi documents the fields as `offset`, `limit`, `count`, `totalCount`,
/// `data`. Some firmware revisions omit `count`/`totalCount` for empty pages,
/// so both are `Option`-typed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnifiDnsPolicyPage {
    #[serde(default)]
    pub offset: u32,
    #[serde(default)]
    pub limit: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub count: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(rename = "totalCount")]
    pub total_count: Option<u32>,
    #[serde(default)]
    pub data: Vec<UnifiDnsPolicy>,
}

impl UnifiDnsPolicyPage {
    /// Number of items in this page. Falls back to `data.len()` when the
    /// controller omits the `count` field.
    pub fn page_count(&self) -> u32 {
        self.count.unwrap_or(self.data.len() as u32)
    }

    /// Total items across all pages, or `None` if the controller omits
    /// `totalCount`. Callers must rely on the empty-page sentinel instead.
    pub fn total(&self) -> Option<u32> {
        self.total_count
    }
}

/// Convert a raw JSON value (the parsed UniFi response body) into our
/// `UnifiDnsPolicyPage` shape. Falls back to a single-page wrapper when the
/// controller returns a bare array.
pub fn parse_page(value: Value) -> Result<UnifiDnsPolicyPage, serde_json::Error> {
    if value.is_array() {
        let data: Vec<UnifiDnsPolicy> = serde_json::from_value(value)?;
        let len = data.len() as u32;
        return Ok(UnifiDnsPolicyPage {
            offset: 0,
            limit: len,
            count: Some(len),
            total_count: Some(len),
            data,
        });
    }
    serde_json::from_value(value)
}

/// One UniFi site as returned by `GET /sites`.
///
/// Different controller firmwares expose slightly different shapes — most have
/// `id` + `name`, some also include `internalReference`. All non-`id` fields
/// are optional so we tolerate that variation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnifiSite {
    pub id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub internal_reference: Option<String>,
}

impl UnifiSite {
    /// Best human-readable label for this site — `name` if present, otherwise
    /// the internal reference, otherwise the UUID.
    pub fn display_name(&self) -> &str {
        self.name
            .as_deref()
            .or(self.internal_reference.as_deref())
            .unwrap_or(&self.id)
    }
}

/// Paginated wrapper around `GET /sites`. Mirrors `UnifiDnsPolicyPage` so the
/// same pagination loop in the client can drive both.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnifiSitePage {
    #[serde(default)]
    pub offset: u32,
    #[serde(default)]
    pub limit: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub count: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(rename = "totalCount")]
    pub total_count: Option<u32>,
    #[serde(default)]
    pub data: Vec<UnifiSite>,
}

impl UnifiSitePage {
    pub fn total(&self) -> Option<u32> {
        self.total_count
    }
}

/// Parse a `GET /sites` response into `UnifiSitePage`, tolerating a bare
/// array (some firmware revisions skip the envelope).
pub fn parse_site_page(value: Value) -> Result<UnifiSitePage, serde_json::Error> {
    if value.is_array() {
        let data: Vec<UnifiSite> = serde_json::from_value(value)?;
        let len = data.len() as u32;
        return Ok(UnifiSitePage {
            offset: 0,
            limit: len,
            count: Some(len),
            total_count: Some(len),
            data,
        });
    }
    serde_json::from_value(value)
}

/// Find the site that matches `needle` against its UUID, `name`, or
/// `internalReference`. Comparisons are case-insensitive so configs can
/// store `"Default"` even when the controller reports `"default"`.
pub fn match_site<'a>(sites: &'a [UnifiSite], needle: &str) -> Option<&'a UnifiSite> {
    let needle = needle.trim();
    if needle.is_empty() {
        return None;
    }
    sites.iter().find(|s| {
        s.id.eq_ignore_ascii_case(needle)
            || s.name
                .as_deref()
                .is_some_and(|n| n.eq_ignore_ascii_case(needle))
            || s.internal_reference
                .as_deref()
                .is_some_and(|n| n.eq_ignore_ascii_case(needle))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn a_record_policy_round_trips() {
        let v = json!({
            "id": "policy-1",
            "type": "A_RECORD",
            "enabled": true,
            "domain": "www.example.com",
            "ipv4Address": "192.168.1.10",
            "ttlSeconds": 300
        });
        let p: UnifiDnsPolicy = serde_json::from_value(v).unwrap();
        assert_eq!(p.id, "policy-1");
        assert_eq!(p.policy_type, UnifiDnsPolicyType::ARecord);
        assert!(p.enabled);
        assert_eq!(p.ipv4_address.as_deref(), Some("192.168.1.10"));
        assert_eq!(p.ttl_seconds, Some(300));
    }

    #[test]
    fn forward_domain_policy_parses() {
        let v = json!({
            "id": "fwd-1",
            "type": "FORWARD_DOMAIN",
            "enabled": true,
            "domain": "lan.example.com",
            "ipAddress": "192.168.1.1"
        });
        let p: UnifiDnsPolicy = serde_json::from_value(v).unwrap();
        assert_eq!(p.policy_type, UnifiDnsPolicyType::ForwardDomain);
        assert_eq!(p.ip_address.as_deref(), Some("192.168.1.1"));
    }

    #[test]
    fn srv_policy_parses_all_fields() {
        let v = json!({
            "id": "srv-1",
            "type": "SRV_RECORD",
            "enabled": false,
            "domain": "_sip._tcp.example.com",
            "serverDomain": "sip.example.com",
            "service": "_sip",
            "protocol": "_tcp",
            "port": 5060,
            "priority": 10,
            "weight": 20,
            "ttlSeconds": 600
        });
        let p: UnifiDnsPolicy = serde_json::from_value(v).unwrap();
        assert_eq!(p.policy_type, UnifiDnsPolicyType::SrvRecord);
        assert!(!p.enabled);
        assert_eq!(p.server_domain.as_deref(), Some("sip.example.com"));
        assert_eq!(p.service.as_deref(), Some("_sip"));
        assert_eq!(p.protocol.as_deref(), Some("_tcp"));
        assert_eq!(p.port, Some(5060));
        assert_eq!(p.priority, Some(10));
        assert_eq!(p.weight, Some(20));
    }

    #[test]
    fn unknown_fields_are_tolerated() {
        let v = json!({
            "id": "p",
            "type": "TXT_RECORD",
            "enabled": true,
            "domain": "_acme.example.com",
            "text": "challenge",
            "futureField": 42
        });
        let p: UnifiDnsPolicy = serde_json::from_value(v).unwrap();
        assert_eq!(p.text.as_deref(), Some("challenge"));
    }

    #[test]
    fn page_parses_full_envelope() {
        let v = json!({
            "offset": 0,
            "limit": 25,
            "count": 1,
            "totalCount": 1,
            "data": [
                {"id": "p", "type": "A_RECORD", "enabled": true, "domain": "x", "ipv4Address": "1.1.1.1"}
            ]
        });
        let page = parse_page(v).unwrap();
        assert_eq!(page.limit, 25);
        assert_eq!(page.page_count(), 1);
        assert_eq!(page.total(), Some(1));
    }

    #[test]
    fn page_parses_bare_array_fallback() {
        let v = json!([
            {"id": "p", "type": "A_RECORD", "enabled": true, "domain": "x", "ipv4Address": "1.1.1.1"}
        ]);
        let page = parse_page(v).unwrap();
        assert_eq!(page.data.len(), 1);
        assert_eq!(page.page_count(), 1);
    }

    #[test]
    fn policy_type_dnsync_label_maps_correctly() {
        assert_eq!(UnifiDnsPolicyType::ARecord.dnsync_record_type(), "A");
        assert_eq!(UnifiDnsPolicyType::AaaaRecord.dnsync_record_type(), "AAAA");
        assert_eq!(
            UnifiDnsPolicyType::CnameRecord.dnsync_record_type(),
            "CNAME"
        );
        assert_eq!(UnifiDnsPolicyType::MxRecord.dnsync_record_type(), "MX");
        assert_eq!(UnifiDnsPolicyType::TxtRecord.dnsync_record_type(), "TXT");
        assert_eq!(UnifiDnsPolicyType::SrvRecord.dnsync_record_type(), "SRV");
        assert_eq!(
            UnifiDnsPolicyType::ForwardDomain.dnsync_record_type(),
            "FORWARD_DOMAIN"
        );
    }

    // ── Site listing ────────────────────────────────────────────────────────

    fn make_sites() -> Vec<UnifiSite> {
        vec![
            UnifiSite {
                id: "11111111-1111-1111-1111-111111111111".to_string(),
                name: Some("Default".to_string()),
                internal_reference: Some("default".to_string()),
            },
            UnifiSite {
                id: "22222222-2222-2222-2222-222222222222".to_string(),
                name: Some("Lab".to_string()),
                internal_reference: None,
            },
        ]
    }

    #[test]
    fn site_page_parses_full_envelope() {
        let v = json!({
            "offset": 0,
            "limit": 25,
            "count": 1,
            "totalCount": 1,
            "data": [
                {"id": "abc", "name": "Default", "internalReference": "default"}
            ]
        });
        let page = parse_site_page(v).unwrap();
        assert_eq!(page.data.len(), 1);
        assert_eq!(page.data[0].name.as_deref(), Some("Default"));
    }

    #[test]
    fn site_page_tolerates_bare_array() {
        let v = json!([{ "id": "abc", "name": "Default" }]);
        let page = parse_site_page(v).unwrap();
        assert_eq!(page.data.len(), 1);
    }

    #[test]
    fn site_page_tolerates_missing_optional_fields() {
        let v = json!({ "data": [{ "id": "minimal" }] });
        let page = parse_site_page(v).unwrap();
        assert!(page.data[0].name.is_none());
        assert_eq!(page.data[0].display_name(), "minimal");
    }

    #[test]
    fn match_site_finds_by_uuid() {
        let sites = make_sites();
        let found = match_site(&sites, "22222222-2222-2222-2222-222222222222").unwrap();
        assert_eq!(found.name.as_deref(), Some("Lab"));
    }

    #[test]
    fn match_site_finds_by_name_case_insensitively() {
        let sites = make_sites();
        let found = match_site(&sites, "default").unwrap();
        assert_eq!(found.id, "11111111-1111-1111-1111-111111111111");
    }

    #[test]
    fn match_site_finds_by_internal_reference() {
        let sites = make_sites();
        // The name is "Default" with capital D; internalReference is lowercase
        // "default". A configured value of "default" should still resolve.
        let found = match_site(&sites, "DEFAULT").unwrap();
        assert_eq!(found.id, "11111111-1111-1111-1111-111111111111");
    }

    #[test]
    fn match_site_returns_none_for_unknown() {
        let sites = make_sites();
        assert!(match_site(&sites, "Missing").is_none());
    }

    #[test]
    fn match_site_rejects_empty_needle() {
        let sites = make_sites();
        assert!(match_site(&sites, "   ").is_none());
        assert!(match_site(&sites, "").is_none());
    }

    #[test]
    fn site_display_name_prefers_name() {
        let s = UnifiSite {
            id: "id".into(),
            name: Some("Pretty".into()),
            internal_reference: Some("ref".into()),
        };
        assert_eq!(s.display_name(), "Pretty");
    }

    #[test]
    fn site_display_name_falls_back_to_internal_reference() {
        let s = UnifiSite {
            id: "id".into(),
            name: None,
            internal_reference: Some("ref".into()),
        };
        assert_eq!(s.display_name(), "ref");
    }

    #[test]
    fn site_display_name_falls_back_to_id() {
        let s = UnifiSite {
            id: "the-id".into(),
            name: None,
            internal_reference: None,
        };
        assert_eq!(s.display_name(), "the-id");
    }
}