use super::{HostRef, Plugin};
use crate::{prelude::*, util::*};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct HostCheckCommand {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub args: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub plugin: Option<Plugin>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hosts: Option<ConfigRefMap<HostRef>>,
#[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_u64",
default
)]
pub priority: Option<u64>,
#[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 HostCheckCommand {}
impl ConfigObject for HostCheckCommand {
type Builder = HostCheckCommandBuilder;
fn builder() -> Self::Builder {
HostCheckCommandBuilder::new()
}
fn config_path() -> Option<String> {
Some("/config/hostcheckcommand".to_string())
}
fn minimal(name: &str) -> Result<Self, OpsviewConfigError> {
Ok(Self {
name: validate_and_trim_hostcheckcommand_name(name)?,
..Default::default()
})
}
fn unique_name(&self) -> String {
self.name.clone()
}
}
impl Persistent for HostCheckCommand {
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(INLINE_FREE_TEXT_REGEX_STR.to_string())
}
fn validated_name(&self, name: &str) -> Result<String, OpsviewConfigError> {
validate_and_trim_hostcheckcommand_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.hosts = None;
self.id = None;
self.priority = None;
self.ref_ = None;
self.uncommitted = None;
}
}
impl PersistentMap for ConfigObjectMap<HostCheckCommand> {
fn config_path() -> Option<String> {
Some("/config/hostcheckcommand".to_string())
}
}
#[derive(Clone, Debug, Default)]
pub struct HostCheckCommandBuilder {
name: Option<String>,
args: Option<String>,
plugin: Option<Plugin>,
}
impl Builder for HostCheckCommandBuilder {
type ConfigObject = HostCheckCommand;
fn new() -> Self {
HostCheckCommandBuilder::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")?;
let args = require_field(&self.args, "args")?;
let plugin = require_field(&self.plugin, "plugin")?;
Ok(HostCheckCommand {
name: validate_and_trim_hostcheckcommand_name(&name)?,
args: Some(args),
plugin: Some(plugin),
hosts: None,
id: None,
priority: None,
ref_: None,
uncommitted: None,
})
}
}
impl HostCheckCommandBuilder {
pub fn args(mut self, args: &str) -> Self {
self.args = Some(args.to_string());
self
}
pub fn clear_args(mut self) -> Self {
self.args = None;
self
}
pub fn clear_name(mut self) -> Self {
self.name = None;
self
}
pub fn clear_plugin(mut self) -> Self {
self.plugin = None;
self
}
pub fn plugin(mut self, plugin: Plugin) -> Self {
self.plugin = Some(plugin);
self
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct HostCheckCommandRef {
name: String,
#[serde(
rename = "ref",
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_readonly",
default
)]
ref_: Option<String>,
}
impl CreateFromJson for HostCheckCommandRef {}
impl ConfigRef for HostCheckCommandRef {
type FullObject = HostCheckCommand;
fn ref_(&self) -> Option<String> {
self.ref_.clone()
}
fn name(&self) -> String {
self.name.clone()
}
fn unique_name(&self) -> String {
self.name.clone()
}
}
impl From<HostCheckCommand> for HostCheckCommandRef {
fn from(host_check_command: HostCheckCommand) -> Self {
Self {
name: host_check_command.name.clone(),
ref_: host_check_command.ref_.clone(),
}
}
}
impl From<Arc<HostCheckCommand>> for HostCheckCommandRef {
fn from(item: Arc<HostCheckCommand>) -> Self {
let cmd: HostCheckCommand = Arc::try_unwrap(item).unwrap_or_else(|arc| (*arc).clone());
HostCheckCommandRef::from(cmd)
}
}
impl From<&ConfigObjectMap<HostCheckCommand>> for ConfigRefMap<HostCheckCommandRef> {
fn from(check_commands: &ConfigObjectMap<HostCheckCommand>) -> Self {
ref_map_from(check_commands)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_minimal() {
let host_check_command = HostCheckCommand::minimal("My `HostCheckCommand`");
assert_eq!(
host_check_command.unwrap().name,
"My `HostCheckCommand`".to_string()
);
}
#[test]
fn test_default() {
let host_check_command = HostCheckCommand::default();
assert!(host_check_command.name.is_empty());
}
#[test]
fn test_is_valid_hostcheckcommand_name() {
assert!(validate_and_trim_hostcheckcommand_name("My `HostCheckCommand`").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My-HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My_HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My.HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My:HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My,HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My'HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My\"HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("My/HostCheckCommand").is_ok());
assert!(validate_and_trim_hostcheckcommand_name("123").is_ok());
assert!(validate_and_trim_hostcheckcommand_name(&"a".repeat(128)).is_ok());
assert!(validate_and_trim_hostcheckcommand_name("").is_err());
assert!(validate_and_trim_hostcheckcommand_name(" ").is_err());
assert!(validate_and_trim_hostcheckcommand_name(&"a".repeat(129)).is_err());
}
}