use crate::error::RegentError;
use crate::hosts::managed_host::InternalApiCallOutcome;
use crate::hosts::managed_host::{AssessCompliance, ReachCompliance, Timeout};
use crate::hosts::properties::HostProperties;
use crate::secrets::SecretProvidersPool;
use crate::state::Check;
use crate::state::attribute::HostHandler;
use crate::state::attribute::Privilege;
use crate::state::compliance::AttributeComplianceAssessment;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "PascalCase")]
pub struct DebugBlockExpectedState {
msg: String,
}
impl Timeout for DebugBlockExpectedState {
fn default_timeout(&self) -> Duration {
Duration::from_secs(1)
}
}
impl Check for DebugBlockExpectedState {
fn check(&self) -> Result<(), RegentError> {
Ok(())
}
fn check_host_compatibility(
&self,
_host_properties: &HostProperties,
) -> Result<(), RegentError> {
Ok(())
}
}
impl<Handler: HostHandler> AssessCompliance<Handler> for DebugBlockExpectedState {
async fn assess_compliance(
&self,
_host_handler: &mut Handler,
host_properties: &Option<HostProperties>,
_privilege: &Privilege,
_optional_secret_provider: &Option<SecretProvidersPool>,
) -> Result<AttributeComplianceAssessment, RegentError> {
if let Some(props) = host_properties {
self.check_host_compatibility(props)?;
}
return Ok(AttributeComplianceAssessment::Compliant);
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DebugApiCall {}
impl DebugApiCall {
pub fn display(&self) -> String {
"Debug module".into()
}
}
impl Timeout for DebugApiCall {
fn default_timeout(&self) -> Duration {
Duration::from_secs(1)
}
}
impl Check for DebugApiCall {
fn check(&self) -> Result<(), RegentError> {
Ok(())
}
fn check_host_compatibility(
&self,
_host_properties: &HostProperties,
) -> Result<(), RegentError> {
Ok(())
}
}
impl<Handler: HostHandler> ReachCompliance<Handler> for DebugApiCall {
async fn call(
&self,
_host_handler: &mut Handler,
host_properties: &Option<HostProperties>,
_optional_secret_provider: &Option<SecretProvidersPool>,
) -> Result<InternalApiCallOutcome, RegentError> {
if let Some(props) = host_properties {
self.check_host_compatibility(props)?;
}
Ok(InternalApiCallOutcome::Success(None))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parsing_debug_module_block_from_yaml_str() {
let attribute = "---
Msg: some content
";
let attribute: DebugBlockExpectedState = yaml_serde::from_str(attribute).unwrap();
assert_eq!(attribute.msg, "some content".to_string());
}
}