use super::{ServiceCheck, ServiceCheckRef};
use crate::{prelude::*, util::*};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct ServiceGroup {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub alias: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub servicechecks: Option<ConfigRefMap<ServiceCheckRef>>,
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_string_or_number_to_u64",
default
)]
pub id: 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 ServiceGroup {}
impl ConfigObject for ServiceGroup {
type Builder = ServiceGroupBuilder;
fn builder() -> Self::Builder {
ServiceGroupBuilder::new()
}
fn config_path() -> Option<String> {
Some("/config/servicegroup".to_string())
}
fn minimal(name: &str) -> Result<Self, OpsviewConfigError> {
Ok(Self {
name: validate_and_trim_servicegroup_name(name)?,
..Default::default()
})
}
fn unique_name(&self) -> String {
self.name.clone()
}
}
impl Persistent for ServiceGroup {
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(SERVICEGROUP_NAME_REGEX_STR.to_string())
}
fn validated_name(&self, name: &str) -> Result<String, OpsviewConfigError> {
validate_and_trim_servicegroup_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.ref_ = None;
self.uncommitted = None;
}
}
impl PersistentMap for ConfigObjectMap<ServiceGroup> {
fn config_path() -> Option<String> {
Some("/config/servicegroup".to_string())
}
}
#[derive(Clone, Debug, Default)]
pub struct ServiceGroupBuilder {
name: Option<String>,
alias: Option<String>,
servicechecks: Option<ConfigRefMap<ServiceCheckRef>>,
}
impl Builder for ServiceGroupBuilder {
type ConfigObject = ServiceGroup;
fn new() -> Self {
Self::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_alias = validate_opt_string(self.alias, validate_and_trim_description)?;
Ok(ServiceGroup {
name: validate_and_trim_servicegroup_name(&name)?,
alias: validated_alias,
servicechecks: self.servicechecks,
id: None,
ref_: None,
uncommitted: None,
})
}
}
impl ServiceGroupBuilder {
pub fn alias(mut self, alias: &str) -> Self {
self.alias = Some(alias.to_string());
self
}
pub fn clear_alias(mut self) -> Self {
self.alias = None;
self
}
pub fn clear_name(mut self) -> Self {
self.name = None;
self
}
pub fn clear_servicechecks(mut self) -> Self {
self.servicechecks = None;
self
}
pub fn servicechecks(mut self, servicechecks: &ConfigObjectMap<ServiceCheck>) -> Self {
self.servicechecks = Some(servicechecks.into());
self
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct ServiceGroupRef {
name: String,
#[serde(
rename = "ref",
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_readonly",
default
)]
ref_: Option<String>,
}
impl CreateFromJson for ServiceGroupRef {}
impl ConfigRef for ServiceGroupRef {
type FullObject = ServiceGroup;
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<ServiceGroup> for ServiceGroupRef {
fn from(service_group: ServiceGroup) -> Self {
Self {
name: service_group.name.clone(),
ref_: service_group.ref_.clone(),
}
}
}
impl From<Arc<ServiceGroup>> for ServiceGroupRef {
fn from(item: Arc<ServiceGroup>) -> Self {
let cmd: ServiceGroup = Arc::try_unwrap(item).unwrap_or_else(|arc| (*arc).clone());
ServiceGroupRef::from(cmd)
}
}
impl From<&ConfigObjectMap<ServiceGroup>> for ConfigRefMap<ServiceGroupRef> {
fn from(groups: &ConfigObjectMap<ServiceGroup>) -> Self {
ref_map_from(groups)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let service_group = ServiceGroup::default();
assert_eq!(service_group.name, "".to_string());
}
#[test]
fn test_minimal() {
let service_group = ServiceGroup::minimal("my group");
assert_eq!(service_group.unwrap().name, "my group".to_string());
}
}