use super::{Host, HostRef};
use crate::{prelude::*, util::*};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct HostGroup {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub children: Option<ConfigRefMap<HostGroupRef>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hosts: Option<ConfigRefMap<HostRef>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent: Option<HostGroupRef>,
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_string_or_number_to_u64",
default
)]
pub id: Option<u64>,
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_string_or_number_to_option_bool",
serialize_with = "serialize_option_bool_as_string",
default
)]
pub is_leaf: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub matpath: Option<String>,
#[serde(
rename = "ref",
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_readonly",
default
)]
pub ref_: Option<String>,
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_string_or_number_to_option_bool",
serialize_with = "serialize_option_bool_as_string",
default
)]
pub uncommitted: Option<bool>,
}
impl CreateFromJson for HostGroup {}
impl ConfigObject for HostGroup {
type Builder = HostGroupBuilder;
fn builder() -> Self::Builder {
HostGroupBuilder::new()
}
fn config_path() -> Option<String> {
Some("/config/hostgroup".to_string())
}
fn minimal(name: &str) -> Result<Self, OpsviewConfigError> {
Ok(Self {
name: validate_and_trim_hostgroup_name(name)?,
..Default::default()
})
}
fn unique_name(&self) -> String {
if let Some(matpath) = &self.matpath {
matpath.to_string()
} else {
self.name.clone()
}
}
}
impl Persistent for HostGroup {
fn id(&self) -> Option<u64> {
self.id
}
fn ref_(&self) -> Option<String> {
if self.ref_.as_ref().is_some_and(|x| !x.is_empty()) {
self.ref_.clone()
} else {
None
}
}
fn name(&self) -> Option<String> {
if self.name.is_empty() {
None
} else {
Some(self.name.clone())
}
}
fn name_regex(&self) -> Option<String> {
Some(HOSTGROUP_NAME_REGEX_STR.to_string())
}
fn validated_name(&self, name: &str) -> Result<String, OpsviewConfigError> {
validate_and_trim_hostgroup_name(name)
}
fn set_name(&mut self, new_name: &str) -> Result<String, OpsviewConfigError> {
self.name = self.validated_name(new_name)?;
Ok(self.name.clone())
}
fn clear_readonly(&mut self) {
self.id = None;
self.is_leaf = None;
self.matpath = None;
self.ref_ = None;
self.uncommitted = None;
}
}
#[derive(Clone, Debug, Default)]
pub struct HostGroupBuilder {
children: Option<ConfigRefMap<HostGroupRef>>,
hosts: Option<ConfigRefMap<HostRef>>,
name: Option<String>,
parent: Option<HostGroupRef>,
}
impl Builder for HostGroupBuilder {
type ConfigObject = HostGroup;
fn new() -> Self {
HostGroupBuilder::default()
}
fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_string());
self
}
fn build(self) -> Result<Self::ConfigObject, OpsviewConfigError> {
let name = require_field(&self.name, "name")?;
Ok(HostGroup {
name: validate_and_trim_hostgroup_name(&name)?,
children: self.children,
parent: self.parent,
hosts: self.hosts,
matpath: None,
id: None,
is_leaf: None,
ref_: None,
uncommitted: None,
})
}
}
impl HostGroupBuilder {
pub fn children(mut self, children: &ConfigObjectMap<HostGroup>) -> Self {
self.children = Some(children.into());
self
}
pub fn clear_children(mut self) -> Self {
self.children = None;
self
}
pub fn clear_hosts(mut self) -> Self {
self.hosts = None;
self
}
pub fn clear_name(mut self) -> Self {
self.name = None;
self
}
pub fn clear_parent(mut self) -> Self {
self.parent = None;
self
}
pub fn hosts(mut self, hosts: &ConfigObjectMap<Host>) -> Self {
self.hosts = Some(hosts.into());
self
}
pub fn parent(mut self, parent: HostGroup) -> Self {
self.parent = Some(HostGroupRef::from(parent));
self
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct HostGroupRef {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
matpath: Option<String>,
#[serde(
rename = "ref",
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_readonly",
default
)]
ref_: Option<String>,
}
impl CreateFromJson for HostGroupRef {}
impl ConfigRef for HostGroupRef {
type FullObject = HostGroup;
fn ref_(&self) -> Option<String> {
self.ref_.clone()
}
fn name(&self) -> String {
self.name.clone()
}
fn unique_name(&self) -> String {
if let Some(matpath) = &self.matpath {
matpath.to_string()
} else {
self.name.clone()
}
}
}
impl PersistentMap for ConfigObjectMap<HostGroup> {
fn config_path() -> Option<String> {
Some("/config/hostgroup".to_string())
}
}
impl From<HostGroup> for HostGroupRef {
fn from(hostgroup: HostGroup) -> Self {
HostGroupRef {
name: hostgroup.name.clone(),
matpath: hostgroup.matpath.clone(),
ref_: hostgroup.ref_.clone(),
}
}
}
impl From<Arc<HostGroup>> for HostGroupRef {
fn from(item: Arc<HostGroup>) -> Self {
let hostgroup: HostGroup = Arc::try_unwrap(item).unwrap_or_else(|arc| (*arc).clone());
HostGroupRef::from(hostgroup)
}
}
impl From<&ConfigObjectMap<HostGroup>> for ConfigRefMap<HostGroupRef> {
fn from(host_groups: &ConfigObjectMap<HostGroup>) -> Self {
ref_map_from(host_groups)
}
}
impl HostGroupRef {
pub fn matpath(&self) -> Option<String> {
self.matpath.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let hostgroup = HostGroup::default();
assert!(hostgroup.name.is_empty());
}
#[test]
fn test_minimal() {
let hostgroup = HostGroup::minimal("My HostGroup");
assert_eq!(hostgroup.unwrap().name, "My HostGroup".to_string());
}
#[test]
fn test_is_valid_hostgroup_name() {
assert!(validate_and_trim_hostgroup_name("Host Group 1").is_ok());
assert!(validate_and_trim_hostgroup_name("Another-Valid_HostGroup/Name+123").is_ok());
assert!(validate_and_trim_hostgroup_name(&"A".repeat(128)).is_ok());
assert!(validate_and_trim_hostgroup_name("").is_err()); assert!(validate_and_trim_hostgroup_name(&"A".repeat(129)).is_err());
assert!(validate_and_trim_hostgroup_name("//foo").is_err());
assert!(validate_and_trim_hostgroup_name("/").is_err());
}
}