use core::default::Default;
use core::str::FromStr;
use std::prelude::v1::*;
use serde::{Deserialize, Serialize};
use product_os_configuration::{ConfigError, ProductOSConfig};
use product_os_request::Uri;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandControl {
pub enable: bool,
pub url_address: String,
pub max_servers: u8,
pub max_failures: u8,
pub pulse_check: bool,
pub pulse_check_cron: String,
pub monitor: bool,
pub monitor_cron: String,
pub auto_start_services: bool,
}
impl Default for CommandControl {
fn default() -> Self {
Self {
enable: false,
url_address: "https://localhost:8443".to_string(),
max_servers: 1,
max_failures: 1,
pulse_check: false,
pulse_check_cron: "0 * * * * * *".to_string(),
monitor: false,
monitor_cron: "0 * * * * * *".to_string(),
auto_start_services: false,
}
}
}
impl ProductOSConfig for CommandControl {
const SECTION_KEY: &'static str = "commandControl";
fn validate(&self) -> Result<(), ConfigError> {
let mut errors = Vec::new();
if Uri::from_str(self.url_address.as_str()).is_err() {
errors.push("url_address must be a valid URI (e.g. https://localhost:8443)".to_string());
}
if self.max_servers == 0 {
errors.push("max_servers must be greater than 0".to_string());
}
if self.max_failures == 0 {
errors.push("max_failures must be greater than 0".to_string());
}
if cron::Schedule::from_str(self.pulse_check_cron.as_str()).is_err() {
errors.push(format!("pulse_check_cron is not a valid cron expression: {}", self.pulse_check_cron));
}
if cron::Schedule::from_str(self.monitor_cron.as_str()).is_err() {
errors.push(format!("monitor_cron is not a valid cron expression: {}", self.monitor_cron));
}
if errors.is_empty() { Ok(()) }
else { Err(ConfigError::ValidationError(errors)) }
}
}