diff --git a/src/api/groups.rs b/src/api/groups.rs new file mode 100644 index 0000000..8762b64 --- /dev/null +++ b/src/api/groups.rs @@ -0,0 +1,97 @@ +use core::ops::Deref; + +use serde::{Deserialize, Serialize}; +#[cfg(feature = "schemars")] +use schemars::JsonSchema; + +use crate::api::{ + BulkCreateDomainExtension, BulkDeleteDomainExtension, BulkReadDomainExtension, + BulkUpdateDomainExtension, DomainExtension, DomainType +}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(JsonSchema))] +pub struct Group { + pub name: String, + pub alias: String +} + +impl Group { + fn new(name: String, alias: String) -> Self { + Self { name, alias } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(JsonSchema))] +pub struct GroupAlias { + alias: String +} + +impl GroupAlias { + fn new(alias: String) -> Self { + Self { alias } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(JsonSchema))] +pub struct BulkGroupUpdate { + name: String, + attributes: GroupAlias +} + +impl BulkGroupUpdate { + fn new(name: String, alias: String) -> Self { + Self { + name, + attributes: GroupAlias::new(alias) + } + } +} + +macro_rules! domain_group { + ($id:ident, $typ:expr) => { + #[derive(Debug, Clone, Serialize, Deserialize)] + #[cfg_attr(feature = "schemars", derive(JsonSchema))] + pub struct $id(Group); + + impl Deref for $id { + type Target = Group; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl $id { + fn new(name: String, alias: String) -> Self { + Self(Group::new(name, alias)) + } + } + + impl DomainExtension for $id { + const DOMAIN_TYPE: DomainType = $typ; + + type CreationRequest = Group; + type CreationQuery = (); + type ReadQuery = (); + type UpdateRequest = GroupAlias; + type DeleteQuery = (); + } + + impl BulkCreateDomainExtension for $id {} + impl BulkReadDomainExtension for $id { + type BulkReadQuery = (); + } + impl BulkUpdateDomainExtension for $id { + type BulkUpdateRequest = BulkGroupUpdate; + } + impl BulkDeleteDomainExtension for $id {} + + } +} + +domain_group!(HostGroup, DomainType::HostGroupConfig); +domain_group!(ServiceGroup, DomainType::ServiceGroupConfig); +domain_group!(ContactGroup, DomainType::ContactGroupConfig); diff --git a/src/api/mod.rs b/src/api/mod.rs index d6ef87f..482cbb1 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,5 +1,6 @@ pub mod folders; pub mod hosts; +pub mod groups; pub(crate) mod rules; use std::fmt;