checkmk-api/src/api/groups.rs
2026-02-27 23:38:05 +01:00

146 lines
4.4 KiB
Rust

use core::ops::Deref;
use serde::{Deserialize, Serialize};
#[cfg(feature = "schemars")]
use schemars::JsonSchema;
use crate::api::{
DomainObject, DomainType
};
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 {
pub name: String,
pub alias: String
}
impl Group {
pub fn new(name: String, alias: String) -> Self {
Self { name, alias }
}
}
impl From<DomainObject<serde_json::Value>> for Group {
fn from(value: DomainObject<serde_json::Value>) -> Self {
Self::new(value.id, value.title.unwrap_or_default())
}
}
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 {
pub fn new(name: String, alias: String) -> Self {
Self(Group::new(name, alias))
}
}
impl From<DomainObject<serde_json::Value>> for $id {
fn from(value: DomainObject<serde_json::Value>) -> Self {
Self(Group::from(value))
}
}
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
}
pub async fn read(&self, id: &str) -> Result<$id> {
let obj: DomainObject<serde_json::Value> =
self.inner.read($typ, id, &()).await?;
Ok($id::from(obj))
}
pub async fn update(&self, id: &str, alias: &str) -> Result<()> {
#[derive(Serialize)]
struct Body<'a> {
alias: &'a str
}
self.inner.update($typ, id, &Body { alias }).await
}
pub async fn delete(&self, id: &str) -> Result<()> {
self.inner.delete($typ, id, &()).await
}
pub async fn bulk_create(&self, entries: &[$id]) -> Result<()> {
self.inner.bulk_create($typ, entries, &()).await
}
pub async fn bulk_read(&self) -> Result<Vec<$id>> {
let entries: Vec<DomainObject<serde_json::Value>> =
self.inner.bulk_read($typ, &()).await?;
let objects = entries.into_iter()
.map($id::from)
.collect();
Ok(objects)
}
pub async fn bulk_update(&self, entries: &[Group]) -> Result<()> {
#[derive(Serialize)]
struct UpdateAttributes<'a> {
alias: &'a str
}
#[derive(Serialize)]
struct UpdateRequest<'a> {
name: &'a str,
attributes: UpdateAttributes<'a>
}
impl <'a> UpdateRequest<'a> {
fn from(value: &'a Group) -> Self {
Self {
name: value.name.as_str(),
attributes: UpdateAttributes {
alias: value.alias.as_str()
}
}
}
}
let entries = entries.iter()
.map(UpdateRequest::from)
.collect::<Vec<_>>();
self.inner.bulk_update($typ, &entries).await
}
pub async fn bulk_delete(&self, entries: &[String]) -> Result<()> {
self.inner.bulk_delete($typ, entries).await
}
}
}
}
domain_group!(HostGroup, DomainType::HostGroupConfig);
domain_group!(ServiceGroup, DomainType::ServiceGroupConfig);
domain_group!(ContactGroup, DomainType::ContactGroupConfig);