cloudflare/endpoints/
dns.rs

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