groups: user domain_client marco instead of doing it manually

This commit is contained in:
Vincent Stuyck 2026-02-28 16:34:00 +01:00
parent f08b37cb7d
commit e9e67358c4

View File

@ -5,24 +5,10 @@ use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
use crate::api::{
DomainObject, DomainType
DomainCollection, DomainObject, DomainType, domain_client
};
use crate::{ApiClient, Client, Result};
impl Client {
pub fn host_group_api(&self) -> ApiClient<HostGroup> {
self.into()
}
pub fn service_group_api(&self) -> ApiClient<ServiceGroup> {
self.into()
}
pub fn contact_group_api(&self) -> ApiClient<ContactGroup> {
self.into()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct Group {
@ -44,10 +30,11 @@ impl From<DomainObject<serde_json::Value>> for Group {
macro_rules! domain_group {
($id:ident, $typ:expr) => {
($id:ident, $typ:expr, $func:ident) => {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct $id(Group);
domain_client!($id, $func);
impl Deref for $id {
type Target = Group;
@ -69,12 +56,6 @@ macro_rules! domain_group {
}
}
impl From<&Client> for ApiClient<$id> {
fn from(value: &Client) -> Self {
Self::new(value)
}
}
impl ApiClient<$id> {
pub async fn create(&self, group: &$id) -> Result<()> {
self.inner.create($typ, group, &()).await
@ -100,12 +81,13 @@ macro_rules! domain_group {
self.inner.bulk_create($typ, entries, &()).await
}
pub async fn bulk_read(&self) -> Result<Vec<$id>> {
let entries: Vec<DomainObject<serde_json::Value>> =
let collection: DomainCollection<serde_json::Value> =
self.inner.bulk_read($typ, &()).await?;
let objects = entries.into_iter()
let groups = collection.value
.into_iter()
.map($id::from)
.collect();
Ok(objects)
Ok(groups)
}
pub async fn bulk_update(&self, entries: &[Group]) -> Result<()> {
#[derive(Serialize)]
@ -140,6 +122,6 @@ macro_rules! domain_group {
}
}
domain_group!(HostGroup, DomainType::HostGroupConfig);
domain_group!(ServiceGroup, DomainType::ServiceGroupConfig);
domain_group!(ContactGroup, DomainType::ContactGroupConfig);
domain_group!(HostGroup, DomainType::HostGroupConfig, host_group_api);
domain_group!(ServiceGroup, DomainType::ServiceGroupConfig, service_group_api);
domain_group!(ContactGroup, DomainType::ContactGroupConfig, contact_group_api);