diff --git a/harmony/src/modules/monitoring/discord_webhook_sender.rs b/harmony/src/modules/monitoring/discord_webhook_sender.rs index bad6402..f4295b7 100644 --- a/harmony/src/modules/monitoring/discord_webhook_sender.rs +++ b/harmony/src/modules/monitoring/discord_webhook_sender.rs @@ -1,32 +1,43 @@ +use super::discord_alert_manager::discord_alert_manager_score; use async_trait::async_trait; -use serde_json::Value; +use serde::Serialize; +use serde_yaml::Value; +use tokio::sync::OnceCell; use url::Url; use crate::{ - interpret::{InterpretError, Outcome}, - topology::K8sAnywhereTopology, + data::{Id, Version}, + interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome}, + inventory::Inventory, + score::Score, + topology::{HelmCommand, K8sAnywhereTopology, Topology}, }; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize)] pub struct DiscordWebhookConfig { pub webhook_url: Url, pub name: String, pub send_resolved_notifications: bool, } -pub trait DiscordWebhookReceiver { - fn deploy_discord_webhook_receiver( - &self, - _notification_adapter_id: &str, - ) -> Result; +#[derive(Debug, Clone)] +pub struct DiscordWebhookReceiverState { + installed: OnceCell, +} +#[async_trait] +pub trait DiscordWebhookReceiver { + async fn deploy_discord_webhook_receiver( + &self, + config: DiscordWebhookConfig, + state: DiscordWebhookReceiverState, + ) -> Result; fn delete_discord_webhook_receiver( &self, - _notification_adapter_id: &str, + config: DiscordWebhookConfig, ) -> Result; } -// trait used to generate alert manager config values impl Monitor for KubePrometheus pub trait AlertManagerConfig { fn get_alert_manager_config(&self) -> Result; } @@ -40,16 +51,81 @@ impl AlertManagerConfig for DiscordWebhookConfig { #[async_trait] impl DiscordWebhookReceiver for K8sAnywhereTopology { - fn deploy_discord_webhook_receiver( + async fn deploy_discord_webhook_receiver( &self, - _notification_adapter_id: &str, + config: DiscordWebhookConfig, + state: DiscordWebhookReceiverState, ) -> Result { - todo!() + let discord_webhook_receiver_score = DiscordWebhookReceiverScore { config }; + let state = state + .installed + .get_or_try_init(|| { + let inventory = Inventory::autoload(); + let interpret = discord_webhook_receiver_score.create_interpret(); + async move { interpret.execute(&inventory, self).await } + }) + .await?; + Ok(state.clone()) } fn delete_discord_webhook_receiver( &self, - _notification_adapter_id: &str, + _config: DiscordWebhookConfig, ) -> Result { todo!() } } + +#[derive(Debug, Clone, Serialize)] +struct DiscordWebhookReceiverScore { + config: DiscordWebhookConfig, +} + +impl Score for DiscordWebhookReceiverScore { + fn create_interpret(&self) -> Box> { + Box::new(DiscordWebhookReceiverScoreInterpret { + config: self.config.clone(), + }) + } + + fn name(&self) -> String { + "DiscordWebhookReceiverScore".to_string() + } +} +#[derive(Debug)] +struct DiscordWebhookReceiverScoreInterpret { + config: DiscordWebhookConfig, +} + +#[async_trait] +impl Interpret for DiscordWebhookReceiverScoreInterpret { + async fn execute( + &self, + inventory: &Inventory, + topology: &T, + ) -> Result { + discord_alert_manager_score( + self.config.webhook_url.clone(), + self.config.name.clone(), + self.config.name.clone(), + ) + .create_interpret() + .execute(inventory, topology) + .await + } + + fn get_name(&self) -> InterpretName { + todo!() + } + + fn get_version(&self) -> Version { + todo!() + } + + fn get_status(&self) -> InterpretStatus { + todo!() + } + + fn get_children(&self) -> Vec { + todo!() + } +}