Skip to main content

dnslib/core/dns/
zones.rs

1use clap::Args;
2use schemars::JsonSchema;
3use serde::Deserialize;
4use serde_json::Value;
5
6use crate::core::{
7    dns::service::{ZoneExport, ZoneImport, ZoneRead, ZoneWrite},
8    error::Result,
9};
10
11/// Shared DNS zone summary.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct ZoneSummary {
14    pub name: String,
15    pub zone_type: String,
16    pub disabled: bool,
17}
18
19/// Overwrite flags for zone-file imports. Used by both CLI and MCP.
20#[derive(Debug, Clone, Args, Deserialize, JsonSchema)]
21pub struct ZoneImportOptions {
22    /// Overwrite existing record sets for imported types (default: true)
23    #[arg(long, default_value_t = true)]
24    #[serde(default = "default_overwrite")]
25    pub overwrite: bool,
26    /// Delete all existing records before importing — clean replace (default: false)
27    #[arg(long, default_value_t = false)]
28    #[serde(default)]
29    pub overwrite_zone: bool,
30    /// Use the SOA serial from the file instead of auto-incrementing (default: false)
31    #[arg(long, default_value_t = false)]
32    #[serde(default)]
33    pub overwrite_soa_serial: bool,
34}
35
36fn default_overwrite() -> bool {
37    true
38}
39
40/// List DNS zones through a vendor-neutral zone reader.
41///
42/// # Errors
43///
44/// Returns any error reported by the selected DNS backend.
45pub async fn list_zones<C: ZoneRead + ?Sized>(
46    client: &C,
47    page: u32,
48    per_page: u32,
49) -> Result<Value> {
50    client.list_zones(page, per_page).await
51}
52
53/// Create a DNS zone through a vendor-neutral zone writer.
54///
55/// # Errors
56///
57/// Returns any error reported by the selected DNS backend.
58pub async fn create_zone<C: ZoneWrite + ?Sized>(
59    client: &C,
60    zone: &str,
61    zone_type: &str,
62) -> Result<Value> {
63    client.create_zone(zone, zone_type).await
64}
65
66/// Delete a DNS zone through a vendor-neutral zone writer.
67///
68/// # Errors
69///
70/// Returns any error reported by the selected DNS backend.
71pub async fn delete_zone<C: ZoneWrite + ?Sized>(client: &C, zone: &str) -> Result<Value> {
72    client.delete_zone(zone).await
73}
74
75/// Enable a DNS zone through a vendor-neutral zone writer.
76///
77/// # Errors
78///
79/// Returns any error reported by the selected DNS backend.
80pub async fn enable_zone<C: ZoneWrite + ?Sized>(client: &C, zone: &str) -> Result<Value> {
81    client.enable_zone(zone).await
82}
83
84/// Disable a DNS zone through a vendor-neutral zone writer.
85///
86/// # Errors
87///
88/// Returns any error reported by the selected DNS backend.
89pub async fn disable_zone<C: ZoneWrite + ?Sized>(client: &C, zone: &str) -> Result<Value> {
90    client.disable_zone(zone).await
91}
92
93/// Import a zone file through a vendor-neutral zone importer.
94///
95/// # Errors
96///
97/// Returns any error reported by the selected DNS backend.
98pub async fn import_zone_file<C: ZoneImport + ?Sized>(
99    client: &C,
100    zone: &str,
101    file_name: String,
102    file_bytes: Vec<u8>,
103    overwrite: bool,
104    overwrite_zone: bool,
105    overwrite_soa_serial: bool,
106) -> Result<Value> {
107    client
108        .import_zone_file(
109            zone,
110            file_name,
111            file_bytes,
112            overwrite,
113            overwrite_zone,
114            overwrite_soa_serial,
115        )
116        .await
117}
118
119/// Export a zone file through a vendor-neutral zone exporter.
120///
121/// # Errors
122///
123/// Returns any error reported by the selected DNS backend.
124pub async fn export_zone_file<C: ZoneExport + ?Sized>(client: &C, zone: &str) -> Result<String> {
125    client.export_zone_file(zone).await
126}