checkmk-api/src/api/hosts.rs
Vincent Stuyck 6de616e8ab cargo fmt
2026-02-28 17:56:26 +01:00

209 lines
5.7 KiB
Rust

use std::fmt::Display;
use std::net::{Ipv4Addr, Ipv6Addr};
#[cfg(feature = "schemars")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::api::folders::FolderAttributes;
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,
};
use crate::{ApiClient, Client, Result};
domain_client!(HostConfig, host_api);
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostConfig {
pub folder: String,
pub attributes: HostAttributes,
pub effective_attributes: HostAttributes,
pub is_cluster: bool,
pub is_offline: bool,
pub cluster_nodes: Option<Vec<String>>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostAttributes {
#[serde(default)]
alias: Option<String>,
#[serde(default)]
ipaddress: Option<String>,
#[serde(default)]
ipv6address: Option<String>,
#[serde(default)]
additional_ipv4addresses: Vec<Ipv4Addr>,
#[serde(default)]
additional_ipv6addresses: Vec<Ipv6Addr>,
#[serde(flatten, default)]
folder_attributes: FolderAttributes,
}
impl DomainExtension for HostConfig {
const DOMAIN_TYPE: super::DomainType = super::DomainType::HostConfig;
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostCreationRequest {
pub host_name: String,
pub folder: String,
pub attributes: FolderAttributes,
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostCreationQuery {
pub bake_agent: bool,
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostReadQuery {
pub effective_attributes: bool,
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostUpdateRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub attributes: Option<FolderAttributes>,
#[serde(skip_serializing_if = "Option::is_none")]
pub update_attributes: Option<FolderAttributes>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub remove_attributes: Vec<String>,
}
impl HostCreationRequest {
pub fn new(hostname: String, folder: String, attributes: FolderAttributes) -> Self {
HostCreationRequest {
host_name: hostname,
folder,
attributes,
}
}
}
impl HostCreationQuery {
pub fn new(bake_agent: bool) -> Self {
Self { bake_agent }
}
}
impl HostReadQuery {
pub fn new(effective_attributes: bool) -> Self {
Self {
effective_attributes,
}
}
}
impl HostUpdateRequest {
pub fn replace(attributes: FolderAttributes) -> Self {
Self {
attributes: Some(attributes),
update_attributes: None,
remove_attributes: Vec::new(),
}
}
pub fn update(attributes: FolderAttributes) -> Self {
Self {
attributes: None,
update_attributes: Some(attributes),
remove_attributes: Vec::new(),
}
}
pub fn remove(attributes: Vec<String>) -> Self {
Self {
attributes: None,
update_attributes: None,
remove_attributes: attributes,
}
}
}
#[derive(Debug, Default, Clone, Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostBulkReadQuery {
effective_attributes: bool,
#[serde(skip_serializing_if = "Option::is_none")]
fields: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
hostnames: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
site: Option<String>,
}
impl HostBulkReadQuery {
pub fn new() -> Self {
Self::default()
}
pub fn set_effective_attributes(&mut self, effective_attributes: bool) {
self.effective_attributes = effective_attributes;
}
pub fn with_effective_attributes(mut self, effective_attributes: bool) -> Self {
self.set_effective_attributes(effective_attributes);
self
}
pub fn set_fields(&mut self, fields: String) {
self.fields = Some(fields);
}
pub fn with_fields(mut self, fields: String) -> Self {
self.set_fields(fields);
self
}
pub fn add_hostname(&mut self, hostname: String) {
self.hostnames.push(hostname);
}
pub fn with_hostname(mut self, hostname: String) -> Self {
self.add_hostname(hostname);
self
}
pub fn set_hostnames(&mut self, hostnames: Vec<String>) {
self.hostnames = hostnames;
}
pub fn with_hostnames(mut self, hostnames: Vec<String>) -> Self {
self.set_hostnames(hostnames);
self
}
pub fn set_site(&mut self, site: String) {
self.site = Some(site);
}
pub fn with_site(mut self, site: String) -> Self {
self.set_site(site);
self
}
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostBulkUpdateRequest {
host_name: String,
#[serde(flatten)]
update_request: HostUpdateRequest,
}
impl HostBulkUpdateRequest {
pub fn new(host_name: String, update_request: HostUpdateRequest) -> Self {
Self {
host_name,
update_request,
}
}
}
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);