rename connector types
This commit is contained in:
parent
bc77bb16d4
commit
48c62d1020
5 changed files with 36 additions and 37 deletions
|
@ -5,7 +5,7 @@ use crate::dns;
|
||||||
// Zone content api
|
// Zone content api
|
||||||
// E.g.: DNS update + axfr, zone file read + write
|
// E.g.: DNS update + axfr, zone file read + write
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait RecordApi {
|
pub trait RecordConnector {
|
||||||
type Error;
|
type Error;
|
||||||
|
|
||||||
async fn get_records(&mut self, zone: dns::Name, class: dns::DNSClass) -> Result<Vec<dns::Record>, Self::Error>;
|
async fn get_records(&mut self, zone: dns::Name, class: dns::DNSClass) -> Result<Vec<dns::Record>, Self::Error>;
|
||||||
|
@ -18,7 +18,7 @@ pub trait RecordApi {
|
||||||
// Zone management api, todo
|
// Zone management api, todo
|
||||||
// E.g.: Manage catalog zone, dynamically generate knot / bind / nsd config...
|
// E.g.: Manage catalog zone, dynamically generate knot / bind / nsd config...
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait ZoneApi {
|
pub trait ZoneConnector {
|
||||||
type Error;
|
type Error;
|
||||||
// get_zones
|
// get_zones
|
||||||
// add_zone
|
// add_zone
|
|
@ -6,14 +6,14 @@ use trust_dns_client::error::ClientError;
|
||||||
|
|
||||||
use super::{Name, Record, RData};
|
use super::{Name, Record, RData};
|
||||||
use super::client::{ClientResponse, DnsClient};
|
use super::client::{ClientResponse, DnsClient};
|
||||||
use super::api::{RecordApi, ZoneApi};
|
use super::connector::{RecordConnector, ZoneConnector};
|
||||||
|
|
||||||
|
|
||||||
const MAX_PAYLOAD_LEN: u16 = 1232;
|
const MAX_PAYLOAD_LEN: u16 = 1232;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum DnsApiError {
|
pub enum DnsConnectorError {
|
||||||
ClientError(ClientError),
|
ClientError(ClientError),
|
||||||
ResponceNotOk {
|
ResponceNotOk {
|
||||||
code: ResponseCode,
|
code: ResponseCode,
|
||||||
|
@ -21,13 +21,13 @@ pub enum DnsApiError {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DnsApiClient {
|
pub struct DnsConnectorClient {
|
||||||
client: DnsClient
|
client: DnsClient
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DnsApiClient {
|
impl DnsConnectorClient {
|
||||||
pub fn new(client: DnsClient) -> Self {
|
pub fn new(client: DnsClient) -> Self {
|
||||||
DnsApiClient {
|
DnsConnectorClient {
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,18 +35,18 @@ impl DnsApiClient {
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl RecordApi for DnsApiClient {
|
impl RecordConnector for DnsConnectorClient {
|
||||||
type Error = DnsApiError;
|
type Error = DnsConnectorError;
|
||||||
|
|
||||||
async fn get_records(&mut self, zone: Name, class: DNSClass) -> Result<Vec<Record>, Self::Error>
|
async fn get_records(&mut self, zone: Name, class: DNSClass) -> Result<Vec<Record>, Self::Error>
|
||||||
{
|
{
|
||||||
let response = {
|
let response = {
|
||||||
let query = self.client.query(zone.clone(), class, RecordType::AXFR);
|
let query = self.client.query(zone.clone(), class, RecordType::AXFR);
|
||||||
query.await.map_err(|e| DnsApiError::ClientError(e))?
|
query.await.map_err(|e| DnsConnectorError::ClientError(e))?
|
||||||
};
|
};
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsConnectorError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
|
@ -89,10 +89,10 @@ impl RecordApi for DnsApiClient {
|
||||||
edns.set_version(0);
|
edns.set_version(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsApiError::ClientError(e))?;
|
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsConnectorError::ClientError(e))?;
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsConnectorError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
|
@ -150,10 +150,10 @@ impl RecordApi for DnsApiClient {
|
||||||
edns.set_version(0);
|
edns.set_version(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsApiError::ClientError(e))?;
|
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsConnectorError::ClientError(e))?;
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsConnectorError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
|
@ -182,7 +182,6 @@ impl RecordApi for DnsApiClient {
|
||||||
message.add_zone(zone_query);
|
message.add_zone(zone_query);
|
||||||
|
|
||||||
let mut delete = records;
|
let mut delete = records;
|
||||||
|
|
||||||
for record in delete.iter_mut() {
|
for record in delete.iter_mut() {
|
||||||
// the class must be none for delete
|
// the class must be none for delete
|
||||||
record.set_dns_class(DNSClass::NONE);
|
record.set_dns_class(DNSClass::NONE);
|
||||||
|
@ -198,10 +197,10 @@ impl RecordApi for DnsApiClient {
|
||||||
edns.set_version(0);
|
edns.set_version(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsApiError::ClientError(e))?;
|
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsConnectorError::ClientError(e))?;
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsConnectorError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
|
@ -214,18 +213,18 @@ impl RecordApi for DnsApiClient {
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl ZoneApi for DnsApiClient {
|
impl ZoneConnector for DnsConnectorClient {
|
||||||
type Error = DnsApiError;
|
type Error = DnsConnectorError;
|
||||||
|
|
||||||
async fn zone_exists(&mut self, zone: Name, class: DNSClass) -> Result<(), Self::Error>
|
async fn zone_exists(&mut self, zone: Name, class: DNSClass) -> Result<(), Self::Error>
|
||||||
{
|
{
|
||||||
let response = {
|
let response = {
|
||||||
let query = self.client.query(zone.clone(), class, RecordType::SOA);
|
let query = self.client.query(zone.clone(), class, RecordType::SOA);
|
||||||
query.await.map_err(|e| DnsApiError::ClientError(e))?
|
query.await.map_err(|e| DnsConnectorError::ClientError(e))?
|
||||||
};
|
};
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsConnectorError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
|
@ -1,6 +1,6 @@
|
||||||
pub mod client;
|
pub mod client;
|
||||||
pub mod dns_api;
|
pub mod dns_connector;
|
||||||
pub mod api;
|
pub mod connector;
|
||||||
|
|
||||||
// Reexport trust dns types for convenience
|
// Reexport trust dns types for convenience
|
||||||
pub use trust_dns_client::rr::rdata::{
|
pub use trust_dns_client::rr::rdata::{
|
||||||
|
@ -12,6 +12,6 @@ pub use trust_dns_client::rr::{
|
||||||
pub use trust_dns_proto::rr::Name;
|
pub use trust_dns_proto::rr::Name;
|
||||||
|
|
||||||
// Reexport module types
|
// Reexport module types
|
||||||
pub use api::{RecordApi, ZoneApi};
|
pub use connector::{RecordConnector, ZoneConnector};
|
||||||
pub use dns_api::DnsApiClient;
|
pub use dns_connector::{DnsConnectorClient, DnsConnectorError};
|
||||||
pub use client::DnsClient;
|
pub use client::DnsClient;
|
|
@ -6,7 +6,7 @@ use rocket_contrib::json::Json;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use djangohashers::{HasherError};
|
use djangohashers::{HasherError};
|
||||||
use diesel::result::Error as DieselError;
|
use diesel::result::Error as DieselError;
|
||||||
use crate::dns::dns_api::DnsApiError;
|
use crate::dns::DnsConnectorError;
|
||||||
use crate::models;
|
use crate::models;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -99,10 +99,10 @@ impl From<UserError> for ErrorResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DnsApiError> for ErrorResponse {
|
impl From<DnsConnectorError> for ErrorResponse {
|
||||||
fn from(e: DnsApiError) -> Self {
|
fn from(e: DnsConnectorError) -> Self {
|
||||||
match e {
|
match e {
|
||||||
DnsApiError::ResponceNotOk { code, zone } => {
|
DnsConnectorError::ResponceNotOk { code, zone } => {
|
||||||
println!("Query for zone {} failed with code {}", zone, code);
|
println!("Query for zone {} failed with code {}", zone, code);
|
||||||
|
|
||||||
ErrorResponse::new(
|
ErrorResponse::new(
|
||||||
|
@ -112,7 +112,7 @@ impl From<DnsApiError> for ErrorResponse {
|
||||||
"zone_name": zone.to_utf8()
|
"zone_name": zone.to_utf8()
|
||||||
}))
|
}))
|
||||||
},
|
},
|
||||||
DnsApiError::ClientError(e) => make_500(e)
|
DnsConnectorError::ClientError(e) => make_500(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use rocket::http::Status;
|
||||||
use rocket_contrib::json::Json;
|
use rocket_contrib::json::Json;
|
||||||
|
|
||||||
use crate::DbConn;
|
use crate::DbConn;
|
||||||
use crate::dns::{DnsClient, DnsApiClient, RecordApi, ZoneApi};
|
use crate::dns::{DnsClient, DnsConnectorClient, RecordConnector, ZoneConnector};
|
||||||
use crate::models;
|
use crate::models;
|
||||||
use crate::models::{ParseRecordList};
|
use crate::models::{ParseRecordList};
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ pub async fn get_zone_records(
|
||||||
}
|
}
|
||||||
}).await?;
|
}).await?;
|
||||||
|
|
||||||
let mut dns_api = DnsApiClient::new(client);
|
let mut dns_api = DnsConnectorClient::new(client);
|
||||||
|
|
||||||
let dns_records = dns_api.get_records(zone.clone(), models::DNSClass::IN.into()).await?;
|
let dns_records = dns_api.get_records(zone.clone(), models::DNSClass::IN.into()).await?;
|
||||||
let records: Vec<_> = dns_records.into_iter().map(models::Record::from).collect();
|
let records: Vec<_> = dns_records.into_iter().map(models::Record::from).collect();
|
||||||
|
@ -56,7 +56,7 @@ pub async fn create_zone_records(
|
||||||
}
|
}
|
||||||
}).await?;
|
}).await?;
|
||||||
|
|
||||||
let mut dns_api = DnsApiClient::new(client);
|
let mut dns_api = DnsConnectorClient::new(client);
|
||||||
|
|
||||||
dns_api.add_records(
|
dns_api.add_records(
|
||||||
zone.clone(),
|
zone.clone(),
|
||||||
|
@ -89,7 +89,7 @@ pub async fn update_zone_records(
|
||||||
}
|
}
|
||||||
}).await?;
|
}).await?;
|
||||||
|
|
||||||
let mut dns_api = DnsApiClient::new(client);
|
let mut dns_api = DnsConnectorClient::new(client);
|
||||||
|
|
||||||
dns_api.update_records(
|
dns_api.update_records(
|
||||||
zone.clone(),
|
zone.clone(),
|
||||||
|
@ -121,7 +121,7 @@ pub async fn delete_zone_records(
|
||||||
}
|
}
|
||||||
}).await?;
|
}).await?;
|
||||||
|
|
||||||
let mut dns_api = DnsApiClient::new(client);
|
let mut dns_api = DnsConnectorClient::new(client);
|
||||||
|
|
||||||
dns_api.delete_records(
|
dns_api.delete_records(
|
||||||
zone.clone(),
|
zone.clone(),
|
||||||
|
@ -159,7 +159,7 @@ pub async fn create_zone(
|
||||||
) -> Result<Json<models::Zone>, models::ErrorResponse> {
|
) -> Result<Json<models::Zone>, models::ErrorResponse> {
|
||||||
user_info?.check_admin()?;
|
user_info?.check_admin()?;
|
||||||
|
|
||||||
let mut dns_api = DnsApiClient::new(client);
|
let mut dns_api = DnsConnectorClient::new(client);
|
||||||
dns_api.zone_exists(zone_request.name.clone(), models::DNSClass::IN.into()).await?;
|
dns_api.zone_exists(zone_request.name.clone(), models::DNSClass::IN.into()).await?;
|
||||||
|
|
||||||
let zone = conn.run(move |c| {
|
let zone = conn.run(move |c| {
|
||||||
|
|
Loading…
Reference in a new issue