@@ -37,6 +37,7 @@ use ulid::Ulid;
3737pub mod alerts_utils;
3838pub mod target;
3939
40+ use crate :: alerts:: target:: TARGETS ;
4041use crate :: parseable:: { StreamNotFound , PARSEABLE } ;
4142use crate :: rbac:: map:: SessionKey ;
4243use crate :: storage;
@@ -513,23 +514,28 @@ pub struct AlertRequest {
513514 pub alert_type : AlertType ,
514515 pub aggregates : Aggregates ,
515516 pub eval_config : EvalConfig ,
516- pub targets : Vec < Target > ,
517+ pub targets : Vec < Ulid > ,
517518}
518519
519- impl From < AlertRequest > for AlertConfig {
520- fn from ( val : AlertRequest ) -> AlertConfig {
521- AlertConfig {
520+ impl AlertRequest {
521+ pub async fn into ( self ) -> Result < AlertConfig , AlertError > {
522+ let mut targets = Vec :: new ( ) ;
523+ for id in self . targets {
524+ targets. push ( TARGETS . get_target_by_id ( id) . await ?) ;
525+ }
526+ let config = AlertConfig {
522527 version : AlertVerison :: from ( CURRENT_ALERTS_VERSION ) ,
523528 id : Ulid :: new ( ) ,
524- severity : val . severity ,
525- title : val . title ,
526- stream : val . stream ,
527- alert_type : val . alert_type ,
528- aggregates : val . aggregates ,
529- eval_config : val . eval_config ,
530- targets : val . targets ,
529+ severity : self . severity ,
530+ title : self . title ,
531+ stream : self . stream ,
532+ alert_type : self . alert_type ,
533+ aggregates : self . aggregates ,
534+ eval_config : self . eval_config ,
535+ targets,
531536 state : AlertState :: default ( ) ,
532- }
537+ } ;
538+ Ok ( config)
533539 }
534540}
535541
@@ -552,14 +558,20 @@ pub struct AlertConfig {
552558}
553559
554560impl AlertConfig {
555- pub fn modify ( & mut self , alert : AlertRequest ) {
561+ pub async fn modify ( & mut self , alert : AlertRequest ) -> Result < ( ) , AlertError > {
562+ let mut targets = Vec :: new ( ) ;
563+ for id in alert. targets {
564+ targets. push ( TARGETS . get_target_by_id ( id) . await ?) ;
565+ }
566+
556567 self . title = alert. title ;
557568 self . stream = alert. stream ;
558569 self . alert_type = alert. alert_type ;
559570 self . aggregates = alert. aggregates ;
560571 self . eval_config = alert. eval_config ;
561- self . targets = alert . targets ;
572+ self . targets = targets;
562573 self . state = AlertState :: default ( ) ;
574+ Ok ( ( ) )
563575 }
564576
565577 pub fn get_base_query ( & self ) -> String {
@@ -801,6 +813,8 @@ pub enum AlertError {
801813 InvalidAlertModifyRequest ,
802814 #[ error( "{0}" ) ]
803815 FromStrError ( #[ from] FromStrError ) ,
816+ #[ error( "Invalid Target ID- {0}" ) ]
817+ InvalidTargetID ( String ) ,
804818}
805819
806820impl actix_web:: ResponseError for AlertError {
@@ -818,6 +832,7 @@ impl actix_web::ResponseError for AlertError {
818832 Self :: Anyhow ( _) => StatusCode :: INTERNAL_SERVER_ERROR ,
819833 Self :: InvalidAlertModifyRequest => StatusCode :: BAD_REQUEST ,
820834 Self :: FromStrError ( _) => StatusCode :: BAD_REQUEST ,
835+ Self :: InvalidTargetID ( _) => StatusCode :: BAD_REQUEST ,
821836 }
822837 }
823838
0 commit comments