1use reqwest::Method;
2
3use crate::VaultClient;
4use crate::api::traits::DatabaseOperations;
5use crate::client::{encode_path, to_body};
6use crate::types::database::*;
7use crate::types::error::VaultError;
8
9#[derive(Debug)]
10pub struct DatabaseHandler<'a> {
11 pub(crate) client: &'a VaultClient,
12 pub(crate) mount: String,
13}
14
15impl DatabaseOperations for DatabaseHandler<'_> {
16 async fn configure(
17 &self,
18 name: &str,
19 params: &DatabaseConfigRequest,
20 ) -> Result<(), VaultError> {
21 let body = to_body(params)?;
22 self.client
23 .exec_empty(
24 Method::POST,
25 &format!("{}/config/{}", self.mount, encode_path(name)),
26 Some(&body),
27 )
28 .await
29 }
30
31 async fn read_config(&self, name: &str) -> Result<DatabaseConfig, VaultError> {
32 self.client
33 .exec_with_data(
34 Method::GET,
35 &format!("{}/config/{}", self.mount, encode_path(name)),
36 None,
37 )
38 .await
39 }
40
41 async fn delete_config(&self, name: &str) -> Result<(), VaultError> {
42 self.client
43 .exec_empty(
44 Method::DELETE,
45 &format!("{}/config/{}", self.mount, encode_path(name)),
46 None,
47 )
48 .await
49 }
50
51 async fn list_connections(&self) -> Result<Vec<String>, VaultError> {
52 self.client
53 .exec_list(&format!("{}/config", self.mount))
54 .await
55 }
56
57 async fn reset_connection(&self, name: &str) -> Result<(), VaultError> {
58 self.client
59 .exec_empty(
60 Method::POST,
61 &format!("{}/reset/{}", self.mount, encode_path(name)),
62 None,
63 )
64 .await
65 }
66
67 async fn create_role(
68 &self,
69 name: &str,
70 params: &DatabaseRoleRequest,
71 ) -> Result<(), VaultError> {
72 let body = to_body(params)?;
73 self.client
74 .exec_empty(
75 Method::POST,
76 &format!("{}/roles/{}", self.mount, encode_path(name)),
77 Some(&body),
78 )
79 .await
80 }
81
82 async fn read_role(&self, name: &str) -> Result<DatabaseRole, VaultError> {
83 self.client
84 .exec_with_data(
85 Method::GET,
86 &format!("{}/roles/{}", self.mount, encode_path(name)),
87 None,
88 )
89 .await
90 }
91
92 async fn delete_role(&self, name: &str) -> Result<(), VaultError> {
93 self.client
94 .exec_empty(
95 Method::DELETE,
96 &format!("{}/roles/{}", self.mount, encode_path(name)),
97 None,
98 )
99 .await
100 }
101
102 async fn list_roles(&self) -> Result<Vec<String>, VaultError> {
103 self.client
104 .exec_list(&format!("{}/roles", self.mount))
105 .await
106 }
107
108 async fn get_credentials(&self, role: &str) -> Result<DatabaseCredentials, VaultError> {
109 self.client
110 .exec_with_data(
111 Method::GET,
112 &format!("{}/creds/{}", self.mount, encode_path(role)),
113 None,
114 )
115 .await
116 }
117
118 async fn create_static_role(
119 &self,
120 name: &str,
121 params: &DatabaseStaticRoleRequest,
122 ) -> Result<(), VaultError> {
123 let body = to_body(params)?;
124 self.client
125 .exec_empty(
126 Method::POST,
127 &format!("{}/static-roles/{}", self.mount, encode_path(name)),
128 Some(&body),
129 )
130 .await
131 }
132
133 async fn read_static_role(&self, name: &str) -> Result<DatabaseStaticRole, VaultError> {
134 self.client
135 .exec_with_data(
136 Method::GET,
137 &format!("{}/static-roles/{}", self.mount, encode_path(name)),
138 None,
139 )
140 .await
141 }
142
143 async fn delete_static_role(&self, name: &str) -> Result<(), VaultError> {
144 self.client
145 .exec_empty(
146 Method::DELETE,
147 &format!("{}/static-roles/{}", self.mount, encode_path(name)),
148 None,
149 )
150 .await
151 }
152
153 async fn list_static_roles(&self) -> Result<Vec<String>, VaultError> {
154 self.client
155 .exec_list(&format!("{}/static-roles", self.mount))
156 .await
157 }
158
159 async fn get_static_credentials(
160 &self,
161 name: &str,
162 ) -> Result<DatabaseStaticCredentials, VaultError> {
163 self.client
164 .exec_with_data(
165 Method::GET,
166 &format!("{}/static-creds/{}", self.mount, encode_path(name)),
167 None,
168 )
169 .await
170 }
171
172 async fn rotate_static_role(&self, name: &str) -> Result<(), VaultError> {
173 self.client
174 .exec_empty(
175 Method::POST,
176 &format!("{}/rotate-role/{}", self.mount, encode_path(name)),
177 None,
178 )
179 .await
180 }
181}