Skip to main content

vault_client_rs/api/
consul.rs

1use reqwest::Method;
2
3use crate::VaultClient;
4use crate::api::traits::ConsulOperations;
5use crate::client::{encode_path, to_body};
6use crate::types::consul::*;
7use crate::types::error::VaultError;
8
9#[derive(Debug)]
10pub struct ConsulHandler<'a> {
11    pub(crate) client: &'a VaultClient,
12    pub(crate) mount: String,
13}
14
15impl ConsulOperations for ConsulHandler<'_> {
16    async fn configure(&self, params: &ConsulConfigRequest) -> Result<(), VaultError> {
17        let body = to_body(params)?;
18        self.client
19            .exec_empty(
20                Method::POST,
21                &format!("{}/config/access", self.mount),
22                Some(&body),
23            )
24            .await
25    }
26
27    async fn read_config(&self) -> Result<ConsulConfig, VaultError> {
28        self.client
29            .exec_with_data(Method::GET, &format!("{}/config/access", self.mount), None)
30            .await
31    }
32
33    async fn delete_config(&self) -> Result<(), VaultError> {
34        self.client
35            .exec_empty(
36                Method::DELETE,
37                &format!("{}/config/access", self.mount),
38                None,
39            )
40            .await
41    }
42
43    async fn create_role(&self, name: &str, params: &ConsulRoleRequest) -> Result<(), VaultError> {
44        let body = to_body(params)?;
45        self.client
46            .exec_empty(
47                Method::POST,
48                &format!("{}/roles/{}", self.mount, encode_path(name)),
49                Some(&body),
50            )
51            .await
52    }
53
54    async fn read_role(&self, name: &str) -> Result<ConsulRole, VaultError> {
55        self.client
56            .exec_with_data(
57                Method::GET,
58                &format!("{}/roles/{}", self.mount, encode_path(name)),
59                None,
60            )
61            .await
62    }
63
64    async fn delete_role(&self, name: &str) -> Result<(), VaultError> {
65        self.client
66            .exec_empty(
67                Method::DELETE,
68                &format!("{}/roles/{}", self.mount, encode_path(name)),
69                None,
70            )
71            .await
72    }
73
74    async fn list_roles(&self) -> Result<Vec<String>, VaultError> {
75        self.client
76            .exec_list(&format!("{}/roles", self.mount))
77            .await
78    }
79
80    async fn get_credentials(&self, role: &str) -> Result<ConsulCredentials, VaultError> {
81        self.client
82            .exec_with_data(
83                Method::GET,
84                &format!("{}/creds/{}", self.mount, encode_path(role)),
85                None,
86            )
87            .await
88    }
89}