aboutsummaryrefslogtreecommitdiff
path: root/src/dyndns_service/gandi.rs
blob: 8851b2c8419cf5479f0ef91a74a7a9ef53c63a75 (plain)
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
// curl --request PUT \
// --url https://api.gandi.net/api/v5/domains/example.net/records/mybox/A \
// --header "authorization: Apikey ${API_KEY}" \
// --header 'content-type: application/json' \
// --data "{ \"rrset_ttl\": 300, \"rrset_values\": [\"1.2.3.4\"] }"

use crate::config::DnsRecordType;
use crate::dyndns_service::DynDnsServiceConfiguration;
use http::Method;
use serde::{Deserialize, Serialize};
use serde_with::DisplayFromStr;
use serde_with::serde_as;

// See https://api.gandi.net/docs/livedns/ for more info
#[serde_as]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Gandi {
    api_key: String,
    #[serde(default = "Gandi::default_method")]
    #[serde_as(as = "DisplayFromStr")]
    method: Method,
    #[serde(default = "Gandi::default_record_type")]
    record_type: DnsRecordType,
}

impl Gandi {
    pub fn new(api_key: String) -> Self {
        Self {
            api_key,
            method: Self::default_method(),
            record_type: Self::default_record_type(),
        }
    }

    fn default_method() -> Method {
        Method::PUT
    }
    fn default_record_type() -> DnsRecordType {
        DnsRecordType::A
    }
}

impl DynDnsServiceConfiguration for Gandi {
    fn get_service_url(&self) -> String {
        format!(
            "https://api.gandi.net/api/v5/domains/{domain}/records/{subdomain}/{record_type}",
            domain = 'a',
            subdomain = 'b',
            record_type = self.record_type
        )
    }
}

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

    #[test]
    fn check_defaults() {
        let input = json!({
            "api_key": "SOME-API-KEY",
        });

        let gandi = serde_json::from_value::<Gandi>(input).unwrap();

        assert_eq!(gandi.record_type, DnsRecordType::A);
        assert_eq!(gandi.method, Method::PUT);
    }
}