cloudflare/endpoints/
dns.rs

1use crate::framework::{
2    endpoint::{Endpoint, Method},
3    response::ApiResult,
4};
5/// https://api.cloudflare.com/#dns-records-for-a-zone-properties
6use crate::framework::{OrderDirection, SearchMatch};
7use chrono::offset::Utc;
8use chrono::DateTime;
9use std::net::{Ipv4Addr, Ipv6Addr};
10
11/// List DNS Records
12/// https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
13pub struct ListDnsRecords<'a> {
14    pub zone_identifier: &'a str,
15    pub params: ListDnsRecordsParams,
16}
17impl<'a> Endpoint<Vec<DnsRecord>, ListDnsRecordsParams> for ListDnsRecords<'a> {
18    fn method(&self) -> Method {
19        Method::Get
20    }
21    fn path(&self) -> String {
22        format!("zones/{}/dns_records", self.zone_identifier)
23    }
24    fn query(&self) -> Option<ListDnsRecordsParams> {
25        Some(self.params.clone())
26    }
27}
28
29/// Create DNS Record
30/// https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record
31pub struct CreateDnsRecord<'a> {
32    pub zone_identifier: &'a str,
33    pub params: CreateDnsRecordParams<'a>,
34}
35
36impl<'a> Endpoint<DnsRecord, (), CreateDnsRecordParams<'a>> for CreateDnsRecord<'a> {
37    fn method(&self) -> Method {
38        Method::Post
39    }
40    fn path(&self) -> String {
41        format!("zones/{}/dns_records", self.zone_identifier)
42    }
43    fn body(&self) -> Option<CreateDnsRecordParams<'a>> {
44        Some(self.params.clone())
45    }
46}
47
48#[serde_with::skip_serializing_none]
49#[derive(Serialize, Clone, Debug)]
50pub struct CreateDnsRecordParams<'a> {
51    /// Time to live for DNS record. Value of 1 is 'automatic'
52    pub ttl: Option<u32>,
53    /// Used with some records like MX and SRV to determine priority.
54    /// If you do not supply a priority for an MX record, a default value of 0 will be set
55    pub priority: Option<u16>,
56    /// Whether the record is receiving the performance and security benefits of Cloudflare
57    pub proxied: Option<bool>,
58    /// DNS record name
59    pub name: &'a str,
60    /// Type of the DNS record that also holds the record value
61    #[serde(flatten)]
62    pub content: DnsContent,
63}
64
65/// Delete DNS Record
66/// https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record
67pub struct DeleteDnsRecord<'a> {
68    pub zone_identifier: &'a str,
69    pub identifier: &'a str,
70}
71impl<'a> Endpoint<DeleteDnsRecordResponse> for DeleteDnsRecord<'a> {
72    fn method(&self) -> Method {
73        Method::Delete
74    }
75    fn path(&self) -> String {
76        format!(
77            "zones/{}/dns_records/{}",
78            self.zone_identifier, self.identifier
79        )
80    }
81}
82
83#[derive(Serialize, Clone, Debug)]
84#[serde(rename_all = "lowercase")]
85pub enum ListDnsRecordsOrder {
86    Type,
87    Name,
88    Content,
89    Ttl,
90    Proxied,
91}
92
93#[serde_with::skip_serializing_none]
94#[derive(Serialize, Clone, Debug, Default)]
95pub struct ListDnsRecordsParams {
96    pub record_type: Option<DnsContent>,
97    pub name: Option<String>,
98    pub page: Option<u32>,
99    pub per_page: Option<u32>,
100    pub order: Option<ListDnsRecordsOrder>,
101    pub direction: Option<OrderDirection>,
102    #[serde(rename = "match")]
103    pub search_match: Option<SearchMatch>,
104}
105
106/// Extra Cloudflare-specific information about the record
107#[derive(Deserialize, Debug)]
108pub struct Meta {
109    /// Will exist if Cloudflare automatically added this DNS record during initial setup.
110    pub auto_added: bool,
111}
112
113/// Type of the DNS record, along with the associated value.
114/// When we add support for other types (LOC/SRV/...), the `meta` field should also probably be encoded
115/// here as an associated, strongly typed value.
116#[derive(Deserialize, Serialize, Clone, Debug)]
117#[serde(tag = "type")]
118pub enum DnsContent {
119    A { content: Ipv4Addr },
120    AAAA { content: Ipv6Addr },
121    CNAME { content: String },
122    NS { content: String },
123    MX { content: String, priority: u16 },
124    TXT { content: String },
125}
126
127#[derive(Deserialize, Debug)]
128pub struct DeleteDnsRecordResponse {
129    /// DNS record identifier tag
130    pub id: String,
131}
132
133#[derive(Deserialize, Debug)]
134pub struct DnsRecord {
135    /// Extra Cloudflare-specific information about the record
136    pub meta: Meta,
137    /// Whether this record can be modified/deleted (true means it's managed by Cloudflare)
138    pub locked: bool,
139    /// DNS record name
140    pub name: String,
141    /// Time to live for DNS record. Value of 1 is 'automatic'
142    pub ttl: u32,
143    /// Zone identifier tag
144    pub zone_id: String,
145    /// When the record was last modified
146    pub modified_on: DateTime<Utc>,
147    /// When the record was created
148    pub created_on: DateTime<Utc>,
149    /// Whether this record can be modified/deleted (true means it's managed by Cloudflare)
150    pub proxiable: bool,
151    /// Type of the DNS record that also holds the record value
152    #[serde(flatten)]
153    pub content: DnsContent,
154    /// DNS record identifier tag
155    pub id: String,
156    /// Whether the record is receiving the performance and security benefits of Cloudflare
157    pub proxied: bool,
158    /// The domain of the record
159    pub zone_name: String,
160}
161
162impl ApiResult for DnsRecord {}
163impl ApiResult for Vec<DnsRecord> {}
164impl ApiResult for DeleteDnsRecordResponse {}