reductstore 1.19.8

ReductStore is a time series database designed specifically for storing and managing large amounts of blob data.
Documentation
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

use crate::api::http::replication::ReplicationModePayloadAxum;
use crate::api::http::{HttpError, StateKeeper};
use crate::auth::policy::FullAccessPolicy;
use axum::extract::{Path, State};
use axum_extra::headers::HeaderMap;
use std::sync::Arc;

// PATCH /api/v1/replications/:replication_name/mode
pub(super) async fn set_mode(
    State(keeper): State<Arc<StateKeeper>>,
    Path(replication_name): Path<String>,
    headers: HeaderMap,
    payload: ReplicationModePayloadAxum,
) -> Result<(), HttpError> {
    let components = keeper
        .get_with_permissions(&headers, FullAccessPolicy {})
        .await?;

    components
        .replication_repo
        .write()
        .await?
        .set_mode(&replication_name, payload.0.mode)
        .await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::http::replication::tests::settings;
    use crate::api::http::tests::{headers, keeper};
    use reduct_base::msg::replication_api::{
        ReplicationMode, ReplicationModePayload, ReplicationSettings,
    };
    use rstest::rstest;
    use std::sync::Arc;

    #[rstest]
    #[tokio::test]
    async fn test_set_mode(
        #[future] keeper: Arc<StateKeeper>,
        headers: HeaderMap,
        settings: ReplicationSettings,
    ) {
        let keeper = keeper.await;
        let components = keeper.get_anonymous().await.unwrap();
        components
            .replication_repo
            .write()
            .await
            .unwrap()
            .create_replication("test", settings)
            .await
            .unwrap();

        set_mode(
            State(Arc::clone(&keeper)),
            Path("test".to_string()),
            headers,
            ReplicationModePayload {
                mode: ReplicationMode::Paused,
            }
            .into(),
        )
        .await
        .unwrap();

        let info = components
            .replication_repo
            .read()
            .await
            .unwrap()
            .get_info("test")
            .await
            .unwrap();
        assert_eq!(info.info.mode, ReplicationMode::Paused);
    }
}