Compare commits

..

6 Commits

Author SHA1 Message Date
Vincent Stuyck
f08b37cb7d go voer some ai feedback 2026-02-28 01:21:34 +01:00
Vincent Stuyck
7014b83ba2 update main for new api 2026-02-28 01:10:29 +01:00
Vincent Stuyck
b1d696de84 update how the client is created with marcos instead of relying on
traits. this makes a cleaner api
2026-02-28 01:07:48 +01:00
Vincent Stuyck
ed787b2099 add group api 2026-02-27 23:38:05 +01:00
Vincent Stuyck
a72fc0a09f add hosttags 2026-02-27 23:09:39 +01:00
Vincent Stuyck
c39315b2b7 rework client a little to make more low level methods available 2026-02-27 23:09:30 +01:00
9 changed files with 724 additions and 246 deletions

View File

@ -10,16 +10,12 @@ use schemars::JsonSchema;
use crate::api::rules::contactgroups::HostContactGroups;
use crate::api::rules::snmp::SnmpCommunity;
use crate::api::{BulkReadDomainExtension, BulkUpdateDomainExtension, DomainExtension};
use crate::api::{DomainExtension, domain_bulk_read, domain_bulk_update, domain_client, domain_create, domain_delete, domain_read, domain_update};
use crate::{ApiClient, Client, Result};
pub const ROOT_FOLDER: &str = "/";
impl Client {
pub fn folder_api(&self) -> ApiClient<FolderConfig> {
self.into()
}
}
domain_client!(FolderConfig, folder_api);
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
@ -136,12 +132,6 @@ pub struct MetaData {
impl DomainExtension for FolderConfig {
const DOMAIN_TYPE: super::DomainType = super::DomainType::FolderConfig;
type CreationRequest = FolderCreationRequest;
type CreationQuery = ();
type ReadQuery = ();
type UpdateRequest = FolderUpdateRequest;
type DeleteQuery = FolderDeleteQuery;
}
#[derive(Debug, Default, Clone, Serialize)]
@ -259,10 +249,6 @@ pub enum FolderDeleteMode {
AbortOnNonEmpty,
}
impl BulkReadDomainExtension for FolderConfig {
type BulkReadQuery = FolderBulkReadQuery;
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct FolderBulkReadQuery {
@ -276,10 +262,6 @@ impl FolderBulkReadQuery {
}
}
impl BulkUpdateDomainExtension for FolderConfig {
type BulkUpdateRequest = FolderBulkUpdateRequest;
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct FolderBulkUpdateRequest {
@ -297,28 +279,27 @@ impl FolderBulkUpdateRequest {
}
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct MoveFolderRequest {
destination: String,
}
impl MoveFolderRequest {
pub fn new(destination: String) -> Self {
Self { destination }
}
}
impl ApiClient<FolderConfig> {
pub async fn move_folder(&self, id: impl Display, request: &MoveFolderRequest) -> Result<()> {
pub async fn move_folder(&self, id: impl Display, destination: &str) -> Result<()> {
#[derive(Serialize)]
struct MoveFolderRequest<'a> {
destination: &'a str,
}
let request = MoveFolderRequest { destination };
let url = format!(
"{}/actions/move/invoke",
self.inner.object_url(FolderConfig::DOMAIN_TYPE, id)
);
let request = self.inner.http.post(url).json(request).build().unwrap();
let request = self.inner.http.post(url).json(&request).build().unwrap();
self.inner.invoke_api(request).await
}
}
// TODO: add show hosts api call
domain_create!(FolderConfig, FolderCreationRequest);
domain_read!(FolderConfig);
domain_update!(FolderConfig, FolderUpdateRequest);
domain_delete!(FolderConfig, FolderDeleteQuery);
domain_bulk_read!(FolderConfig, FolderBulkReadQuery);
domain_bulk_update!(FolderConfig, FolderBulkUpdateRequest);

View File

@ -5,9 +5,23 @@ use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
use crate::api::{
BulkCreateDomainExtension, BulkDeleteDomainExtension, BulkReadDomainExtension,
BulkUpdateDomainExtension, DomainExtension, DomainType
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))]
@ -22,33 +36,12 @@ impl Group {
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct GroupAlias {
alias: String
}
impl GroupAlias {
pub fn new(alias: String) -> Self {
Self { 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())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct BulkGroupUpdate {
name: String,
attributes: GroupAlias
}
impl BulkGroupUpdate {
pub fn new(name: String, alias: String) -> Self {
Self {
name,
attributes: GroupAlias::new(alias)
}
}
}
macro_rules! domain_group {
($id:ident, $typ:expr) => {
@ -70,25 +63,80 @@ macro_rules! domain_group {
}
}
impl DomainExtension for $id {
const DOMAIN_TYPE: DomainType = $typ;
type CreationRequest = Group;
type CreationQuery = ();
type ReadQuery = ();
type UpdateRequest = GroupAlias;
type DeleteQuery = ();
impl From<DomainObject<serde_json::Value>> for $id {
fn from(value: DomainObject<serde_json::Value>) -> Self {
Self(Group::from(value))
}
}
impl BulkCreateDomainExtension for $id {}
impl BulkReadDomainExtension for $id {
type BulkReadQuery = ();
impl From<&Client> for ApiClient<$id> {
fn from(value: &Client) -> Self {
Self::new(value)
}
}
impl BulkUpdateDomainExtension for $id {
type BulkUpdateRequest = BulkGroupUpdate;
}
impl BulkDeleteDomainExtension for $id {}
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
}
}
}
}

View File

@ -1,3 +1,4 @@
use std::fmt::Display;
use std::net::{Ipv4Addr, Ipv6Addr};
use serde::{Deserialize, Serialize};
@ -6,16 +7,12 @@ use schemars::JsonSchema;
use crate::api::folders::FolderAttributes;
use crate::api::{
BulkCreateDomainExtension, BulkDeleteDomainExtension, BulkReadDomainExtension,
BulkUpdateDomainExtension, DomainExtension,
DomainExtension, domain_bulk_create, domain_bulk_delete, domain_bulk_read, domain_bulk_update, domain_client, domain_create, domain_delete, domain_read, domain_update
};
use crate::{ApiClient, Client};
use crate::{ApiClient, Client, Result};
impl Client {
pub fn host_api(&self) -> ApiClient<HostConfig> {
self.into()
}
}
domain_client!(HostConfig, host_api);
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
@ -49,12 +46,6 @@ pub struct HostAttributes {
impl DomainExtension for HostConfig {
const DOMAIN_TYPE: super::DomainType = super::DomainType::HostConfig;
type CreationRequest = HostCreationRequest;
type CreationQuery = HostCreationQuery;
type ReadQuery = HostReadQuery;
type UpdateRequest = HostUpdateRequest;
type DeleteQuery = ();
}
#[derive(Serialize)]
@ -133,15 +124,6 @@ impl HostUpdateRequest {
}
}
impl BulkCreateDomainExtension for HostConfig {}
impl BulkReadDomainExtension for HostConfig {
type BulkReadQuery = HostBulkReadQuery;
}
impl BulkUpdateDomainExtension for HostConfig {
type BulkUpdateRequest = HostBulkUpdateRequest;
}
impl BulkDeleteDomainExtension for HostConfig {}
#[derive(Debug, Default, Clone, Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostBulkReadQuery {
@ -216,3 +198,12 @@ impl HostBulkUpdateRequest {
}
}
}
domain_create!(HostConfig, HostCreationRequest, HostCreationQuery);
domain_read!(HostConfig, HostReadQuery);
domain_update!(HostConfig, HostUpdateRequest);
domain_delete!(HostConfig);
domain_bulk_create!(HostConfig, HostCreationRequest, HostCreationQuery);
domain_bulk_read!(HostConfig, HostBulkReadQuery);
domain_bulk_update!(HostConfig, HostBulkUpdateRequest);
domain_bulk_delete!(HostConfig);

View File

@ -5,10 +5,13 @@ pub mod groups;
pub(crate) mod rules;
pub mod tags;
use std::fmt;
use std::fmt::{self, Display};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use crate::Result;
use crate::client::InnerClient;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DomainType {
@ -88,22 +91,248 @@ pub(crate) struct DomainCollection<E> {
pub trait DomainExtension: DeserializeOwned + Serialize {
const DOMAIN_TYPE: DomainType;
type CreationRequest: Serialize;
type CreationQuery: Serialize;
type ReadQuery: Serialize;
type UpdateRequest: Serialize;
type DeleteQuery: Serialize;
}
// these are essential tags for domain extensions to signal that bulk operations are possible
// not every extension supports bulk operations. and not all extensions that do support all of them
// and those that do tend to have a bit different types
pub trait BulkCreateDomainExtension: DomainExtension {}
pub trait BulkReadDomainExtension: DomainExtension {
type BulkReadQuery: Serialize;
impl InnerClient {
pub(crate) async fn create_domain_object<E, O, Q>(
&self,
object: &O,
query: &Q,
) -> Result<()>
where
E: DomainExtension,
O: Serialize,
Q: Serialize
{
self.create(E::DOMAIN_TYPE, object, query).await
}
pub(crate) async fn read_domain_object<E, Q>(
&self,
id: impl Display,
query: &Q,
) -> Result<DomainObject<E>>
where
E: DomainExtension,
Q: Serialize
{
self.read(E::DOMAIN_TYPE, id, query).await
}
pub(crate) async fn update_domain_object<E, O>(
&self,
id: impl Display,
object: &O,
) -> Result<()>
where
E: DomainExtension,
O: Serialize
{
self.update(E::DOMAIN_TYPE, id, object).await
}
pub(crate) async fn delete_domain_object<E, Q>(
&self,
id: impl Display,
query: &Q,
) -> Result<()>
where
E: DomainExtension,
Q: Serialize
{
self.delete(E::DOMAIN_TYPE, id, query).await
}
pub(crate) async fn bulk_create_domain_objects<E, O, Q>(
&self,
entries: &[O],
query: &Q,
) -> Result<()>
where
E: DomainExtension,
O: Serialize,
Q: Serialize
{
self.bulk_create(E::DOMAIN_TYPE, entries, query).await
}
pub(crate) async fn bulk_read_domain_objects<E, Q>(
&self,
query: &Q,
) -> Result<Vec<DomainObject<E>>>
where
E: DomainExtension,
Q: Serialize
{
self.bulk_read(E::DOMAIN_TYPE, query).await
}
pub(crate) async fn bulk_update_domain_objects<E, O>(
&self,
entries: &[O],
) -> Result<()>
where
E: DomainExtension,
O: Serialize
{
self.bulk_update(E::DOMAIN_TYPE, entries).await
}
pub(crate) async fn bulk_delete_domain_objects<E>(
&self,
entries: &[String],
) -> Result<()>
where
E: DomainExtension
{
self.bulk_delete(E::DOMAIN_TYPE, entries).await
}
}
pub trait BulkUpdateDomainExtension: DomainExtension {
type BulkUpdateRequest: Serialize;
macro_rules! domain_client {
($id:ident, $func:ident) => {
impl Client {
pub fn $func(&self) -> ApiClient<$id> {
self.into()
}
}
domain_client!($id);
};
($id:ident) => {
impl From<&Client> for ApiClient<$id> {
fn from(value: &Client) -> Self {
Self::new(value)
}
}
}
}
pub trait BulkDeleteDomainExtension: DomainExtension {}
pub(super) use domain_client;
macro_rules! domain_create {
($id:ident, $object:ident) => {
impl ApiClient<$id> {
pub async fn create(&self, object: &$object) -> Result<()> {
self.inner.create_domain_object::<$id, _, _>(object, &()).await
}
}
};
($id:ident, $object:ident, $qry:ident) => {
impl ApiClient<$id> {
pub async fn create(&self, object: &$object, query: &$qry) -> Result<()> {
self.inner.create_domain_object::<$id, _, _>(object, query).await
}
}
};
}
pub(super) use domain_create;
macro_rules! domain_read {
($id:ident) => {
impl ApiClient<$id> {
pub async fn read(&self, id: impl Display) -> Result<$id> {
self.inner.read_domain_object::<$id, _>(id, &()).await
.map(|obj| obj.extensions)
}
}
};
($id:ident, $qry:ident) => {
impl ApiClient<$id> {
pub async fn read(&self, id: impl Display, query: &$qry) -> Result<$id> {
self.inner.read_domain_object::<$id, _>(id, &query).await
.map(|obj| obj.extensions)
}
}
};
}
pub(super) use domain_read;
macro_rules! domain_update {
($id:ident, $object:ident) => {
impl ApiClient<$id> {
pub async fn update(&self, id: impl Display, object: &$object) -> Result<()> {
self.inner.update_domain_object::<$id, _>(id, object).await
}
}
};
}
pub(super) use domain_update;
macro_rules! domain_delete {
($id:ident) => {
impl ApiClient<$id> {
pub async fn delete(&self, id: impl Display) -> Result<()> {
self.inner.delete_domain_object::<$id, _>(id, &()).await
}
}
};
($id:ident, $qry:ident) => {
impl ApiClient<$id> {
pub async fn delete(&self, id: impl Display, query: &$qry) -> Result<()> {
self.inner.delete_domain_object::<$id, _>(id, &query).await
}
}
};
}
pub(super) use domain_delete;
macro_rules! domain_bulk_create {
($id:ident, $object:ident) => {
impl ApiClient<$id> {
pub async fn bulk_create(&self, entries: &[$object]) -> Result<()> {
self.inner.bulk_create_domain_objects::<$id, _, _>(entries, &()).await
}
}
};
($id:ident, $object:ident, $qry:ident) => {
impl ApiClient<$id> {
pub async fn bulk_create(&self, entries: &[$object], query: &$qry) -> Result<()> {
self.inner.bulk_create_domain_objects::<$id, _, _>(entries, query).await
}
}
};
}
pub(super) use domain_bulk_create;
macro_rules! domain_bulk_read {
($id:ident) => {
impl ApiClient<$id> {
pub async fn bulk_read(&self) -> Result<Vec<$id>> {
let objs = self.inner.bulk_read_domain_objects::<$id, _>(&()).await?;
let objs = objs.into_iter()
.map(|obj| obj.extensions)
.collect();
Ok(objs)
}
}
};
($id:ident, $qry:ident) => {
impl ApiClient<$id> {
pub async fn bulk_read(&self, query: &$qry) -> Result<Vec<$id>> {
let objs = self.inner.bulk_read_domain_objects::<$id, _>(&query).await?;
let objs = objs.into_iter()
.map(|obj| obj.extensions)
.collect();
Ok(objs)
}
}
};
}
pub(super) use domain_bulk_read;
macro_rules! domain_bulk_update {
($id:ident, $object:ident) => {
impl ApiClient<$id> {
pub async fn bulk_update(&self, entries: &[$object]) -> Result<()> {
self.inner.bulk_update_domain_objects::<$id, _>(entries).await
}
}
};
}
pub(super) use domain_bulk_update;
macro_rules! domain_bulk_delete {
($id:ident) => {
impl ApiClient<$id> {
pub async fn bulk_delete(&self, entries: &[String]) -> Result<()> {
self.inner.bulk_delete_domain_objects::<$id>(entries).await
}
}
};
}
pub(super) use domain_bulk_delete;

View File

@ -6,9 +6,9 @@ use schemars::JsonSchema;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostContactGroups {
groups: Vec<String>,
r#use: bool,
use_for_services: bool,
recurse_use: bool,
recurse_perms: bool,
pub groups: Vec<String>,
pub r#use: bool,
pub use_for_services: bool,
pub recurse_use: bool,
pub recurse_perms: bool,
}

View File

@ -1,8 +1,13 @@
use crate::api::Display;
use serde::{Deserialize, Serialize};
#[cfg(feature = "schemars")]
use schemars::JsonSchema;
use crate::api::{BulkReadDomainExtension, DomainExtension, DomainType};
use crate::api::{DomainExtension, DomainType, domain_bulk_read, domain_client, domain_create, domain_delete, domain_read, domain_update};
use crate::{ApiClient, Client, Result};
domain_client!(TagGroup, tag_api);
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
@ -144,17 +149,10 @@ pub enum TagDeleteMode {
impl DomainExtension for TagGroup {
const DOMAIN_TYPE: super::DomainType = DomainType::HostTagGroup;
type CreationRequest = ExtendedTagGroup;
type CreationQuery = ();
type ReadQuery = ();
type UpdateRequest = TagUpdateRequest;
type DeleteQuery = ();
}
// TODO: id and title are in the "DomainObject".
// should be added to during query?
// maybe add "with_title_and_id" to DomainExtension, defaulting to no-op?
impl BulkReadDomainExtension for TagGroup {
type BulkReadQuery = ();
}
domain_create!(TagGroup, ExtendedTagGroup);
domain_read!(TagGroup);
domain_update!(TagGroup, TagUpdateRequest);
domain_delete!(TagGroup);
domain_bulk_read!(TagGroup);

View File

@ -11,8 +11,7 @@ use serde::de::DeserializeOwned;
use tokio::sync::Semaphore;
use crate::api::{
BulkCreateDomainExtension, BulkDeleteDomainExtension, BulkReadDomainExtension,
BulkUpdateDomainExtension, DomainCollection, DomainExtension, DomainObject, DomainType,
DomainCollection, DomainObject, DomainType,
};
use crate::{Error, Result};
@ -206,12 +205,15 @@ impl Client {
}
impl InnerClient {
#[inline]
pub(crate) fn object_url(&self, domain_type: DomainType, id: impl Display) -> String {
format!("{}/objects/{}/{}", self.url, domain_type, id)
}
#[inline]
pub(crate) fn collection_url(&self, domain_type: DomainType) -> String {
format!("{}/domain-types/{}/collections/all", self.url, domain_type)
}
#[inline]
pub(crate) fn bulk_action_url(&self, domain_type: DomainType, action: BulkAction) -> String {
format!(
"{}/domain-types/{}/actions/bulk-{}/invoke",
@ -282,60 +284,83 @@ impl InnerClient {
}
}
pub(crate) async fn create_domain_object<E: DomainExtension>(
pub(crate) async fn create<O, Q>(
&self,
body: &E::CreationRequest,
query: &E::CreationQuery,
) -> Result<()> {
domain_type: DomainType,
object: &O,
query: &Q
) -> Result<()>
where
O: Serialize,
Q: Serialize
{
let url = self.collection_url(domain_type);
let request = self
.http
.post(self.collection_url(E::DOMAIN_TYPE))
.json(body)
.post(url)
.json(object)
.query(query)
.build()
.unwrap();
self.invoke_api(request).await
}
pub(crate) async fn read_domain_object<E: DomainExtension>(
pub(crate) async fn read<O, Q>(
&self,
domain_type: DomainType,
id: impl Display,
query: &E::ReadQuery,
) -> Result<DomainObject<E>> {
query: &Q,
) -> Result<O>
where
O: DeserializeOwned,
Q: Serialize
{
let url = self.object_url(domain_type, id);
let request = self
.http
.get(self.object_url(E::DOMAIN_TYPE, id))
.get(url)
.query(query)
.build()
.unwrap();
self.query_api(request).await
}
pub(crate) async fn update_domain_object<E: DomainExtension>(
pub(crate) async fn update<O>(
&self,
domain_type: DomainType,
id: impl Display,
request: &E::UpdateRequest,
) -> Result<()> {
let url = self.object_url(E::DOMAIN_TYPE, id);
object: &O,
) -> Result<()>
where
O: Serialize,
{
let url = self.object_url(domain_type, id);
let etag = self.get_etag(&url).await?;
let request = self
.http
.put(url)
.json(&request)
.json(object)
.header(reqwest::header::IF_MATCH, etag)
.build()
.unwrap();
self.invoke_api(request).await
}
pub(crate) async fn delete_domain_object<E: DomainExtension>(
pub(crate) async fn delete<Q>(
&self,
domain_type: DomainType,
id: impl Display,
query: &E::DeleteQuery,
) -> Result<()> {
query: &Q,
) -> Result<()>
where
Q: Serialize,
{
let url = self.object_url(domain_type, id);
let request = self
.http
.delete(self.object_url(E::DOMAIN_TYPE, id))
.delete(url)
.query(&query)
.build()
.unwrap();
@ -343,71 +368,96 @@ impl InnerClient {
self.invoke_api(request).await
}
pub(crate) async fn bulk_create_domain_objects<E: BulkCreateDomainExtension>(
pub(crate) async fn bulk_create<O, Q>(
&self,
entries: &[E::CreationRequest],
query: &E::CreationQuery,
) -> Result<()> {
domain_type: DomainType,
entries: &[O],
query: &Q,
) -> Result<()>
where
O: Serialize,
Q: Serialize,
{
#[derive(Serialize)]
struct Request<'a, D: BulkCreateDomainExtension> {
entries: &'a [D::CreationRequest],
struct Request<'a, E: Serialize> {
entries: &'a [E],
}
let url = self.bulk_action_url(domain_type, BulkAction::Create);
let body = Request::<O> { entries };
let request = self
.http
.post(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Create))
.json(&Request::<E> { entries })
.post(url)
.json(&body)
.query(query)
.build()
.unwrap();
self.invoke_api(request).await
}
pub(crate) async fn bulk_read_domain_objects<E: BulkReadDomainExtension>(
pub(crate) async fn bulk_read<O, Q>(
&self,
query: &E::BulkReadQuery,
) -> Result<Vec<DomainObject<E>>> {
let request = self
.http
.get(self.collection_url(E::DOMAIN_TYPE))
domain_type: DomainType,
query: &Q
) -> Result<Vec<DomainObject<O>>>
where
O: DeserializeOwned,
Q: Serialize
{
let url = self.collection_url(domain_type);
let request = self.http
.get(url)
.query(query)
.build()
.unwrap();
let response: DomainCollection<E> = self.query_api(request).await?;
let response: DomainCollection<O> = self.query_api(request).await?;
Ok(response.value)
}
pub(crate) async fn bulk_update_domain_objects<E: BulkUpdateDomainExtension>(
pub(crate) async fn bulk_update<O>(
&self,
entries: &[E::BulkUpdateRequest],
) -> Result<()> {
domain_type: DomainType,
entries: &[O],
) -> Result<()>
where
O: Serialize,
{
#[derive(Serialize)]
struct Request<'a, D: BulkUpdateDomainExtension> {
entries: &'a [D::BulkUpdateRequest],
struct Request<'a, E: Serialize> {
entries: &'a [E],
}
let url = self.bulk_action_url(domain_type, BulkAction::Update);
let body = Request::<O> { entries };
let request = self
.http
.put(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Update))
.json(&Request::<E> { entries })
.put(url)
.json(&body)
.build()
.unwrap();
self.invoke_api(request).await
}
pub(crate) async fn bulk_delete_domain_objects<E: BulkDeleteDomainExtension>(
pub(crate) async fn bulk_delete(
&self,
entries: &[String],
domain_type: DomainType,
entries: &[String]
) -> Result<()> {
#[derive(Serialize)]
struct Request<'a> {
entries: &'a [String],
}
let url = self.bulk_action_url(domain_type, BulkAction::Delete);
let body = Request { entries };
let request = self
.http
.post(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Delete))
.json(&Request { entries })
.post(url)
.json(&body)
.build()
.unwrap();
@ -446,63 +496,3 @@ impl <D> ApiClient<D> {
}
}
}
impl<D: DomainExtension> From<&Client> for ApiClient<D> {
fn from(value: &Client) -> Self {
Self::new(value)
}
}
impl<D: DomainExtension> ApiClient<D> {
pub async fn create(
&self,
request: &D::CreationRequest,
query: &D::CreationQuery,
) -> Result<()> {
self.inner.create_domain_object::<D>(request, query).await
}
pub async fn read(&self, id: impl Display, query: &D::ReadQuery) -> Result<D> {
self.inner
.read_domain_object(id, query)
.await
.map(|obj| obj.extensions)
}
pub async fn update(&self, id: impl Display, request: &D::UpdateRequest) -> Result<()> {
self.inner.update_domain_object::<D>(id, request).await
}
pub async fn delete(&self, id: impl Display, query: &D::DeleteQuery) -> Result<()> {
self.inner.delete_domain_object::<D>(id, query).await
}
}
impl<D: BulkCreateDomainExtension> ApiClient<D> {
pub async fn bulk_create(
&self,
request: &[D::CreationRequest],
query: &D::CreationQuery,
) -> Result<()> {
self.inner
.bulk_create_domain_objects::<D>(request, query)
.await
}
}
impl<D: BulkReadDomainExtension> ApiClient<D> {
pub async fn bulk_read(&self, query: &D::BulkReadQuery) -> Result<Vec<D>> {
let objs = self.inner.bulk_read_domain_objects::<D>(query).await?;
let exts = objs.into_iter().map(|obj| obj.extensions).collect();
Ok(exts)
}
}
impl<D: BulkUpdateDomainExtension> ApiClient<D> {
pub async fn bulk_update(&self, entries: &[D::BulkUpdateRequest]) -> Result<()> {
self.inner.bulk_update_domain_objects::<D>(entries).await
}
}
impl<D: BulkDeleteDomainExtension> ApiClient<D> {
pub async fn bulk_delete(&self, ids: &[String]) -> Result<()> {
self.inner.bulk_delete_domain_objects::<D>(ids).await
}
}

View File

@ -2,6 +2,5 @@ pub mod api;
mod client;
mod error;
pub(crate) use client::ApiClient;
pub use client::{Client, ClientBuilder};
pub use client::{ApiClient, Client, ClientBuilder};
pub use error::{Error, Result};

View File

@ -8,9 +8,13 @@ const PASSWORD: &str = "C5t71DUPAu2D";
#[tokio::main]
async fn main() -> Result<()> {
let log_config = simplelog::ConfigBuilder::new()
.set_location_level(simplelog::LevelFilter::Error)
.build();
simplelog::TermLogger::init(
simplelog::LevelFilter::Trace,
simplelog::Config::default(),
log_config,
simplelog::TerminalMode::Stderr,
simplelog::ColorChoice::Auto,
)
@ -25,7 +29,12 @@ async fn main() -> Result<()> {
test_folders(client.clone()).await?;
test_hosts(client.clone()).await?;
test_hosttags(client.clone()).await?;
test_host_groups(client.clone()).await?;
test_service_groups(client.clone()).await?;
test_contact_groups(client.clone()).await?;
info!("tests done; all pass");
Ok(())
}
@ -46,12 +55,12 @@ async fn test_folders(client: Client) -> Result<()> {
info!("creating testfolder");
folderapi
.create(&creq, &())
.create(&creq)
.await
.inspect_err(|e| error!("failed to create folder: {e}"))?;
let folder = folderapi
.read(TESTDIRQRY, &())
.read(TESTDIRQRY)
.await
.inspect_err(|e| error!("failed to read folder: {e}"))?;
info!("folder config on site: {folder:?}");
@ -136,9 +145,242 @@ async fn test_hosts(client: Client) -> Result<()> {
info!("deleting host");
hostapi
.delete(TESTHOST, &())
.delete(TESTHOST)
.await
.inspect_err(|e| error!("failed to delete host: {e}"))?;
Ok(())
}
async fn test_hosttags(client: Client) -> Result<()> {
use checkmk_api::api::tags::*;
const TESTTAG: &str = "test-tag-group";
info!("testing hosttags");
let tagapi = client.tag_api();
let creq = ExtendedTagGroup::new(TESTTAG.to_string(), "Test Tag Group".to_string())
.with_topic("Testing".to_string())
.with_help("A test tag group".to_string())
.with_tag(Tag::new("test1".to_string(), "Test Tag 1".to_string()))
.with_tag(Tag::new("test2".to_string(), "Test Tag 2".to_string()));
let ureq = TagUpdateRequest::new("Updated Test Tag Group".to_string(), false)
.with_topic("Updated Testing".to_string())
.with_help("An updated test tag group".to_string())
.with_tag(Tag::new("test3".to_string(), "Test Tag 3".to_string()));
info!("creating test tag group");
tagapi
.create(&creq)
.await
.inspect_err(|e| error!("failed to create tag group: {e}"))?;
let tag_group = tagapi
.read(TESTTAG)
.await
.inspect_err(|e| error!("failed to read tag group: {e}"))?;
info!("tag group config: {tag_group:#?}");
info!("updating tag group");
tagapi
.update(TESTTAG, &ureq)
.await
.inspect_err(|e| error!("failed to update tag group: {e}"))?;
let tag_groups = tagapi
.bulk_read()
.await
.inspect_err(|e| error!("failed to read all tag groups: {e}"));
info!("all tag groups: {tag_groups:?}");
info!("deleting tag group");
tagapi
.delete(TESTTAG)
.await
.inspect_err(|e| error!("failed to delete tag group: {e}"))?;
Ok(())
}
async fn test_host_groups(client: Client) -> Result<()> {
use checkmk_api::api::groups::*;
const TESTGROUP: &str = "test-host-group";
info!("testing host groups");
let api = client.host_group_api();
info!("creating test host group");
api.create(&HostGroup::new(TESTGROUP.to_string(), "Test Host Group".to_string()))
.await
.inspect_err(|e| error!("failed to create host group: {e}"))?;
let group = api
.read(TESTGROUP)
.await
.inspect_err(|e| error!("failed to read host group: {e}"))?;
info!("host group config: {group:#?}");
info!("updating host group");
api.update(TESTGROUP, "Updated Test Host Group")
.await
.inspect_err(|e| error!("failed to update host group: {e}"))?;
info!("bulk creating additional host groups");
api.bulk_create(&[
HostGroup::new("test-host-group-2".to_string(), "Test Host Group 2".to_string()),
HostGroup::new("test-host-group-3".to_string(), "Test Host Group 3".to_string()),
])
.await
.inspect_err(|e| error!("failed to bulk create host groups: {e}"))?;
let groups = api
.bulk_read()
.await
.inspect_err(|e| error!("failed to bulk read host groups: {e}"));
info!("all host groups: {groups:?}");
info!("bulk updating host groups");
api.bulk_update(&[
Group::new("test-host-group-2".to_string(), "Updated Host Group 2".to_string()),
Group::new("test-host-group-3".to_string(), "Updated Host Group 3".to_string()),
])
.await
.inspect_err(|e| error!("failed to bulk update host groups: {e}"))?;
info!("bulk deleting extra host groups");
api.bulk_delete(&[
"test-host-group-2".to_string(),
"test-host-group-3".to_string(),
])
.await
.inspect_err(|e| error!("failed to bulk delete host groups: {e}"))?;
info!("deleting test host group");
api.delete(TESTGROUP)
.await
.inspect_err(|e| error!("failed to delete host group: {e}"))?;
Ok(())
}
async fn test_service_groups(client: Client) -> Result<()> {
use checkmk_api::api::groups::*;
const TESTGROUP: &str = "test-service-group";
info!("testing service groups");
let api = client.service_group_api();
info!("creating test service group");
api.create(&ServiceGroup::new(TESTGROUP.to_string(), "Test Service Group".to_string()))
.await
.inspect_err(|e| error!("failed to create service group: {e}"))?;
let group = api
.read(TESTGROUP)
.await
.inspect_err(|e| error!("failed to read service group: {e}"))?;
info!("service group config: {group:#?}");
info!("updating service group");
api.update(TESTGROUP, "Updated Test Service Group")
.await
.inspect_err(|e| error!("failed to update service group: {e}"))?;
info!("bulk creating additional service groups");
api.bulk_create(&[
ServiceGroup::new("test-service-group-2".to_string(), "Test Service Group 2".to_string()),
ServiceGroup::new("test-service-group-3".to_string(), "Test Service Group 3".to_string()),
])
.await
.inspect_err(|e| error!("failed to bulk create service groups: {e}"))?;
let groups = api
.bulk_read()
.await
.inspect_err(|e| error!("failed to bulk read service groups: {e}"));
info!("all service groups: {groups:?}");
info!("bulk updating service groups");
api.bulk_update(&[
Group::new("test-service-group-2".to_string(), "Updated Service Group 2".to_string()),
Group::new("test-service-group-3".to_string(), "Updated Service Group 3".to_string()),
])
.await
.inspect_err(|e| error!("failed to bulk update service groups: {e}"))?;
info!("bulk deleting extra service groups");
api.bulk_delete(&[
"test-service-group-2".to_string(),
"test-service-group-3".to_string(),
])
.await
.inspect_err(|e| error!("failed to bulk delete service groups: {e}"))?;
info!("deleting test service group");
api.delete(TESTGROUP)
.await
.inspect_err(|e| error!("failed to delete service group: {e}"))?;
Ok(())
}
async fn test_contact_groups(client: Client) -> Result<()> {
use checkmk_api::api::groups::*;
const TESTGROUP: &str = "test-contact-group";
info!("testing contact groups");
let api = client.contact_group_api();
info!("creating test contact group");
api.create(&ContactGroup::new(TESTGROUP.to_string(), "Test Contact Group".to_string()))
.await
.inspect_err(|e| error!("failed to create contact group: {e}"))?;
let group = api
.read(TESTGROUP)
.await
.inspect_err(|e| error!("failed to read contact group: {e}"))?;
info!("contact group config: {group:#?}");
info!("updating contact group");
api.update(TESTGROUP, "Updated Test Contact Group")
.await
.inspect_err(|e| error!("failed to update contact group: {e}"))?;
info!("bulk creating additional contact groups");
api.bulk_create(&[
ContactGroup::new("test-contact-group-2".to_string(), "Test Contact Group 2".to_string()),
ContactGroup::new("test-contact-group-3".to_string(), "Test Contact Group 3".to_string()),
])
.await
.inspect_err(|e| error!("failed to bulk create contact groups: {e}"))?;
let groups = api
.bulk_read()
.await
.inspect_err(|e| error!("failed to bulk read contact groups: {e}"));
info!("all contact groups: {groups:?}");
info!("bulk updating contact groups");
api.bulk_update(&[
Group::new("test-contact-group-2".to_string(), "Updated Contact Group 2".to_string()),
Group::new("test-contact-group-3".to_string(), "Updated Contact Group 3".to_string()),
])
.await
.inspect_err(|e| error!("failed to bulk update contact groups: {e}"))?;
info!("bulk deleting extra contact groups");
api.bulk_delete(&[
"test-contact-group-2".to_string(),
"test-contact-group-3".to_string(),
])
.await
.inspect_err(|e| error!("failed to bulk delete contact groups: {e}"))?;
info!("deleting test contact group");
api.delete(TESTGROUP)
.await
.inspect_err(|e| error!("failed to delete contact group: {e}"))?;
Ok(())
}