feat: implement update handlers for upstream and upstream target management
This commit is contained in:
@@ -142,7 +142,16 @@ impl UpstreamService {
|
||||
upstream: UpdateUpstreamInfo,
|
||||
tx: Option<&mut DatabaseTransaction>,
|
||||
) -> Result<UpstreamInfo, ServiceError> {
|
||||
let current_model = with_conn!(&*self.connection, tx, conn, {
|
||||
// If a transaction was provided use it, otherwise create and own one here.
|
||||
let mut maybe_owned_tx: Option<DatabaseTransaction> = None;
|
||||
let tx_ref: Option<&mut DatabaseTransaction> = if let Some(tx) = tx {
|
||||
Some(tx)
|
||||
} else {
|
||||
maybe_owned_tx = Some(self.connection.begin().await?);
|
||||
maybe_owned_tx.as_mut()
|
||||
};
|
||||
|
||||
let current_model = with_conn!(&*self.connection, tx_ref, conn, {
|
||||
upstream::Entity::find_by_id(id)
|
||||
.one(*conn)
|
||||
.await?
|
||||
@@ -151,9 +160,36 @@ impl UpstreamService {
|
||||
id
|
||||
)))?
|
||||
});
|
||||
let active_model = upstream.apply_to_model(current_model);
|
||||
let upstream_active_model = upstream.clone().apply_to_model(current_model);
|
||||
|
||||
let r = active_model.update(&*self.connection).await?;
|
||||
let r = with_conn!(&*self.connection, tx_ref, conn, {
|
||||
let updated_upstream_model = upstream_active_model.update(*conn).await?;
|
||||
|
||||
// update upstream targets if any
|
||||
if let Some(targets) = upstream.upstream_targets {
|
||||
for (target_id, enabled) in targets.into_iter() {
|
||||
let target_model = upstream_target::Entity::find_by_id(target_id)
|
||||
.one(*conn)
|
||||
.await?
|
||||
.ok_or(ServiceError::NotFound(format!(
|
||||
"Upstream target with id {} not found",
|
||||
target_id
|
||||
)))?;
|
||||
let mut target_active_model: upstream_target::ActiveModel = target_model.into();
|
||||
target_active_model.enabled = sea_orm::ActiveValue::Set(enabled);
|
||||
|
||||
target_active_model.update(*conn).await?;
|
||||
Ok::<(), ServiceError>(())?;
|
||||
}
|
||||
}
|
||||
|
||||
updated_upstream_model
|
||||
});
|
||||
|
||||
// Commit
|
||||
if let Some(t) = maybe_owned_tx.take() {
|
||||
t.commit().await?;
|
||||
}
|
||||
Ok(r.into())
|
||||
}
|
||||
|
||||
@@ -494,14 +530,10 @@ mod tests {
|
||||
let svc = UpstreamService::new(Arc::new(db));
|
||||
|
||||
let update_info = crate::services::nginx::info::upstream::UpdateUpstreamInfo {
|
||||
id: None,
|
||||
name: None,
|
||||
protocol: None,
|
||||
algorithm: None,
|
||||
sticky_session: None,
|
||||
created_by: None,
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
upstream_targets: None,
|
||||
};
|
||||
let res = svc.update_upstream(id, update_info, None).await;
|
||||
@@ -522,14 +554,11 @@ mod tests {
|
||||
.update_upstream(
|
||||
uuid::Uuid::new_v4(),
|
||||
crate::services::nginx::info::upstream::UpdateUpstreamInfo {
|
||||
id: None,
|
||||
name: None,
|
||||
protocol: None,
|
||||
algorithm: None,
|
||||
sticky_session: None,
|
||||
created_by: None,
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
|
||||
upstream_targets: None,
|
||||
},
|
||||
None,
|
||||
@@ -650,16 +679,11 @@ mod tests {
|
||||
let svc = UpstreamService::new(Arc::new(db));
|
||||
|
||||
let update_info = crate::services::nginx::info::upstream_target::UpdateUpstreamTargetInfo {
|
||||
id: None,
|
||||
target_host: None,
|
||||
target_port: None,
|
||||
weight: None,
|
||||
is_backup: None,
|
||||
enabled: None,
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
upstream_id: None,
|
||||
upstream: None,
|
||||
};
|
||||
let res = svc.update_upstream_target(id, update_info, None).await;
|
||||
assert!(res.is_ok());
|
||||
|
||||
Reference in New Issue
Block a user