Emit warning and continue if the to be deleted user does not exist

This commit is contained in:
Dreaded_X 2025-03-18 01:35:49 +01:00
parent e858baaa3c
commit 5f746b90a8
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
2 changed files with 38 additions and 3 deletions

View File

@ -43,6 +43,10 @@ pub trait ControllerEvents {
async fn user_deleted<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
where
T: Resource<DynamicType = ()> + Sync;
async fn user_not_found<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
where
T: Resource<DynamicType = ()> + Sync;
}
#[async_trait]
@ -99,4 +103,21 @@ impl ControllerEvents for Recorder {
)
.await
}
async fn user_not_found<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
where
T: Resource<DynamicType = ()> + Sync,
{
self.publish(
&Event {
type_: EventType::Warning,
reason: "UserNotFound".into(),
note: Some(format!("User '{username}' not found")),
action: "UserNotFound".into(),
secondary: None,
},
&obj.object_ref(&()),
)
.await
}
}

View File

@ -18,7 +18,7 @@ use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tracing::{debug, instrument, trace};
use tracing::{debug, instrument, trace, warn};
use crate::context::{Context, ControllerEvents};
use crate::lldap;
@ -244,8 +244,22 @@ impl Reconcile for ServiceUser {
let lldap_client = ctx.lldap_config.build_client().await?;
trace!(name, username, "Deleting user");
lldap_client.delete_user(&username).await?;
match lldap_client.delete_user(&username).await {
Err(lldap::Error::GraphQl(err))
if err.message == format!("Entity not found: `No such user: '{username}'`") =>
{
ctx.recorder
.user_not_found(self.as_ref(), &username)
.await?;
warn!(name, username, "User not found");
Ok(())
}
Ok(_) => {
ctx.recorder.user_deleted(self.as_ref(), &username).await?;
Ok(())
}
Err(err) => Err(err),
}?;
Ok(Action::await_change())
}