@@ -37,15 +37,14 @@ 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;
4344use crate :: storage:: ObjectStorageError ;
4445use crate :: sync:: alert_runtime;
4546use crate :: utils:: user_auth_for_query;
4647
47- use self :: target:: Target ;
48-
4948// these types describe the scheduled task for an alert
5049pub type ScheduledTaskHandlers = ( JoinHandle < ( ) > , Receiver < ( ) > , Sender < ( ) > ) ;
5150
@@ -531,23 +530,28 @@ pub struct AlertRequest {
531530 pub alert_type : AlertType ,
532531 pub aggregates : Aggregates ,
533532 pub eval_config : EvalConfig ,
534- pub targets : Vec < Target > ,
533+ pub targets : Vec < Ulid > ,
535534}
536535
537- impl From < AlertRequest > for AlertConfig {
538- fn from ( val : AlertRequest ) -> AlertConfig {
539- AlertConfig {
536+ impl AlertRequest {
537+ pub async fn into ( self ) -> Result < AlertConfig , AlertError > {
538+ // Validate that all target IDs exist
539+ for id in & self . targets {
540+ TARGETS . get_target_by_id ( id) . await ?;
541+ }
542+ let config = AlertConfig {
540543 version : AlertVerison :: from ( CURRENT_ALERTS_VERSION ) ,
541544 id : Ulid :: new ( ) ,
542- severity : val . severity ,
543- title : val . title ,
544- stream : val . stream ,
545- alert_type : val . alert_type ,
546- aggregates : val . aggregates ,
547- eval_config : val . eval_config ,
548- targets : val . targets ,
545+ severity : self . severity ,
546+ title : self . title ,
547+ stream : self . stream ,
548+ alert_type : self . alert_type ,
549+ aggregates : self . aggregates ,
550+ eval_config : self . eval_config ,
551+ targets : self . targets ,
549552 state : AlertState :: default ( ) ,
550- }
553+ } ;
554+ Ok ( config)
551555 }
552556}
553557
@@ -563,21 +567,26 @@ pub struct AlertConfig {
563567 pub alert_type : AlertType ,
564568 pub aggregates : Aggregates ,
565569 pub eval_config : EvalConfig ,
566- pub targets : Vec < Target > ,
570+ pub targets : Vec < Ulid > ,
567571 // for new alerts, state should be resolved
568572 #[ serde( default ) ]
569573 pub state : AlertState ,
570574}
571575
572576impl AlertConfig {
573- pub fn modify ( & mut self , alert : AlertRequest ) {
577+ pub async fn modify ( & mut self , alert : AlertRequest ) -> Result < ( ) , AlertError > {
578+ // Validate that all target IDs exist
579+ for id in & alert. targets {
580+ TARGETS . get_target_by_id ( id) . await ?;
581+ }
574582 self . title = alert. title ;
575583 self . stream = alert. stream ;
576584 self . alert_type = alert. alert_type ;
577585 self . aggregates = alert. aggregates ;
578586 self . eval_config = alert. eval_config ;
579587 self . targets = alert. targets ;
580588 self . state = AlertState :: default ( ) ;
589+ Ok ( ( ) )
581590 }
582591
583592 pub fn get_base_query ( & self ) -> String {
@@ -599,7 +608,8 @@ impl AlertConfig {
599608 } ;
600609
601610 // validate that target repeat notifs !> eval_frequency
602- for target in & self . targets {
611+ for target_id in & self . targets {
612+ let target = TARGETS . get_target_by_id ( target_id) . await ?;
603613 match & target. timeout . times {
604614 target:: Retry :: Infinite => { }
605615 target:: Retry :: Finite ( repeat) => {
@@ -806,7 +816,8 @@ impl AlertConfig {
806816 pub async fn trigger_notifications ( & self , message : String ) -> Result < ( ) , AlertError > {
807817 let mut context = self . get_context ( ) ;
808818 context. message = message;
809- for target in & self . targets {
819+ for target_id in & self . targets {
820+ let target = TARGETS . get_target_by_id ( target_id) . await ?;
810821 trace ! ( "Target (trigger_notifications)-\n {target:?}" ) ;
811822 target. call ( context. clone ( ) ) ;
812823 }
@@ -840,6 +851,12 @@ pub enum AlertError {
840851 InvalidAlertModifyRequest ,
841852 #[ error( "{0}" ) ]
842853 FromStrError ( #[ from] FromStrError ) ,
854+ #[ error( "Invalid Target ID- {0}" ) ]
855+ InvalidTargetID ( String ) ,
856+ #[ error( "Target already exists" ) ]
857+ DuplicateTargetConfig ,
858+ #[ error( "Can't delete a Target which is being used" ) ]
859+ TargetInUse ,
843860}
844861
845862impl actix_web:: ResponseError for AlertError {
@@ -857,6 +874,9 @@ impl actix_web::ResponseError for AlertError {
857874 Self :: Anyhow ( _) => StatusCode :: INTERNAL_SERVER_ERROR ,
858875 Self :: InvalidAlertModifyRequest => StatusCode :: BAD_REQUEST ,
859876 Self :: FromStrError ( _) => StatusCode :: BAD_REQUEST ,
877+ Self :: InvalidTargetID ( _) => StatusCode :: BAD_REQUEST ,
878+ Self :: DuplicateTargetConfig => StatusCode :: BAD_REQUEST ,
879+ Self :: TargetInUse => StatusCode :: CONFLICT ,
860880 }
861881 }
862882
0 commit comments