Skip to main content

vault_client_rs/api/sys/
raft.rs

1use reqwest::Method;
2
3use crate::types::error::VaultError;
4use crate::types::sys::{AutopilotState, RaftConfig};
5
6use super::SysHandler;
7
8impl SysHandler<'_> {
9    pub async fn raft_config(&self) -> Result<RaftConfig, VaultError> {
10        self.client
11            .exec_with_data(Method::GET, "sys/storage/raft/configuration", None)
12            .await
13    }
14
15    pub async fn raft_autopilot_state(&self) -> Result<AutopilotState, VaultError> {
16        self.client
17            .exec_with_data(Method::GET, "sys/storage/raft/autopilot/state", None)
18            .await
19    }
20
21    pub async fn raft_remove_peer(&self, server_id: &str) -> Result<(), VaultError> {
22        let body = serde_json::json!({ "server_id": server_id });
23        self.client
24            .exec_empty(Method::POST, "sys/storage/raft/remove-peer", Some(&body))
25            .await
26    }
27
28    /// Take a Raft snapshot, returning the raw snapshot bytes
29    pub async fn raft_snapshot(&self) -> Result<Vec<u8>, VaultError> {
30        let resp = self
31            .client
32            .execute(Method::GET, "sys/storage/raft/snapshot", None)
33            .await?;
34        Ok(resp.bytes().await.map_err(VaultError::Http)?.to_vec())
35    }
36
37    /// Restore a Raft snapshot from raw bytes
38    pub async fn raft_snapshot_restore(&self, snapshot: &[u8]) -> Result<(), VaultError> {
39        let url_str = format!("{}v1/sys/storage/raft/snapshot", self.client.inner.base_url);
40        let url = url::Url::parse(&url_str)?;
41        let req = self
42            .client
43            .inner
44            .http
45            .post(url)
46            .header("X-Vault-Request", "true")
47            .body(snapshot.to_vec());
48        let req = self.client.inject_headers(req)?;
49        let _resp: reqwest::Response = req.send().await.map_err(VaultError::Http)?;
50        Ok(())
51    }
52}