use super::{NotificationProfileRef, SharedNotificationProfileRef, Variable, VariableValueRef};
use crate::{prelude::*, util::*};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct NotificationMethod {
pub name: 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 active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub command: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub contact_variables: 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 master: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub namespace: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<ConfigRefMap<VariableValueRef>>,
#[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")]
pub notificationprofiles: Option<ConfigRefMap<NotificationProfileRef>>,
#[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")]
pub sharednotificationprofiles: Option<ConfigRefMap<SharedNotificationProfileRef>>,
#[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 NotificationMethod {}
impl ConfigObject for NotificationMethod {
type Builder = NotificationMethodBuilder;
fn builder() -> Self::Builder {
NotificationMethodBuilder::new()
}
fn config_path() -> Option<String> {
Some("/config/notificationmethod".to_string())
}
fn unique_name(&self) -> String {
self.name.clone()
}
}
impl Persistent for NotificationMethod {
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(NOTIFICATIONMETHOD_NAME_REGEX_STR.to_string())
}
fn validated_name(&self, name: &str) -> Result<String, OpsviewConfigError> {
validate_and_trim_notificationmethod_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.notificationprofiles = None;
self.ref_ = None;
self.sharednotificationprofiles = None;
self.uncommitted = None;
}
}
impl PersistentMap for ConfigObjectMap<NotificationMethod> {
fn config_path() -> Option<String> {
Some("/config/notificationmethod".to_string())
}
}
#[derive(Clone, Debug, Default)]
pub struct NotificationMethodBuilder {
name: Option<String>,
active: Option<bool>,
command: Option<String>,
contact_variables: Option<String>,
master: Option<bool>,
namespace: Option<String>,
variables: Option<ConfigRefMap<VariableValueRef>>,
}
impl Builder for NotificationMethodBuilder {
type ConfigObject = NotificationMethod;
fn new() -> Self {
NotificationMethodBuilder::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 validated_command =
validate_opt_string(self.command, validate_and_trim_notification_command)?;
Ok(NotificationMethod {
name: validate_and_trim_notificationmethod_name(&name)?,
active: self.active,
command: validated_command,
contact_variables: self.contact_variables,
master: self.master,
namespace: self.namespace,
variables: self.variables,
id: None,
notificationprofiles: None,
ref_: None,
sharednotificationprofiles: None,
uncommitted: None,
})
}
}
impl NotificationMethodBuilder {
pub fn active(mut self, active: bool) -> Self {
self.active = Some(active);
self
}
pub fn clear_active(mut self) -> Self {
self.active = None;
self
}
pub fn clear_command(mut self) -> Self {
self.command = None;
self
}
pub fn clear_contact_variables(mut self) -> Self {
self.contact_variables = None;
self
}
pub fn clear_master(mut self) -> Self {
self.master = None;
self
}
pub fn clear_name(mut self) -> Self {
self.name = None;
self
}
pub fn clear_namespace(mut self) -> Self {
self.namespace = None;
self
}
pub fn clear_variables(mut self) -> Self {
self.variables = None;
self
}
pub fn command(mut self, command: &str) -> Self {
self.command = Some(command.to_string());
self
}
pub fn contact_variables(mut self, contact_variables: &str) -> Self {
self.contact_variables = Some(contact_variables.to_string());
self
}
pub fn master(mut self, master: bool) -> Self {
self.master = Some(master);
self
}
pub fn namespace(mut self, namespace: &str) -> Self {
self.namespace = Some(namespace.to_string());
self
}
pub fn variables(mut self, variables: &ConfigObjectMap<Variable>) -> Self {
self.variables = Some(variables.into());
self
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct NotificationMethodRef {
name: String,
#[serde(
rename = "ref",
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_readonly",
default
)]
ref_: Option<String>,
}
impl CreateFromJson for NotificationMethodRef {}
impl ConfigRef for NotificationMethodRef {
type FullObject = NotificationMethod;
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<NotificationMethod> for NotificationMethodRef {
fn from(notification_method: NotificationMethod) -> Self {
Self {
name: notification_method.name.clone(),
ref_: notification_method.ref_.clone(),
}
}
}
impl From<Arc<NotificationMethod>> for NotificationMethodRef {
fn from(item: Arc<NotificationMethod>) -> Self {
let cmd: NotificationMethod = Arc::try_unwrap(item).unwrap_or_else(|arc| (*arc).clone());
NotificationMethodRef::from(cmd)
}
}
impl From<&ConfigObjectMap<NotificationMethod>> for ConfigRefMap<NotificationMethodRef> {
fn from(methods: &ConfigObjectMap<NotificationMethod>) -> Self {
ref_map_from(methods)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let method = NotificationMethod::default();
assert!(method.name.is_empty());
}
#[test]
fn test_minimal() {
let method = NotificationMethod::minimal("My NotificationMethod");
assert_eq!(method.unwrap().name, "My NotificationMethod".to_string());
}
}