cargo fmt

This commit is contained in:
Vincent Stuyck 2025-12-20 13:17:03 +01:00
parent cabb054dc5
commit 62fca534a1
5 changed files with 72 additions and 57 deletions

View File

@ -4,10 +4,10 @@ use std::fmt::Display;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::{ApiClient, Client, Result};
use crate::api::{BulkReadDomainExtention, BulkUpdateDomainExtention, DomainExtention};
use crate::api::rules::contactgroups::HostContactGroups;
use crate::api::rules::snmp::SnmpCommunity;
use crate::api::{BulkReadDomainExtention, BulkUpdateDomainExtention, DomainExtention};
use crate::{ApiClient, Client, Result};
pub const ROOT_FOLDER: &str = "/";
@ -132,9 +132,10 @@ pub struct FolderCreationRequest {
impl FolderCreationRequest {
pub fn new(title: String, parent: String) -> Self {
Self {
title, parent,
title,
parent,
name: None,
attributes: None
attributes: None,
}
}
@ -198,7 +199,7 @@ impl FolderUpdateRequest {
title: Some(title),
attributes: None,
update_attributes: None,
remove_attributes: Vec::new()
remove_attributes: Vec::new(),
}
}
pub fn set_title(&mut self, title: String) {
@ -253,18 +254,21 @@ impl BulkUpdateDomainExtention for FolderConfig {
pub struct FolderBulkUpdateRequest {
folder: String,
#[serde(flatten)]
update_request: FolderUpdateRequest
update_request: FolderUpdateRequest,
}
impl FolderBulkUpdateRequest {
pub fn new(folder: String, update_request: FolderUpdateRequest) -> Self {
Self { folder, update_request }
Self {
folder,
update_request,
}
}
}
#[derive(Serialize)]
pub struct MoveFolderRequest {
destination: String
destination: String,
}
impl MoveFolderRequest {
@ -279,11 +283,7 @@ impl ApiClient<FolderConfig> {
"{}/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
}

View File

@ -1,9 +1,11 @@
use serde::{Deserialize, Serialize};
use crate::{Client, ApiClient};
use crate::api::{BulkCreateDomainExtention, BulkDeleteDomainExtention, BulkReadDomainExtention, BulkUpdateDomainExtention, DomainExtention};
use crate::api::folders::FolderAttributes;
use crate::api::{
BulkCreateDomainExtention, BulkDeleteDomainExtention, BulkReadDomainExtention,
BulkUpdateDomainExtention, DomainExtention,
};
use crate::{ApiClient, Client};
impl Client {
pub fn host_api(&self) -> ApiClient<HostConfig> {
@ -18,7 +20,7 @@ pub struct HostConfig {
pub effective_attributes: FolderAttributes,
pub is_cluster: bool,
pub is_offline: bool,
pub cluster_nodes: Option<Vec<String>>
pub cluster_nodes: Option<Vec<String>>,
}
impl DomainExtention for HostConfig {
@ -35,15 +37,15 @@ impl DomainExtention for HostConfig {
pub struct HostCreationRequest {
pub hostname: String,
pub folder: String,
pub attributes: FolderAttributes
pub attributes: FolderAttributes,
}
#[derive(Serialize)]
pub struct HostCreationQuery {
pub bake_agent: bool
pub bake_agent: bool,
}
#[derive(Serialize)]
pub struct HostReadQuery {
pub effective_attributes: bool
pub effective_attributes: bool,
}
#[derive(Serialize)]
pub struct HostUpdateRequest {
@ -57,7 +59,11 @@ pub struct HostUpdateRequest {
impl HostCreationRequest {
pub fn new(hostname: String, folder: String, attributes: FolderAttributes) -> Self {
HostCreationRequest { hostname, folder, attributes }
HostCreationRequest {
hostname,
folder,
attributes,
}
}
}
@ -69,7 +75,9 @@ impl HostCreationQuery {
impl HostReadQuery {
pub fn new(effective_attributes: bool) -> Self {
Self { effective_attributes }
Self {
effective_attributes,
}
}
}
@ -114,7 +122,7 @@ pub struct HostBulkReadQuery {
#[serde(skip_serializing_if = "Vec::is_empty")]
hostnames: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
site: Option<String>
site: Option<String>,
}
impl HostBulkReadQuery {
@ -167,11 +175,14 @@ impl HostBulkReadQuery {
pub struct HostBulkUpdateRequest {
host_name: String,
#[serde(flatten)]
update_request: HostUpdateRequest
update_request: HostUpdateRequest,
}
impl HostBulkUpdateRequest {
pub fn new(host_name: String, update_request: HostUpdateRequest) -> Self {
Self { host_name, update_request }
Self {
host_name,
update_request,
}
}
}

View File

@ -224,7 +224,8 @@ impl InnerClient {
debug!("sending {}-request to {}", request.method(), request.url());
trace!(
"with body: {}",
request.body()
request
.body()
.as_ref()
.and_then(|b| b.as_bytes())
.map(String::from_utf8_lossy)
@ -248,8 +249,7 @@ impl InnerClient {
if status.is_success() {
Ok(body)
} else {
let cmkerror = serde_json::from_str(&body)
.map_err(Error::DeserializeResponse)?;
let cmkerror = serde_json::from_str(&body).map_err(Error::DeserializeResponse)?;
Err(Error::CheckmkError(cmkerror))
}
}
@ -350,13 +350,13 @@ impl InnerClient {
) -> Result<()> {
#[derive(Serialize)]
struct Request<'a, D: BulkCreateDomainExtention> {
entries: &'a [D::CreationRequest]
entries: &'a [D::CreationRequest],
}
let request = self
.http
.post(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Create))
.json(&Request::<E>{ entries })
.json(&Request::<E> { entries })
.query(query)
.build()
.unwrap();
@ -383,13 +383,13 @@ impl InnerClient {
) -> Result<()> {
#[derive(Serialize)]
struct Request<'a, D: BulkUpdateDomainExtention> {
entries: &'a [D::BulkUpdateRequest]
entries: &'a [D::BulkUpdateRequest],
}
let request = self
.http
.put(self.bulk_action_url(E::DOMAIN_TYPE, BulkAction::Update))
.json(&Request::<E>{ entries })
.json(&Request::<E> { entries })
.build()
.unwrap();
@ -401,7 +401,7 @@ impl InnerClient {
) -> Result<()> {
#[derive(Serialize)]
struct Request<'a> {
entries: &'a [String]
entries: &'a [String],
}
let request = self
@ -438,11 +438,11 @@ pub struct ApiClient<D: DomainExtention> {
_marker: PhantomData<D>,
}
impl <D: DomainExtention> From<&Client> for ApiClient<D> {
impl<D: DomainExtention> From<&Client> for ApiClient<D> {
fn from(value: &Client) -> Self {
Self {
inner: value.0.clone(),
_marker: Default::default()
_marker: Default::default(),
}
}
}

View File

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

View File

@ -1,4 +1,3 @@
use checkmk_api::{Client, Result};
use log::{error, info};
@ -13,7 +12,7 @@ async fn main() -> Result<()> {
simplelog::LevelFilter::Trace,
simplelog::Config::default(),
simplelog::TerminalMode::Stderr,
simplelog::ColorChoice::Auto
simplelog::ColorChoice::Auto,
)
.unwrap();
@ -36,40 +35,45 @@ async fn test_folders(client: Client) -> Result<()> {
info!("testing folders");
let folderapi = client.folder_api();
let creq = FolderCreationRequest::new(
"Testing".to_string(),
ROOT_FOLDER.to_string(),
)
.with_name(TESTDIR.to_string());
let ureq1 = FolderUpdateRequest::replace(
FolderAttributes::default()
.with_site("dev".to_string())
);
let creq = FolderCreationRequest::new("Testing".to_string(), ROOT_FOLDER.to_string())
.with_name(TESTDIR.to_string());
let ureq1 =
FolderUpdateRequest::replace(FolderAttributes::default().with_site("dev".to_string()));
let ureq2 = FolderUpdateRequest::update(
FolderAttributes::default()
.with_label("test".to_string(), "testing".to_string())
FolderAttributes::default().with_label("test".to_string(), "testing".to_string()),
);
info!("creating testfolder");
folderapi.create(&creq, &()).await
folderapi
.create(&creq, &())
.await
.inspect_err(|e| error!("failed to create folder: {e}"))?;
let folder = folderapi.read(TESTDIRQRY, &()).await
let folder = folderapi
.read(TESTDIRQRY, &())
.await
.inspect_err(|e| error!("failed to read folder: {e}"))?;
info!("folder config on site: {folder:?}");
info!("updating folder");
folderapi.update(TESTDIRQRY, &ureq1).await
folderapi
.update(TESTDIRQRY, &ureq1)
.await
.inspect_err(|e| error!("failed to update folder: {e}"))?;
let folders = folderapi.bulk_read(&FolderBulkReadQuery::new("~".to_string(), false)).await
let folders = folderapi
.bulk_read(&FolderBulkReadQuery::new("~".to_string(), false))
.await
.inspect_err(|e| error!("failed to read all folders: {e}"));
info!("folder config on site: {folders:?}");
folderapi.bulk_update(&[FolderBulkUpdateRequest::new(TESTDIRQRY.to_string(), ureq2)]).await
folderapi
.bulk_update(&[FolderBulkUpdateRequest::new(TESTDIRQRY.to_string(), ureq2)])
.await
.inspect_err(|e| error!("failed to do a bulk update of folders: {e}"))?;
info!("deleting folder");
folderapi.delete(TESTDIRQRY, &FolderDeleteQuery::default()).await
folderapi
.delete(TESTDIRQRY, &FolderDeleteQuery::default())
.await
.inspect_err(|e| error!("failed to delete folder: {e}"))?;
Ok(())
}