Skip to content

Commit 69dbdd7

Browse files
committed
Updates: Coderabbit suggestions and validations
1 parent cd363c2 commit 69dbdd7

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/alerts/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,10 @@ pub struct AlertRequest {
535535

536536
impl AlertRequest {
537537
pub async fn into(self) -> Result<AlertConfig, AlertError> {
538-
// let mut targets = Vec::new();
539-
// for id in self.targets {
540-
// targets.push(TARGETS.get_target_by_id(id).await?);
541-
// }
538+
// Validate that all target IDs exist
539+
for id in &self.targets {
540+
TARGETS.get_target_by_id(id).await?;
541+
}
542542
let config = AlertConfig {
543543
version: AlertVerison::from(CURRENT_ALERTS_VERSION),
544544
id: Ulid::new(),
@@ -575,10 +575,10 @@ pub struct AlertConfig {
575575

576576
impl AlertConfig {
577577
pub async fn modify(&mut self, alert: AlertRequest) -> Result<(), AlertError> {
578-
// let mut targets = Vec::new();
579-
// for id in alert.targets {
580-
// targets.push(id);
581-
// }
578+
// Validate that all target IDs exist
579+
for id in &alert.targets {
580+
TARGETS.get_target_by_id(id).await?;
581+
}
582582
self.title = alert.title;
583583
self.stream = alert.stream;
584584
self.alert_type = alert.alert_type;
@@ -853,6 +853,8 @@ pub enum AlertError {
853853
FromStrError(#[from] FromStrError),
854854
#[error("Invalid Target ID- {0}")]
855855
InvalidTargetID(String),
856+
#[error("Target already exists")]
857+
DuplicateTargetConfig,
856858
}
857859

858860
impl actix_web::ResponseError for AlertError {
@@ -871,6 +873,7 @@ impl actix_web::ResponseError for AlertError {
871873
Self::InvalidAlertModifyRequest => StatusCode::BAD_REQUEST,
872874
Self::FromStrError(_) => StatusCode::BAD_REQUEST,
873875
Self::InvalidTargetID(_) => StatusCode::BAD_REQUEST,
876+
Self::DuplicateTargetConfig => StatusCode::BAD_REQUEST,
874877
}
875878
}
876879

src/alerts/target.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl TargetConfigs {
9898
}
9999
}
100100

101-
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
101+
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq)]
102102
#[serde(rename_all = "camelCase")]
103103
#[serde(untagged)]
104104
pub enum Retry {
@@ -125,9 +125,19 @@ pub struct Target {
125125
}
126126

127127
impl Target {
128-
pub async fn validate(&self) {
128+
pub async fn validate(&self) -> Result<(), AlertError> {
129129
// just check for liveness
130130
// what if the target is not live yet but is added by the user?
131+
let targets = TARGETS.list().await?;
132+
for target in targets {
133+
if target.target == self.target
134+
&& target.timeout.interval == self.timeout.interval
135+
&& target.timeout.times == self.timeout.times
136+
{
137+
return Err(AlertError::DuplicateTargetConfig);
138+
}
139+
}
140+
Ok(())
131141
}
132142

133143
pub fn call(&self, context: Context) {

src/handlers/http/targets.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub async fn post(
2020
Json(target): Json<Target>,
2121
) -> Result<impl Responder, AlertError> {
2222
// should check for duplicacy and liveness (??)
23-
target.validate().await;
23+
target.validate().await?;
2424

2525
let path = target_json_path(target.id);
2626

@@ -64,8 +64,9 @@ pub async fn update(
6464

6565
// esnure that the supplied target id is assigned to the target config
6666
target.id = target_id;
67+
6768
// should check for duplicacy and liveness (??)
68-
target.validate().await;
69+
target.validate().await?;
6970

7071
let path = target_json_path(target.id);
7172

@@ -88,5 +89,10 @@ pub async fn delete(
8889

8990
let target = TARGETS.delete(&target_id).await?;
9091

92+
let path = target_json_path(target.id);
93+
94+
let store = PARSEABLE.storage.get_object_store();
95+
store.delete_object(&path).await?;
96+
9197
Ok(web::Json(target))
9298
}

0 commit comments

Comments
 (0)