digitalocean_api/api/
domain.rs

1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Create, Delete, Get, List};
4use crate::request::DomainRequest;
5use crate::request::Request;
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use getset::{Getters, Setters};
8use serde::Deserialize;
9use serde::Serialize;
10use std::fmt::Display;
11use std::net::IpAddr;
12use url::Url;
13
14const DOMAINS_SEGMENT: &str = "domains";
15
16/// Domain resources are domain names that you have purchased from a domain
17/// name registrar that you are managing through the DigitalOcean DNS interface.
18///
19/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domains)
20#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
21#[get = "pub"]
22pub struct Domain {
23    /// The name of the domain itself. This should follow the standard domain
24    /// format of domain.TLD. For instance, example.com is a valid domain name.
25    name: String,
26
27    /// This value is the time to live for the records on this domain, in
28    /// seconds. This defines the time frame that clients can cache queried
29    /// information before a refresh should be requested.
30    ttl: Option<usize>,
31
32    /// This attribute contains the complete contents of the zone file for the
33    /// selected domain. Individual domain record resources should be used to
34    /// get more granular control over records. However, this attribute can
35    /// also be used to get information about the SOA record, which is created
36    /// automatically and is not accessible as an individual record resource.
37    zone_file: Option<String>,
38}
39
40impl Domain {
41    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-new-domain)
42    pub fn create<N, I>(name: N, ip_address: I) -> DomainRequest<Create, Domain>
43    where
44        N: AsRef<str> + Serialize + Display,
45        I: Into<IpAddr> + Serialize + Display,
46    {
47        let mut url = ROOT_URL.clone();
48        url.path_segments_mut()
49            .expect(STATIC_URL_ERROR)
50            .push(DOMAINS_SEGMENT);
51
52        let mut req = Request::new(url);
53        req.set_body(json!({
54            "name": name,
55            "ip_address": ip_address,
56        }));
57        req
58    }
59
60    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-domains)
61    pub fn list() -> DomainRequest<List, Vec<Domain>> {
62        let mut url = ROOT_URL.clone();
63        url.path_segments_mut()
64            .expect(STATIC_URL_ERROR)
65            .push(DOMAINS_SEGMENT);
66
67        Request::new(url)
68    }
69
70    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-domain)
71    pub fn get<N: AsRef<str> + Display>(name: N) -> DomainRequest<Get, Domain> {
72        let mut url = ROOT_URL.clone();
73        url.path_segments_mut()
74            .expect(STATIC_URL_ERROR)
75            .push(DOMAINS_SEGMENT)
76            .push(name.as_ref());
77
78        Request::new(url)
79    }
80
81    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#delete-a-domain)
82    pub fn delete<N: AsRef<str> + Display>(name: N) -> DomainRequest<Delete, ()> {
83        let mut url = ROOT_URL.clone();
84        url.path_segments_mut()
85            .expect(STATIC_URL_ERROR)
86            .push(DOMAINS_SEGMENT)
87            .push(name.as_ref());
88
89        Request::new(url)
90    }
91}
92
93/// Response type returned from Digital Ocean.
94#[derive(Deserialize, Serialize, Debug, Clone)]
95pub struct DomainResponse {
96    domain: Domain,
97}
98
99impl HasResponse for Domain {
100    type Response = DomainResponse;
101}
102
103impl HasValue for DomainResponse {
104    type Value = Domain;
105
106    fn value(self) -> Domain {
107        self.domain
108    }
109}
110
111/// Response type returned from Digital Ocean.
112#[derive(Deserialize, Serialize, Debug, Clone)]
113pub struct DomainListResponse {
114    domains: Vec<Domain>,
115    links: ApiLinks,
116    meta: ApiMeta,
117}
118
119impl HasResponse for Vec<Domain> {
120    type Response = DomainListResponse;
121}
122
123impl HasPagination for DomainListResponse {
124    fn next_page(&self) -> Option<Url> {
125        self.links.next()
126    }
127}
128
129impl HasValue for DomainListResponse {
130    type Value = Vec<Domain>;
131
132    fn value(self) -> Vec<Domain> {
133        self.domains
134    }
135}