1- use std:: {
2- net:: { Ipv4Addr , Ipv6Addr } ,
3- str:: FromStr ,
4- } ;
5-
61use anyhow:: Result ;
72use async_trait:: async_trait;
8- use lum_libs:: {
9- serde:: { Deserialize , Serialize } ,
10- serde_json,
11- } ;
3+ use lum_libs:: serde_json;
124use reqwest:: header:: HeaderMap ;
135use thiserror:: Error ;
146
157use crate :: {
16- config:: dns:: { AutomaticRecordConfig , RecordConfig , ResolveType } ,
17- provider:: { self , Feature , Provider } ,
18- types:: dns:: { self , MxRecord , Record , RecordValue } ,
8+ provider:: { Feature , GetAllRecordsInput , GetRecordsInput , Provider } ,
9+ types:: dns:: { self } ,
1910} ;
2011
21- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
22- #[ serde( crate = "lum_libs::serde" ) ]
23- pub struct Config {
24- pub name : String ,
25- pub api_key : String ,
26- pub api_base_url : String ,
27- }
12+ pub mod config;
13+ pub mod model;
2814
29- impl Default for Config {
30- fn default ( ) -> Self {
31- Config {
32- name : "Nitrado1" . to_string ( ) ,
33- api_key : "your_api_key" . to_string ( ) ,
34- api_base_url : "https://api.nitrado.net" . to_string ( ) ,
35- }
36- }
37- }
38-
39- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
40- #[ serde( crate = "lum_libs::serde" ) ]
41- pub struct DomainConfig {
42- pub domain : String ,
43- pub records : Vec < RecordConfig > ,
44- }
45-
46- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
47- #[ serde( crate = "lum_libs::serde" ) ]
48- pub struct DnsConfig {
49- pub provider_name : String ,
50- pub domains : Vec < DomainConfig > ,
51- }
52-
53- impl Default for DnsConfig {
54- fn default ( ) -> Self {
55- DnsConfig {
56- provider_name : "Nitrado1" . to_string ( ) ,
57- domains : vec ! [ DomainConfig {
58- domain: "example.com" . to_string( ) ,
59- records: vec![
60- RecordConfig :: Manual ( dns:: Record {
61- domain: "ipv4" . to_string( ) ,
62- value: RecordValue :: A ( Ipv4Addr :: from_str( "127.0.0.1" ) . unwrap( ) ) ,
63- ttl: Some ( 3600 ) ,
64- } ) ,
65- RecordConfig :: Manual ( dns:: Record {
66- domain: "ipv6" . to_string( ) ,
67- value: RecordValue :: AAAA ( Ipv6Addr :: from_str( "::1" ) . unwrap( ) ) ,
68- ttl: Some ( 3600 ) ,
69- } ) ,
70- RecordConfig :: Manual ( dns:: Record {
71- domain: "forward" . to_string( ) ,
72- value: RecordValue :: CNAME ( "example.com" . to_string( ) ) ,
73- ttl: Some ( 3600 ) ,
74- } ) ,
75- RecordConfig :: Manual ( dns:: Record {
76- domain: "@" . to_string( ) ,
77- value: RecordValue :: MX ( MxRecord {
78- priority: 10 ,
79- target: "mail.example.com" . to_string( ) ,
80- } ) ,
81- ttl: Some ( 3600 ) ,
82- } ) ,
83- RecordConfig :: Manual ( dns:: Record {
84- domain: "@" . to_string( ) ,
85- value: RecordValue :: TXT ( "v=spf1 include:example.com ~all" . to_string( ) ) ,
86- ttl: Some ( 3600 ) ,
87- } ) ,
88- RecordConfig :: Manual ( dns:: Record {
89- domain: "@" . to_string( ) ,
90- value: RecordValue :: Custom ( "RecordType" . to_string( ) , "Value" . to_string( ) ) ,
91- ttl: Some ( 3600 ) ,
92- } ) ,
93- RecordConfig :: Automatic ( AutomaticRecordConfig {
94- domain: "auto-ipv4" . to_string( ) ,
95- ttl: Some ( 300 ) ,
96- resolve_type: ResolveType :: IPv4 ,
97- } ) ,
98- RecordConfig :: Automatic ( AutomaticRecordConfig {
99- domain: "auto-ipv6" . to_string( ) ,
100- ttl: Some ( 300 ) ,
101- resolve_type: ResolveType :: IPv6 ,
102- } ) ,
103- ] ,
104- } ] ,
105- }
106- }
107- }
15+ pub use config:: { Config , DnsConfig , DomainConfig } ;
16+ pub use model:: { GetRecordsResponse , Record , RecordMode , TryFromRecordError } ;
10817
10918pub struct NitradoProvider < ' provider_config > {
11019 pub provider_config : & ' provider_config Config ,
@@ -136,8 +45,8 @@ impl Provider for NitradoProvider<'_> {
13645
13746 fn get_supported_features ( & self ) -> Vec < Feature > {
13847 vec ! [
139- Feature :: GetRecord ,
14048 Feature :: GetRecords ,
49+ Feature :: GetAllRecords ,
14150 Feature :: AddRecord ,
14251 Feature :: UpdateRecord ,
14352 Feature :: DeleteRecord ,
@@ -146,8 +55,25 @@ impl Provider for NitradoProvider<'_> {
14655 async fn get_records (
14756 & self ,
14857 reqwest : reqwest:: Client ,
149- input : & provider:: GetRecordsInput ,
150- ) -> Result < Vec < Record > > {
58+ input : & GetRecordsInput ,
59+ ) -> Result < Vec < dns:: Record > > {
60+ let get_all_records_input = GetAllRecordsInput :: from ( input) ;
61+ let records = self
62+ . get_all_records ( reqwest, & get_all_records_input)
63+ . await ?;
64+ let records = records
65+ . into_iter ( )
66+ . filter ( |record| input. subdomains . contains ( & record. domain . as_str ( ) ) )
67+ . collect ( ) ;
68+
69+ Ok ( records)
70+ }
71+
72+ async fn get_all_records (
73+ & self ,
74+ reqwest : reqwest:: Client ,
75+ input : & GetAllRecordsInput ,
76+ ) -> Result < Vec < dns:: Record > > {
15177 let mut headers = HeaderMap :: new ( ) ;
15278 headers. insert (
15379 "Authorization" ,
@@ -168,23 +94,21 @@ impl Provider for NitradoProvider<'_> {
16894 }
16995
17096 let text = response. text ( ) . await ?;
171- let json = serde_json:: from_str ( & text) ?;
172- Ok ( json)
173- }
97+ let response: GetRecordsResponse = serde_json:: from_str ( & text) ?;
98+ let records: Vec < dns:: Record > = response. try_into ( ) ?;
17499
175- async fn get_all_records ( & self , _reqwest : reqwest:: Client ) -> Result < Vec < Record > > {
176- unimplemented ! ( )
100+ Ok ( records)
177101 }
178102
179- async fn add_record ( & self , _reqwest : reqwest:: Client , _input : & Record ) -> Result < ( ) > {
103+ async fn add_record ( & self , _reqwest : reqwest:: Client , _input : & dns :: Record ) -> Result < ( ) > {
180104 unimplemented ! ( )
181105 }
182106
183- async fn update_record ( & self , _reqwest : reqwest:: Client , _input : & Record ) -> Result < ( ) > {
107+ async fn update_record ( & self , _reqwest : reqwest:: Client , _input : & dns :: Record ) -> Result < ( ) > {
184108 unimplemented ! ( )
185109 }
186110
187- async fn delete_record ( & self , _reqwest : reqwest:: Client , _input : & Record ) -> Result < ( ) > {
111+ async fn delete_record ( & self , _reqwest : reqwest:: Client , _input : & dns :: Record ) -> Result < ( ) > {
188112 unimplemented ! ( )
189113 }
190114}
0 commit comments