From 91be1f0acb1f1f919dd5da6cd04a2a262085878c Mon Sep 17 00:00:00 2001 From: Vincent Stuyck Date: Sat, 20 Dec 2025 01:59:41 +0100 Subject: [PATCH] impl DomainExtention for FolderConfig and create corresponding types and constructors --- src/api/folders.rs | 144 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 2 deletions(-) diff --git a/src/api/folders.rs b/src/api/folders.rs index 8176560..b97315b 100644 --- a/src/api/folders.rs +++ b/src/api/folders.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::fmt::Display; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -41,6 +42,57 @@ pub struct FolderAttributes { // pub management: ManagementProtocol, } +impl FolderAttributes { + pub fn set_site(&mut self, site: String) { + self.site = Some(site.to_string()); + } + pub fn with_site(mut self, site: String) -> Self { + self.set_site(site); + self + } + + pub fn set_contactgroups(&mut self, contactgroups: HostContactGroups) { + self.contactgroups = Some(contactgroups); + } + pub fn with_contactgroups(mut self, contactgroups: HostContactGroups) -> Self { + self.set_contactgroups(contactgroups); + self + } + + pub fn add_parent(&mut self, parent: impl Display) { + self.parents.push(parent.to_string()); + } + pub fn with_parent(mut self, parent: impl Display) -> Self { + self.add_parent(parent); + self + } + + pub fn set_snmp_community(&mut self, community: SnmpCommunity) { + self.snmp_community = Some(community); + } + pub fn with_snmp_community(mut self, community: SnmpCommunity) -> Self { + self.set_snmp_community(community); + self + } + + pub fn add_label(&mut self, labelkey: String, labelvalue: String) { + self.labels.insert(labelkey, labelvalue); + } + pub fn with_label(mut self, taggroup: String, tagvalue: String) -> Self { + self.add_label(taggroup, tagvalue); + self + } + + pub fn add_tag(&mut self, taggroup: impl Display, tagvalue: String) { + self.tags + .insert(format!("tag_{taggroup}"), tagvalue.to_string()); + } + pub fn with_tag(mut self, taggroup: impl Display, tagvalue: String) -> Self { + self.add_tag(taggroup, tagvalue); + self + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct MetaData { pub created_at: DateTime, @@ -49,6 +101,94 @@ pub struct MetaData { pub created_by: Option, } -// impl DomainExtention for FolderConfig { +impl DomainExtention for FolderConfig { + const DOMAIN_TYPE: super::DomainType = super::DomainType::FolderConfig; -// } + type CreationRequest = FolderCreationRequest; + type CreationQuery = FolderCreationQuery; + type ReadQuery = FolderReadQuery; + type UpdateRequest = FolderUpdateRequest; + type DeleteQuery = FolderDeleteQuery; +} + +#[derive(Debug, Serialize)] +pub struct FolderCreationRequest { + pub name: String, + pub title: String, + pub parent: String, + pub attributes: FolderAttributes +} + +impl FolderCreationRequest { + pub fn new( + name: String, + title: String, + parent: String, + attributes: FolderAttributes + ) -> Self { + Self { + name, + title, + parent, + attributes, + } + } +} + +#[derive(Debug, Serialize)] +pub struct FolderCreationQuery; +#[derive(Debug, Serialize)] +pub struct FolderReadQuery; + +#[derive(Debug, Serialize)] +pub struct FolderUpdateRequest { + pub title: String, + pub attributes: Option, + pub update_attributes: Option, + pub remove_attributes: Option, +} + +impl FolderUpdateRequest { + pub fn replace(title: String, attributes: FolderAttributes) -> Self { + Self { + title, + attributes: Some(attributes), + update_attributes: None, + remove_attributes: None + } + } + pub fn update(title: String, attributes: FolderAttributes) -> Self { + Self { + title, + attributes: None, + update_attributes: Some(attributes), + remove_attributes: None + } + } + pub fn remove(title: String, attributes: FolderAttributes) -> Self { + Self { + title, + attributes: None, + update_attributes: None, + remove_attributes: Some(attributes) + } + } +} + +#[derive(Debug, Serialize)] +pub struct FolderDeleteQuery { + pub delete_mode: FolderDeleteMode +} + +impl FolderDeleteQuery { + pub fn new(delete_mode: FolderDeleteMode) -> Self { + Self { delete_mode } + } +} + +#[derive(Debug, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum FolderDeleteMode { + Recursive, + AbotyOnNonEmpty +}