cargo fmt

This commit is contained in:
Vincent Stuyck 2026-02-28 17:56:26 +01:00
parent 71b1d3411d
commit 6de616e8ab
11 changed files with 289 additions and 285 deletions

View File

@ -4,8 +4,8 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;
use crate::{ApiClient, Client, Result};
use crate::api::{DomainCollection, DomainExtension, DomainObject, domain_client, domain_read}; use crate::api::{DomainCollection, DomainExtension, DomainObject, domain_client, domain_read};
use crate::{ApiClient, Client, Result};
domain_client!(ActivationStatus, activation_api); domain_client!(ActivationStatus, activation_api);
@ -18,7 +18,7 @@ pub struct ActivationStatus {
time_started: DateTime<Utc>, time_started: DateTime<Utc>,
changes: Vec<PendingChange>, changes: Vec<PendingChange>,
#[serde(default)] #[serde(default)]
status_per_site: Vec<SiteStatus> status_per_site: Vec<SiteStatus>,
} }
impl DomainExtension for ActivationStatus { impl DomainExtension for ActivationStatus {
@ -44,7 +44,7 @@ pub struct SiteStatus {
status_text: String, status_text: String,
status_details: String, status_details: String,
start_time: DateTime<Utc>, start_time: DateTime<Utc>,
end_time: DateTime<Utc> end_time: DateTime<Utc>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -57,7 +57,7 @@ pub enum ChangePhase {
Sync, Sync,
Activate, Activate,
Finishing, Finishing,
Done Done,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -66,7 +66,7 @@ pub enum ChangePhase {
pub enum ChangeState { pub enum ChangeState {
Success, Success,
Error, Error,
Warning Warning,
} }
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]
@ -74,7 +74,7 @@ pub enum ChangeState {
pub struct ActivateChangeRequest { pub struct ActivateChangeRequest {
redirect: bool, redirect: bool,
sites: Vec<String>, sites: Vec<String>,
force_foreign_changes: bool force_foreign_changes: bool,
} }
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]
@ -82,7 +82,7 @@ pub struct ActivateChangeRequest {
pub struct ActivationOptions { pub struct ActivationOptions {
redirect: bool, redirect: bool,
sites: Vec<String>, sites: Vec<String>,
force_foreign_changes: bool force_foreign_changes: bool,
} }
impl ActivationOptions { impl ActivationOptions {
@ -120,40 +120,38 @@ impl ActivationOptions {
domain_read!(ActivationStatus); domain_read!(ActivationStatus);
impl ApiClient<ActivationStatus> { impl ApiClient<ActivationStatus> {
pub async fn activate_pending_changes( pub async fn activate_pending_changes(&self, options: &ActivationOptions) -> Result<String> {
&self,
options: &ActivationOptions
) -> Result<String> {
let etag = { let etag = {
let url = format!("{}/pending_changes", self.collection_root()); let url = format!("{}/pending_changes", self.collection_root());
self.inner.get_etag(&url).await self.inner.get_etag(&url).await
}?; }?;
let url = format!( let url = format!(
"{}/domain-types/{}/actions/activate-changes/invoke", "{}/domain-types/{}/actions/activate-changes/invoke",
self.inner.url, ActivationStatus::DOMAIN_TYPE self.inner.url,
ActivationStatus::DOMAIN_TYPE
); );
let request = self.inner.http let request = self
.inner
.http
.post(url) .post(url)
.json(&options) .json(&options)
.header(reqwest::header::IF_MATCH, etag) .header(reqwest::header::IF_MATCH, etag)
.build() .build()
.unwrap(); .unwrap();
let response: DomainObject<ActivationStatus> = let response: DomainObject<ActivationStatus> = self.inner.query_api(request).await?;
self.inner.query_api(request).await?;
Ok(response.id) Ok(response.id)
} }
pub async fn await_completion(&self, activation_id: impl Display) -> Result<()> { pub async fn await_completion(&self, activation_id: impl Display) -> Result<()> {
let url = format!( let url = format!(
"{}/objects/{}/{}/actions/wait-for-completion/invoke", "{}/objects/{}/{}/actions/wait-for-completion/invoke",
self.inner.url, ActivationStatus::DOMAIN_TYPE, activation_id self.inner.url,
ActivationStatus::DOMAIN_TYPE,
activation_id
); );
let request = self.inner.http let request = self.inner.http.get(url).build().unwrap();
.get(url)
.build()
.unwrap();
self.inner.invoke_api(request).await self.inner.invoke_api(request).await
} }
@ -162,16 +160,14 @@ impl ApiClient<ActivationStatus> {
fn collection_root(&self) -> String { fn collection_root(&self) -> String {
format!( format!(
"{}/domain-types/{}/collections", "{}/domain-types/{}/collections",
self.inner.url, ActivationStatus::DOMAIN_TYPE self.inner.url,
ActivationStatus::DOMAIN_TYPE
) )
} }
pub async fn read_running_activations(&self) -> Result<Vec<ActivationStatus>> { pub async fn read_running_activations(&self) -> Result<Vec<ActivationStatus>> {
let url = format!("{}/running", self.collection_root()); let url = format!("{}/running", self.collection_root());
let request = self.inner.http let request = self.inner.http.get(url).build().unwrap();
.get(url)
.build()
.unwrap();
let collection: DomainCollection<_> = self.inner.query_api(request).await?; let collection: DomainCollection<_> = self.inner.query_api(request).await?;
Ok(collection.into()) Ok(collection.into())
@ -179,14 +175,11 @@ impl ApiClient<ActivationStatus> {
pub async fn read_pending_changes(&self) -> Result<Vec<PendingChange>> { pub async fn read_pending_changes(&self) -> Result<Vec<PendingChange>> {
let url = format!("{}/pending_changes", self.collection_root()); let url = format!("{}/pending_changes", self.collection_root());
let request = self.inner.http let request = self.inner.http.get(url).build().unwrap();
.get(url)
.build()
.unwrap();
#[derive(Deserialize)] #[derive(Deserialize)]
struct ChangeCollection { struct ChangeCollection {
value: Vec<PendingChange> value: Vec<PendingChange>,
} }
let collection: ChangeCollection = self.inner.query_api(request).await?; let collection: ChangeCollection = self.inner.query_api(request).await?;

View File

@ -8,9 +8,10 @@ use serde_json::Value;
#[cfg(feature = "schemars")] #[cfg(feature = "schemars")]
use schemars::JsonSchema; use schemars::JsonSchema;
use crate::api::rules::contactgroups::HostContactGroups; use crate::api::{
use crate::api::rules::snmp::SnmpCommunity; DomainExtension, domain_bulk_read, domain_bulk_update, domain_client, domain_create,
use crate::api::{DomainExtension, domain_bulk_read, domain_bulk_update, domain_client, domain_create, domain_delete, domain_read, domain_update}; domain_delete, domain_read, domain_update,
};
use crate::{ApiClient, Client, Result}; use crate::{ApiClient, Client, Result};
pub const ROOT_FOLDER: &str = "/"; pub const ROOT_FOLDER: &str = "/";
@ -24,6 +25,7 @@ pub struct FolderConfig {
pub attributes: FolderAttributes, pub attributes: FolderAttributes,
} }
#[allow(dead_code)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))] #[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct FolderAttributes { pub struct FolderAttributes {
@ -46,7 +48,6 @@ pub struct FolderAttributes {
#[serde(default, flatten, skip_serializing_if = "HashMap::is_empty")] #[serde(default, flatten, skip_serializing_if = "HashMap::is_empty")]
pub tags: HashMap<String, String>, pub tags: HashMap<String, String>,
// these fields are used to prevent tags from taking them up // these fields are used to prevent tags from taking them up
#[serde(default)] #[serde(default)]
management_snmp_community: Option<SnmpCommunity>, management_snmp_community: Option<SnmpCommunity>,
@ -112,8 +113,7 @@ impl FolderAttributes {
} }
pub fn add_tag(&mut self, taggroup: impl Display, tagvalue: String) { pub fn add_tag(&mut self, taggroup: impl Display, tagvalue: String) {
self.tags self.tags.insert(format!("tag_{taggroup}"), tagvalue);
.insert(format!("tag_{taggroup}"), tagvalue);
} }
pub fn with_tag(mut self, taggroup: impl Display, tagvalue: String) -> Self { pub fn with_tag(mut self, taggroup: impl Display, tagvalue: String) -> Self {
self.add_tag(taggroup, tagvalue); self.add_tag(taggroup, tagvalue);
@ -130,6 +130,89 @@ pub struct MetaData {
pub created_by: Option<String>, pub created_by: Option<String>,
} }
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum SnmpV3AuthProtocol {
#[serde(rename = "MD5-96")]
Md5,
#[serde(rename = "SHA-1-96")]
Sha96,
#[serde(rename = "SHA-2-224")]
Sha224,
#[serde(rename = "SHA-2-256")]
Sha256,
#[serde(rename = "SHA-2-384")]
Sha384,
#[serde(rename = "SHA-2-512")]
Sha512,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct SnmpV3Auth {
pub auth_protocol: SnmpV3AuthProtocol,
pub security_name: String,
pub auth_password: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum SnmpV3PrivProtocol {
#[serde(rename = "CBC-DES")]
Des,
#[serde(rename = "3DES-EDE")]
Des3,
#[serde(rename = "AES-128")]
Aes128,
#[serde(rename = "AES-192")]
Aes192,
#[serde(rename = "AES-256")]
Aes256,
#[serde(rename = "AES-192-Blumenthal")]
Aes192Blumenthal,
#[serde(rename = "AES-256-Blumenthal")]
Aes256Blumenthal,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct SnmpV3Priv {
pub privacy_protocol: SnmpV3PrivProtocol,
pub privacy_password: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(tag = "type")]
pub enum SnmpCommunity {
V1V2Community {
community: String,
},
NoAuthNoPriv {
security_name: String,
},
AuthNoPriv {
#[serde(flatten)]
auth: SnmpV3Auth,
},
AuthPriv {
#[serde(flatten)]
authentication: SnmpV3Auth,
#[serde(flatten)]
privacy: SnmpV3Priv,
},
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostContactGroups {
pub groups: Vec<String>,
pub r#use: bool,
pub use_for_services: bool,
pub recurse_use: bool,
pub recurse_perms: bool,
}
impl DomainExtension for FolderConfig { impl DomainExtension for FolderConfig {
const DOMAIN_TYPE: super::DomainType = super::DomainType::FolderConfig; const DOMAIN_TYPE: super::DomainType = super::DomainType::FolderConfig;
} }

View File

@ -1,19 +1,17 @@
use core::ops::Deref; use core::ops::Deref;
use serde::{Deserialize, Serialize};
#[cfg(feature = "schemars")] #[cfg(feature = "schemars")]
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::api::{ use crate::api::{DomainCollection, DomainObject, DomainType, domain_client};
DomainCollection, DomainObject, DomainType, domain_client
};
use crate::{ApiClient, Client, Result}; use crate::{ApiClient, Client, Result};
#[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 {
pub name: String, pub name: String,
pub alias: String pub alias: String,
} }
impl Group { impl Group {
@ -28,7 +26,6 @@ impl From<DomainObject<serde_json::Value>> for Group {
} }
} }
macro_rules! domain_group { macro_rules! domain_group {
($id:ident, $typ:expr, $func:ident) => { ($id:ident, $typ:expr, $func:ident) => {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -61,14 +58,13 @@ macro_rules! domain_group {
self.inner.create($typ, group, &()).await self.inner.create($typ, group, &()).await
} }
pub async fn read(&self, id: &str) -> Result<$id> { pub async fn read(&self, id: &str) -> Result<$id> {
let obj: DomainObject<serde_json::Value> = let obj: DomainObject<serde_json::Value> = self.inner.read($typ, id, &()).await?;
self.inner.read($typ, id, &()).await?;
Ok($id::from(obj)) Ok($id::from(obj))
} }
pub async fn update(&self, id: &str, alias: &str) -> Result<()> { pub async fn update(&self, id: &str, alias: &str) -> Result<()> {
#[derive(Serialize)] #[derive(Serialize)]
struct Body<'a> { struct Body<'a> {
alias: &'a str alias: &'a str,
} }
self.inner.update($typ, id, &Body { alias }).await self.inner.update($typ, id, &Body { alias }).await
@ -83,45 +79,48 @@ macro_rules! domain_group {
pub async fn bulk_read(&self) -> Result<Vec<$id>> { pub async fn bulk_read(&self) -> Result<Vec<$id>> {
let collection: DomainCollection<serde_json::Value> = let collection: DomainCollection<serde_json::Value> =
self.inner.bulk_read($typ, &()).await?; self.inner.bulk_read($typ, &()).await?;
let groups = collection.value let groups = collection.value.into_iter().map($id::from).collect();
.into_iter()
.map($id::from)
.collect();
Ok(groups) Ok(groups)
} }
pub async fn bulk_update(&self, entries: &[Group]) -> Result<()> { pub async fn bulk_update(&self, entries: &[Group]) -> Result<()> {
#[derive(Serialize)] #[derive(Serialize)]
struct UpdateAttributes<'a> { struct UpdateAttributes<'a> {
alias: &'a str alias: &'a str,
} }
#[derive(Serialize)] #[derive(Serialize)]
struct UpdateRequest<'a> { struct UpdateRequest<'a> {
name: &'a str, name: &'a str,
attributes: UpdateAttributes<'a> attributes: UpdateAttributes<'a>,
} }
impl<'a> UpdateRequest<'a> { impl<'a> UpdateRequest<'a> {
fn from(value: &'a Group) -> Self { fn from(value: &'a Group) -> Self {
Self { Self {
name: value.name.as_str(), name: value.name.as_str(),
attributes: UpdateAttributes { attributes: UpdateAttributes {
alias: value.alias.as_str() alias: value.alias.as_str(),
} },
} }
} }
} }
let entries = entries.iter() let entries = entries.iter().map(UpdateRequest::from).collect::<Vec<_>>();
.map(UpdateRequest::from)
.collect::<Vec<_>>();
self.inner.bulk_update($typ, &entries).await self.inner.bulk_update($typ, &entries).await
} }
pub async fn bulk_delete(&self, entries: &[String]) -> Result<()> { pub async fn bulk_delete(&self, entries: &[String]) -> Result<()> {
self.inner.bulk_delete($typ, entries).await self.inner.bulk_delete($typ, entries).await
} }
} }
} };
} }
domain_group!(HostGroup, DomainType::HostGroupConfig, host_group_api); domain_group!(HostGroup, DomainType::HostGroupConfig, host_group_api);
domain_group!(ServiceGroup, DomainType::ServiceGroupConfig, service_group_api); domain_group!(
domain_group!(ContactGroup, DomainType::ContactGroupConfig, contact_group_api); ServiceGroup,
DomainType::ServiceGroupConfig,
service_group_api
);
domain_group!(
ContactGroup,
DomainType::ContactGroupConfig,
contact_group_api
);

View File

@ -1,17 +1,17 @@
use std::fmt::Display; use std::fmt::Display;
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
use serde::{Deserialize, Serialize};
#[cfg(feature = "schemars")] #[cfg(feature = "schemars")]
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::api::folders::FolderAttributes; use crate::api::folders::FolderAttributes;
use crate::api::{ use crate::api::{
DomainExtension, domain_bulk_create, domain_bulk_delete, domain_bulk_read, domain_bulk_update, domain_client, domain_create, domain_delete, domain_read, domain_update 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, Result}; use crate::{ApiClient, Client, Result};
domain_client!(HostConfig, host_api); domain_client!(HostConfig, host_api);
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]
@ -25,7 +25,6 @@ pub struct HostConfig {
pub cluster_nodes: Option<Vec<String>>, pub cluster_nodes: Option<Vec<String>>,
} }
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))] #[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostAttributes { pub struct HostAttributes {
@ -41,7 +40,7 @@ pub struct HostAttributes {
additional_ipv6addresses: Vec<Ipv6Addr>, additional_ipv6addresses: Vec<Ipv6Addr>,
#[serde(flatten, default)] #[serde(flatten, default)]
folder_attributes: FolderAttributes folder_attributes: FolderAttributes,
} }
impl DomainExtension for HostConfig { impl DomainExtension for HostConfig {

View File

@ -1,8 +1,7 @@
pub mod activations; pub mod activations;
pub mod folders; pub mod folders;
pub mod hosts;
pub mod groups; pub mod groups;
pub(crate) mod rules; pub mod hosts;
pub mod tags; pub mod tags;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
@ -35,7 +34,7 @@ impl fmt::Display for DomainType {
DomainType::HostGroupConfig => write!(f, "host_group_config"), DomainType::HostGroupConfig => write!(f, "host_group_config"),
DomainType::ServiceGroupConfig => write!(f, "service_group_config"), DomainType::ServiceGroupConfig => write!(f, "service_group_config"),
DomainType::HostTagGroup => write!(f, "host_tag_group"), DomainType::HostTagGroup => write!(f, "host_tag_group"),
DomainType::ActivationRun => write!(f, "activation_run") DomainType::ActivationRun => write!(f, "activation_run"),
} }
} }
} }
@ -93,10 +92,7 @@ pub(crate) struct DomainCollection<E> {
impl<E> From<DomainCollection<E>> for Vec<E> { impl<E> From<DomainCollection<E>> for Vec<E> {
fn from(value: DomainCollection<E>) -> Self { fn from(value: DomainCollection<E>) -> Self {
value.value value.value.into_iter().map(|obj| obj.extensions).collect()
.into_iter()
.map(|obj| obj.extensions)
.collect()
} }
} }
@ -105,15 +101,11 @@ pub trait DomainExtension: DeserializeOwned + Serialize {
} }
impl InnerClient { impl InnerClient {
pub(crate) async fn create_domain_object<E, O, Q>( pub(crate) async fn create_domain_object<E, O, Q>(&self, object: &O, query: &Q) -> Result<()>
&self,
object: &O,
query: &Q,
) -> Result<()>
where where
E: DomainExtension, E: DomainExtension,
O: Serialize, O: Serialize,
Q: Serialize Q: Serialize,
{ {
self.create(E::DOMAIN_TYPE, object, query).await self.create(E::DOMAIN_TYPE, object, query).await
} }
@ -124,7 +116,7 @@ impl InnerClient {
) -> Result<DomainObject<E>> ) -> Result<DomainObject<E>>
where where
E: DomainExtension, E: DomainExtension,
Q: Serialize Q: Serialize,
{ {
self.read(E::DOMAIN_TYPE, id, query).await self.read(E::DOMAIN_TYPE, id, query).await
} }
@ -135,18 +127,14 @@ impl InnerClient {
) -> Result<()> ) -> Result<()>
where where
E: DomainExtension, E: DomainExtension,
O: Serialize O: Serialize,
{ {
self.update(E::DOMAIN_TYPE, id, object).await self.update(E::DOMAIN_TYPE, id, object).await
} }
pub(crate) async fn delete_domain_object<E, Q>( pub(crate) async fn delete_domain_object<E, Q>(&self, id: impl Display, query: &Q) -> Result<()>
&self,
id: impl Display,
query: &Q,
) -> Result<()>
where where
E: DomainExtension, E: DomainExtension,
Q: Serialize Q: Serialize,
{ {
self.delete(E::DOMAIN_TYPE, id, query).await self.delete(E::DOMAIN_TYPE, id, query).await
} }
@ -159,7 +147,7 @@ impl InnerClient {
where where
E: DomainExtension, E: DomainExtension,
O: Serialize, O: Serialize,
Q: Serialize Q: Serialize,
{ {
self.bulk_create(E::DOMAIN_TYPE, entries, query).await self.bulk_create(E::DOMAIN_TYPE, entries, query).await
} }
@ -169,33 +157,25 @@ impl InnerClient {
) -> Result<DomainCollection<E>> ) -> Result<DomainCollection<E>>
where where
E: DomainExtension, E: DomainExtension,
Q: Serialize Q: Serialize,
{ {
self.bulk_read(E::DOMAIN_TYPE, query).await self.bulk_read(E::DOMAIN_TYPE, query).await
} }
pub(crate) async fn bulk_update_domain_objects<E, O>( pub(crate) async fn bulk_update_domain_objects<E, O>(&self, entries: &[O]) -> Result<()>
&self,
entries: &[O],
) -> Result<()>
where where
E: DomainExtension, E: DomainExtension,
O: Serialize O: Serialize,
{ {
self.bulk_update(E::DOMAIN_TYPE, entries).await self.bulk_update(E::DOMAIN_TYPE, entries).await
} }
pub(crate) async fn bulk_delete_domain_objects<E>( pub(crate) async fn bulk_delete_domain_objects<E>(&self, entries: &[String]) -> Result<()>
&self,
entries: &[String],
) -> Result<()>
where where
E: DomainExtension E: DomainExtension,
{ {
self.bulk_delete(E::DOMAIN_TYPE, entries).await self.bulk_delete(E::DOMAIN_TYPE, entries).await
} }
} }
macro_rules! domain_client { macro_rules! domain_client {
($id:ident, $func:ident) => { ($id:ident, $func:ident) => {
impl Client { impl Client {
@ -211,7 +191,7 @@ macro_rules! domain_client {
Self::new(value) Self::new(value)
} }
} }
} };
} }
pub(super) use domain_client; pub(super) use domain_client;
@ -219,14 +199,18 @@ macro_rules! domain_create {
($id:ident, $object:ident) => { ($id:ident, $object:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn create(&self, object: &$object) -> Result<()> { pub async fn create(&self, object: &$object) -> Result<()> {
self.inner.create_domain_object::<$id, _, _>(object, &()).await self.inner
.create_domain_object::<$id, _, _>(object, &())
.await
} }
} }
}; };
($id:ident, $object:ident, $qry:ident) => { ($id:ident, $object:ident, $qry:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn create(&self, object: &$object, query: &$qry) -> Result<()> { pub async fn create(&self, object: &$object, query: &$qry) -> Result<()> {
self.inner.create_domain_object::<$id, _, _>(object, query).await self.inner
.create_domain_object::<$id, _, _>(object, query)
.await
} }
} }
}; };
@ -237,7 +221,9 @@ macro_rules! domain_read {
($id:ident) => { ($id:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn read(&self, id: impl Display) -> Result<$id> { pub async fn read(&self, id: impl Display) -> Result<$id> {
self.inner.read_domain_object::<$id, _>(id, &()).await self.inner
.read_domain_object::<$id, _>(id, &())
.await
.map(|obj| obj.extensions) .map(|obj| obj.extensions)
} }
} }
@ -245,7 +231,9 @@ macro_rules! domain_read {
($id:ident, $qry:ident) => { ($id:ident, $qry:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn read(&self, id: impl Display, query: &$qry) -> Result<$id> { pub async fn read(&self, id: impl Display, query: &$qry) -> Result<$id> {
self.inner.read_domain_object::<$id, _>(id, &query).await self.inner
.read_domain_object::<$id, _>(id, &query)
.await
.map(|obj| obj.extensions) .map(|obj| obj.extensions)
} }
} }
@ -286,14 +274,18 @@ macro_rules! domain_bulk_create {
($id:ident, $object:ident) => { ($id:ident, $object:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn bulk_create(&self, entries: &[$object]) -> Result<()> { pub async fn bulk_create(&self, entries: &[$object]) -> Result<()> {
self.inner.bulk_create_domain_objects::<$id, _, _>(entries, &()).await self.inner
.bulk_create_domain_objects::<$id, _, _>(entries, &())
.await
} }
} }
}; };
($id:ident, $object:ident, $qry:ident) => { ($id:ident, $object:ident, $qry:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn bulk_create(&self, entries: &[$object], query: &$qry) -> Result<()> { pub async fn bulk_create(&self, entries: &[$object], query: &$qry) -> Result<()> {
self.inner.bulk_create_domain_objects::<$id, _, _>(entries, query).await self.inner
.bulk_create_domain_objects::<$id, _, _>(entries, query)
.await
} }
} }
}; };
@ -312,7 +304,10 @@ macro_rules! domain_bulk_read {
($id:ident, $qry:ident) => { ($id:ident, $qry:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn bulk_read(&self, query: &$qry) -> Result<Vec<$id>> { pub async fn bulk_read(&self, query: &$qry) -> Result<Vec<$id>> {
let collection = self.inner.bulk_read_domain_objects::<$id, _>(&query).await?; let collection = self
.inner
.bulk_read_domain_objects::<$id, _>(&query)
.await?;
Ok(collection.into()) Ok(collection.into())
} }
} }
@ -324,7 +319,9 @@ macro_rules! domain_bulk_update {
($id:ident, $object:ident) => { ($id:ident, $object:ident) => {
impl ApiClient<$id> { impl ApiClient<$id> {
pub async fn bulk_update(&self, entries: &[$object]) -> Result<()> { pub async fn bulk_update(&self, entries: &[$object]) -> Result<()> {
self.inner.bulk_update_domain_objects::<$id, _>(entries).await self.inner
.bulk_update_domain_objects::<$id, _>(entries)
.await
} }
} }
}; };

View File

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

View File

@ -1,2 +0,0 @@
pub mod contactgroups;
pub mod snmp;

View File

@ -1,77 +0,0 @@
use serde::{Deserialize, Serialize};
#[cfg(feature = "schemars")]
use schemars::JsonSchema;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum SnmpV3AuthProtocol {
#[serde(rename = "MD5-96")]
Md5,
#[serde(rename = "SHA-1-96")]
Sha96,
#[serde(rename = "SHA-2-224")]
Sha224,
#[serde(rename = "SHA-2-256")]
Sha256,
#[serde(rename = "SHA-2-384")]
Sha384,
#[serde(rename = "SHA-2-512")]
Sha512,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct SnmpV3Auth {
pub auth_protocol: SnmpV3AuthProtocol,
pub security_name: String,
pub auth_password: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum SnmpV3PrivProtocol {
#[serde(rename = "CBC-DES")]
Des,
#[serde(rename = "3DES-EDE")]
Des3,
#[serde(rename = "AES-128")]
Aes128,
#[serde(rename = "AES-192")]
Aes192,
#[serde(rename = "AES-256")]
Aes256,
#[serde(rename = "AES-192-Blumenthal")]
Aes192Blumenthal,
#[serde(rename = "AES-256-Blumenthal")]
Aes256Blumenthal,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct SnmpV3Priv {
pub privacy_protocol: SnmpV3PrivProtocol,
pub privacy_password: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(tag = "type")]
pub enum SnmpCommunity {
V1V2Community {
community: String,
},
NoAuthNoPriv {
security_name: String,
},
AuthNoPriv {
#[serde(flatten)]
auth: SnmpV3Auth,
},
AuthPriv {
#[serde(flatten)]
authentication: SnmpV3Auth,
#[serde(flatten)]
privacy: SnmpV3Priv,
},
}

View File

@ -1,10 +1,13 @@
use crate::api::Display; use crate::api::Display;
use serde::{Deserialize, Serialize};
#[cfg(feature = "schemars")] #[cfg(feature = "schemars")]
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::api::{DomainExtension, DomainType, domain_bulk_read, domain_client, domain_create, domain_delete, domain_read, domain_update}; use crate::api::{
DomainExtension, DomainType, domain_bulk_read, domain_client, domain_create, domain_delete,
domain_read, domain_update,
};
use crate::{ApiClient, Client, Result}; use crate::{ApiClient, Client, Result};
domain_client!(TagGroup, tag_api); domain_client!(TagGroup, tag_api);
@ -15,7 +18,7 @@ pub struct ExtendedTagGroup {
pub id: String, pub id: String,
pub title: String, pub title: String,
#[serde(flatten, default)] #[serde(flatten, default)]
pub inner: TagGroup pub inner: TagGroup,
} }
#[derive(Debug, Default, Clone, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Serialize, Deserialize)]
@ -25,7 +28,7 @@ pub struct TagGroup {
pub topic: String, pub topic: String,
#[serde(default, skip_serializing_if = "String::is_empty")] #[serde(default, skip_serializing_if = "String::is_empty")]
pub help: String, pub help: String,
pub tags: Vec<Tag> pub tags: Vec<Tag>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -33,12 +36,16 @@ pub struct TagGroup {
pub struct Tag { pub struct Tag {
pub id: Option<String>, pub id: Option<String>,
pub title: String, pub title: String,
pub aux_tags: Vec<String> pub aux_tags: Vec<String>,
} }
impl ExtendedTagGroup { impl ExtendedTagGroup {
pub fn new(id: String, title: String) -> Self { pub fn new(id: String, title: String) -> Self {
Self { id, title, inner: Default::default() } Self {
id,
title,
inner: Default::default(),
}
} }
pub fn set_topic(&mut self, topic: String) { pub fn set_topic(&mut self, topic: String) {
@ -69,14 +76,14 @@ impl Tag {
Self { Self {
id: Some(id), id: Some(id),
title, title,
aux_tags: Vec::new() aux_tags: Vec::new(),
} }
} }
pub fn new_default(title: String) -> Self { pub fn new_default(title: String) -> Self {
Self { Self {
id: None, id: None,
title, title,
aux_tags: Vec::new() aux_tags: Vec::new(),
} }
} }
@ -95,12 +102,16 @@ pub struct TagUpdateRequest {
title: String, title: String,
#[serde(flatten, default)] #[serde(flatten, default)]
inner: TagGroup, inner: TagGroup,
repair: bool repair: bool,
} }
impl TagUpdateRequest { impl TagUpdateRequest {
pub fn new(title: String, repair: bool) -> Self { pub fn new(title: String, repair: bool) -> Self {
Self { title, repair, inner: Default::default() } Self {
title,
repair,
inner: Default::default(),
}
} }
pub fn set_topic(&mut self, topic: String) { pub fn set_topic(&mut self, topic: String) {
@ -130,7 +141,7 @@ impl TagUpdateRequest {
#[cfg_attr(feature = "schemars", derive(JsonSchema))] #[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct TagDeleteQuery { pub struct TagDeleteQuery {
repair: bool, repair: bool,
mode: Option<TagDeleteMode> mode: Option<TagDeleteMode>,
} }
impl TagDeleteQuery { impl TagDeleteQuery {
@ -143,7 +154,8 @@ impl TagDeleteQuery {
#[cfg_attr(feature = "schemars", derive(JsonSchema))] #[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum TagDeleteMode { pub enum TagDeleteMode {
Abort, Abort,
#[default] Delete, #[default]
Delete,
Remove, Remove,
} }

View File

@ -10,9 +10,7 @@ use serde::Serialize;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use tokio::sync::Semaphore; use tokio::sync::Semaphore;
use crate::api::{ use crate::api::{DomainCollection, DomainType};
DomainCollection, DomainType,
};
use crate::{Error, Result}; use crate::{Error, Result};
#[derive(Clone)] #[derive(Clone)]
@ -288,11 +286,11 @@ impl InnerClient {
&self, &self,
domain_type: DomainType, domain_type: DomainType,
object: &O, object: &O,
query: &Q query: &Q,
) -> Result<()> ) -> Result<()>
where where
O: Serialize, O: Serialize,
Q: Serialize Q: Serialize,
{ {
let url = self.collection_url(domain_type); let url = self.collection_url(domain_type);
let request = self let request = self
@ -314,15 +312,10 @@ impl InnerClient {
) -> Result<O> ) -> Result<O>
where where
O: DeserializeOwned, O: DeserializeOwned,
Q: Serialize Q: Serialize,
{ {
let url = self.object_url(domain_type, id); let url = self.object_url(domain_type, id);
let request = self let request = self.http.get(url).query(query).build().unwrap();
.http
.get(url)
.query(query)
.build()
.unwrap();
self.query_api(request).await self.query_api(request).await
} }
@ -358,12 +351,7 @@ impl InnerClient {
Q: Serialize, Q: Serialize,
{ {
let url = self.object_url(domain_type, id); let url = self.object_url(domain_type, id);
let request = self let request = self.http.delete(url).query(&query).build().unwrap();
.http
.delete(url)
.query(&query)
.build()
.unwrap();
self.invoke_api(request).await self.invoke_api(request).await
} }
@ -399,27 +387,18 @@ impl InnerClient {
pub(crate) async fn bulk_read<O, Q>( pub(crate) async fn bulk_read<O, Q>(
&self, &self,
domain_type: DomainType, domain_type: DomainType,
query: &Q query: &Q,
) -> Result<DomainCollection<O>> ) -> Result<DomainCollection<O>>
where where
O: DeserializeOwned, O: DeserializeOwned,
Q: Serialize Q: Serialize,
{ {
let url = self.collection_url(domain_type); let url = self.collection_url(domain_type);
let request = self.http let request = self.http.get(url).query(query).build().unwrap();
.get(url)
.query(query)
.build()
.unwrap();
self.query_api(request).await self.query_api(request).await
} }
pub(crate) async fn bulk_update<O>( pub(crate) async fn bulk_update<O>(&self, domain_type: DomainType, entries: &[O]) -> Result<()>
&self,
domain_type: DomainType,
entries: &[O],
) -> Result<()>
where where
O: Serialize, O: Serialize,
{ {
@ -431,19 +410,14 @@ impl InnerClient {
let url = self.bulk_action_url(domain_type, BulkAction::Update); let url = self.bulk_action_url(domain_type, BulkAction::Update);
let body = Request::<O> { entries }; let body = Request::<O> { entries };
let request = self let request = self.http.put(url).json(&body).build().unwrap();
.http
.put(url)
.json(&body)
.build()
.unwrap();
self.invoke_api(request).await self.invoke_api(request).await
} }
pub(crate) async fn bulk_delete( pub(crate) async fn bulk_delete(
&self, &self,
domain_type: DomainType, domain_type: DomainType,
entries: &[String] entries: &[String],
) -> Result<()> { ) -> Result<()> {
#[derive(Serialize)] #[derive(Serialize)]
struct Request<'a> { struct Request<'a> {
@ -453,12 +427,7 @@ impl InnerClient {
let url = self.bulk_action_url(domain_type, BulkAction::Delete); let url = self.bulk_action_url(domain_type, BulkAction::Delete);
let body = Request { entries }; let body = Request { entries };
let request = self let request = self.http.post(url).json(&body).build().unwrap();
.http
.post(url)
.json(&body)
.build()
.unwrap();
self.invoke_api(request).await self.invoke_api(request).await
} }

View File

@ -93,8 +93,8 @@ async fn test_folders(client: Client) -> Result<()> {
} }
async fn test_hosts(client: Client) -> Result<()> { async fn test_hosts(client: Client) -> Result<()> {
use checkmk_api::api::hosts::*;
use checkmk_api::api::folders::FolderAttributes; use checkmk_api::api::folders::FolderAttributes;
use checkmk_api::api::hosts::*;
const TESTHOST: &str = "test-host"; const TESTHOST: &str = "test-host";
info!("testing hosts"); info!("testing hosts");
@ -102,17 +102,17 @@ async fn test_hosts(client: Client) -> Result<()> {
let creq = HostCreationRequest::new( let creq = HostCreationRequest::new(
TESTHOST.to_string(), TESTHOST.to_string(),
"~".to_string(), "~".to_string(),
FolderAttributes::default().with_site("dev".to_string()) FolderAttributes::default().with_site("dev".to_string()),
); );
let cquery = HostCreationQuery::new(false); let cquery = HostCreationQuery::new(false);
let rquery = HostReadQuery::new(true); let rquery = HostReadQuery::new(true);
let ureq1 = HostUpdateRequest::replace( let ureq1 = HostUpdateRequest::replace(
FolderAttributes::default() FolderAttributes::default()
.with_site("dev".to_string()) .with_site("dev".to_string())
.with_label("environment".to_string(), "test".to_string()) .with_label("environment".to_string(), "test".to_string()),
); );
let ureq2 = HostUpdateRequest::update( let ureq2 = HostUpdateRequest::update(
FolderAttributes::default().with_label("purpose".to_string(), "testing".to_string()) FolderAttributes::default().with_label("purpose".to_string(), "testing".to_string()),
); );
info!("creating test host"); info!("creating test host");
@ -211,7 +211,10 @@ async fn test_host_groups(client: Client) -> Result<()> {
let api = client.host_group_api(); let api = client.host_group_api();
info!("creating test host group"); info!("creating test host group");
api.create(&HostGroup::new(TESTGROUP.to_string(), "Test Host Group".to_string())) api.create(&HostGroup::new(
TESTGROUP.to_string(),
"Test Host Group".to_string(),
))
.await .await
.inspect_err(|e| error!("failed to create host group: {e}"))?; .inspect_err(|e| error!("failed to create host group: {e}"))?;
@ -228,8 +231,14 @@ async fn test_host_groups(client: Client) -> Result<()> {
info!("bulk creating additional host groups"); info!("bulk creating additional host groups");
api.bulk_create(&[ api.bulk_create(&[
HostGroup::new("test-host-group-2".to_string(), "Test Host Group 2".to_string()), HostGroup::new(
HostGroup::new("test-host-group-3".to_string(), "Test Host Group 3".to_string()), "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 .await
.inspect_err(|e| error!("failed to bulk create host groups: {e}"))?; .inspect_err(|e| error!("failed to bulk create host groups: {e}"))?;
@ -242,8 +251,14 @@ async fn test_host_groups(client: Client) -> Result<()> {
info!("bulk updating host groups"); info!("bulk updating host groups");
api.bulk_update(&[ api.bulk_update(&[
Group::new("test-host-group-2".to_string(), "Updated Host Group 2".to_string()), Group::new(
Group::new("test-host-group-3".to_string(), "Updated Host Group 3".to_string()), "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 .await
.inspect_err(|e| error!("failed to bulk update host groups: {e}"))?; .inspect_err(|e| error!("failed to bulk update host groups: {e}"))?;
@ -272,7 +287,10 @@ async fn test_service_groups(client: Client) -> Result<()> {
let api = client.service_group_api(); let api = client.service_group_api();
info!("creating test service group"); info!("creating test service group");
api.create(&ServiceGroup::new(TESTGROUP.to_string(), "Test Service Group".to_string())) api.create(&ServiceGroup::new(
TESTGROUP.to_string(),
"Test Service Group".to_string(),
))
.await .await
.inspect_err(|e| error!("failed to create service group: {e}"))?; .inspect_err(|e| error!("failed to create service group: {e}"))?;
@ -289,8 +307,14 @@ async fn test_service_groups(client: Client) -> Result<()> {
info!("bulk creating additional service groups"); info!("bulk creating additional service groups");
api.bulk_create(&[ api.bulk_create(&[
ServiceGroup::new("test-service-group-2".to_string(), "Test Service Group 2".to_string()), ServiceGroup::new(
ServiceGroup::new("test-service-group-3".to_string(), "Test Service Group 3".to_string()), "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 .await
.inspect_err(|e| error!("failed to bulk create service groups: {e}"))?; .inspect_err(|e| error!("failed to bulk create service groups: {e}"))?;
@ -303,8 +327,14 @@ async fn test_service_groups(client: Client) -> Result<()> {
info!("bulk updating service groups"); info!("bulk updating service groups");
api.bulk_update(&[ api.bulk_update(&[
Group::new("test-service-group-2".to_string(), "Updated Service Group 2".to_string()), Group::new(
Group::new("test-service-group-3".to_string(), "Updated Service Group 3".to_string()), "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 .await
.inspect_err(|e| error!("failed to bulk update service groups: {e}"))?; .inspect_err(|e| error!("failed to bulk update service groups: {e}"))?;
@ -372,7 +402,10 @@ async fn test_contact_groups(client: Client) -> Result<()> {
let api = client.contact_group_api(); let api = client.contact_group_api();
info!("creating test contact group"); info!("creating test contact group");
api.create(&ContactGroup::new(TESTGROUP.to_string(), "Test Contact Group".to_string())) api.create(&ContactGroup::new(
TESTGROUP.to_string(),
"Test Contact Group".to_string(),
))
.await .await
.inspect_err(|e| error!("failed to create contact group: {e}"))?; .inspect_err(|e| error!("failed to create contact group: {e}"))?;
@ -389,8 +422,14 @@ async fn test_contact_groups(client: Client) -> Result<()> {
info!("bulk creating additional contact groups"); info!("bulk creating additional contact groups");
api.bulk_create(&[ api.bulk_create(&[
ContactGroup::new("test-contact-group-2".to_string(), "Test Contact Group 2".to_string()), ContactGroup::new(
ContactGroup::new("test-contact-group-3".to_string(), "Test Contact Group 3".to_string()), "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 .await
.inspect_err(|e| error!("failed to bulk create contact groups: {e}"))?; .inspect_err(|e| error!("failed to bulk create contact groups: {e}"))?;
@ -403,8 +442,14 @@ async fn test_contact_groups(client: Client) -> Result<()> {
info!("bulk updating contact groups"); info!("bulk updating contact groups");
api.bulk_update(&[ api.bulk_update(&[
Group::new("test-contact-group-2".to_string(), "Updated Contact Group 2".to_string()), Group::new(
Group::new("test-contact-group-3".to_string(), "Updated Contact Group 3".to_string()), "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 .await
.inspect_err(|e| error!("failed to bulk update contact groups: {e}"))?; .inspect_err(|e| error!("failed to bulk update contact groups: {e}"))?;