groups: user domain_client marco instead of doing it manually
This commit is contained in:
parent
f08b37cb7d
commit
e9e67358c4
@ -5,24 +5,10 @@ use serde::{Deserialize, Serialize};
|
|||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
|
|
||||||
use crate::api::{
|
use crate::api::{
|
||||||
DomainObject, DomainType
|
DomainCollection, DomainObject, DomainType, domain_client
|
||||||
};
|
};
|
||||||
use crate::{ApiClient, Client, Result};
|
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)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
|
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
|
||||||
pub struct Group {
|
pub struct Group {
|
||||||
@ -44,10 +30,11 @@ impl From<DomainObject<serde_json::Value>> for Group {
|
|||||||
|
|
||||||
|
|
||||||
macro_rules! domain_group {
|
macro_rules! domain_group {
|
||||||
($id:ident, $typ:expr) => {
|
($id:ident, $typ:expr, $func:ident) => {
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
|
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
|
||||||
pub struct $id(Group);
|
pub struct $id(Group);
|
||||||
|
domain_client!($id, $func);
|
||||||
|
|
||||||
impl Deref for $id {
|
impl Deref for $id {
|
||||||
type Target = Group;
|
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> {
|
impl ApiClient<$id> {
|
||||||
pub async fn create(&self, group: &$id) -> Result<()> {
|
pub async fn create(&self, group: &$id) -> Result<()> {
|
||||||
self.inner.create($typ, group, &()).await
|
self.inner.create($typ, group, &()).await
|
||||||
@ -100,12 +81,13 @@ macro_rules! domain_group {
|
|||||||
self.inner.bulk_create($typ, entries, &()).await
|
self.inner.bulk_create($typ, entries, &()).await
|
||||||
}
|
}
|
||||||
pub async fn bulk_read(&self) -> Result<Vec<$id>> {
|
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?;
|
self.inner.bulk_read($typ, &()).await?;
|
||||||
let objects = entries.into_iter()
|
let groups = collection.value
|
||||||
|
.into_iter()
|
||||||
.map($id::from)
|
.map($id::from)
|
||||||
.collect();
|
.collect();
|
||||||
Ok(objects)
|
Ok(groups)
|
||||||
}
|
}
|
||||||
pub async fn bulk_update(&self, entries: &[Group]) -> Result<()> {
|
pub async fn bulk_update(&self, entries: &[Group]) -> Result<()> {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@ -140,6 +122,6 @@ macro_rules! domain_group {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
domain_group!(HostGroup, DomainType::HostGroupConfig);
|
domain_group!(HostGroup, DomainType::HostGroupConfig, host_group_api);
|
||||||
domain_group!(ServiceGroup, DomainType::ServiceGroupConfig);
|
domain_group!(ServiceGroup, DomainType::ServiceGroupConfig, service_group_api);
|
||||||
domain_group!(ContactGroup, DomainType::ContactGroupConfig);
|
domain_group!(ContactGroup, DomainType::ContactGroupConfig, contact_group_api);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user