feat: added the steps to install discord-webhook-receiver for k8s anywhere topology if not already installed #50
| @ -1,32 +1,43 @@ | |||||||
|  | use super::discord_alert_manager::discord_alert_manager_score; | ||||||
| use async_trait::async_trait; | use async_trait::async_trait; | ||||||
| use serde_json::Value; | use serde::Serialize; | ||||||
|  | use serde_yaml::Value; | ||||||
|  | use tokio::sync::OnceCell; | ||||||
| use url::Url; | use url::Url; | ||||||
| 
 | 
 | ||||||
| use crate::{ | use crate::{ | ||||||
|     interpret::{InterpretError, Outcome}, |     data::{Id, Version}, | ||||||
|     topology::K8sAnywhereTopology, |     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 struct DiscordWebhookConfig { | ||||||
|     pub webhook_url: Url, |     pub webhook_url: Url, | ||||||
|     pub name: String, |     pub name: String, | ||||||
|     pub send_resolved_notifications: bool, |     pub send_resolved_notifications: bool, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub trait DiscordWebhookReceiver { | #[derive(Debug, Clone)] | ||||||
|     fn deploy_discord_webhook_receiver( | pub struct DiscordWebhookReceiverState { | ||||||
|         &self, |     installed: OnceCell<Outcome>, | ||||||
|         _notification_adapter_id: &str, | } | ||||||
|     ) -> Result<Outcome, InterpretError>; |  | ||||||
| 
 | 
 | ||||||
|  | #[async_trait] | ||||||
|  | pub trait DiscordWebhookReceiver { | ||||||
|  |     async fn deploy_discord_webhook_receiver( | ||||||
|  |         &self, | ||||||
|  |         config: DiscordWebhookConfig, | ||||||
|  |         state: DiscordWebhookReceiverState, | ||||||
|  |     ) -> Result<Outcome, InterpretError>; | ||||||
|     fn delete_discord_webhook_receiver( |     fn delete_discord_webhook_receiver( | ||||||
|         &self, |         &self, | ||||||
|         _notification_adapter_id: &str, |         config: DiscordWebhookConfig, | ||||||
|     ) -> Result<Outcome, InterpretError>; |     ) -> Result<Outcome, InterpretError>; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // trait used to generate alert manager config values impl<T: Topology + AlertManagerConfig> Monitor for KubePrometheus
 |  | ||||||
| pub trait AlertManagerConfig<T> { | pub trait AlertManagerConfig<T> { | ||||||
|     fn get_alert_manager_config(&self) -> Result<Value, InterpretError>; |     fn get_alert_manager_config(&self) -> Result<Value, InterpretError>; | ||||||
| } | } | ||||||
| @ -40,16 +51,81 @@ impl<T: DiscordWebhookReceiver> AlertManagerConfig<T> for DiscordWebhookConfig { | |||||||
| 
 | 
 | ||||||
| #[async_trait] | #[async_trait] | ||||||
| impl DiscordWebhookReceiver for K8sAnywhereTopology { | impl DiscordWebhookReceiver for K8sAnywhereTopology { | ||||||
|     fn deploy_discord_webhook_receiver( |     async fn deploy_discord_webhook_receiver( | ||||||
|         &self, |         &self, | ||||||
|         _notification_adapter_id: &str, |         config: DiscordWebhookConfig, | ||||||
|  |         state: DiscordWebhookReceiverState, | ||||||
|     ) -> Result<Outcome, InterpretError> { |     ) -> Result<Outcome, InterpretError> { | ||||||
|         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( |     fn delete_discord_webhook_receiver( | ||||||
|         &self, |         &self, | ||||||
|         _notification_adapter_id: &str, |         _config: DiscordWebhookConfig, | ||||||
|     ) -> Result<Outcome, InterpretError> { |     ) -> Result<Outcome, InterpretError> { | ||||||
|         todo!() |         todo!() | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | #[derive(Debug, Clone, Serialize)] | ||||||
|  | struct DiscordWebhookReceiverScore { | ||||||
|  |     config: DiscordWebhookConfig, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl<T: Topology + HelmCommand> Score<T> for DiscordWebhookReceiverScore { | ||||||
|  |     fn create_interpret(&self) -> Box<dyn Interpret<T>> { | ||||||
|  |         Box::new(DiscordWebhookReceiverScoreInterpret { | ||||||
|  |             config: self.config.clone(), | ||||||
|  |         }) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     fn name(&self) -> String { | ||||||
|  |         "DiscordWebhookReceiverScore".to_string() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | #[derive(Debug)] | ||||||
|  | struct DiscordWebhookReceiverScoreInterpret { | ||||||
|  |     config: DiscordWebhookConfig, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[async_trait] | ||||||
|  | impl<T: Topology + HelmCommand> Interpret<T> for DiscordWebhookReceiverScoreInterpret { | ||||||
|  |     async fn execute( | ||||||
|  |         &self, | ||||||
|  |         inventory: &Inventory, | ||||||
|  |         topology: &T, | ||||||
|  |     ) -> Result<Outcome, InterpretError> { | ||||||
|  |         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<Id> { | ||||||
|  |         todo!() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user