vaultrs/
database.rs

1pub mod connection {
2    use crate::api;
3    use crate::api::database::{
4        requests::{
5            DeleteConnectionRequest, ListConnectionsRequest, PostgreSQLConnectionRequest,
6            PostgreSQLConnectionRequestBuilder, ReadConnectionRequest, ResetConnectionRequest,
7            RotateRootRequest,
8        },
9        responses::{ListConnectionsResponse, ReadConnectionResponse},
10    };
11    use crate::client::Client;
12    use crate::error::ClientError;
13
14    /// Creates or updates a PostgreSQL connection
15    ///
16    /// See [PostgreSQLConnectionRequest]
17    pub async fn postgres(
18        client: &impl Client,
19        mount: &str,
20        name: &str,
21        opts: Option<&mut PostgreSQLConnectionRequestBuilder>,
22    ) -> Result<(), ClientError> {
23        let mut t = PostgreSQLConnectionRequest::builder();
24        let endpoint = opts
25            .unwrap_or(&mut t)
26            .mount(mount)
27            .name(name)
28            .build()
29            .unwrap();
30        api::exec_with_empty(client, endpoint).await
31    }
32
33    /// Deletes a connection
34    ///
35    /// See [DeleteConnectionRequest]
36    pub async fn delete(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
37        let endpoint = DeleteConnectionRequest::builder()
38            .mount(mount)
39            .name(name)
40            .build()
41            .unwrap();
42        api::exec_with_empty(client, endpoint).await
43    }
44
45    /// Lists all connections
46    ///
47    /// See [ListConnectionsRequest]
48    pub async fn list(
49        client: &impl Client,
50        mount: &str,
51    ) -> Result<ListConnectionsResponse, ClientError> {
52        let endpoint = ListConnectionsRequest::builder()
53            .mount(mount)
54            .build()
55            .unwrap();
56        api::exec_with_result(client, endpoint).await
57    }
58
59    /// Reads a connection
60    ///
61    /// See [ReadConnectionRequest]
62    pub async fn read(
63        client: &impl Client,
64        mount: &str,
65        name: &str,
66    ) -> Result<ReadConnectionResponse, ClientError> {
67        let endpoint = ReadConnectionRequest::builder()
68            .mount(mount)
69            .name(name)
70            .build()
71            .unwrap();
72        api::exec_with_result(client, endpoint).await
73    }
74
75    /// Reset a connection
76    ///
77    /// See [ResetConnectionRequest]
78    pub async fn reset(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
79        let endpoint = ResetConnectionRequest::builder()
80            .mount(mount)
81            .name(name)
82            .build()
83            .unwrap();
84        api::exec_with_empty(client, endpoint).await
85    }
86
87    /// Rotates the root account configured in a connection
88    ///
89    /// See [RotateRootRequest]
90    pub async fn rotate(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
91        let endpoint = RotateRootRequest::builder()
92            .mount(mount)
93            .name(name)
94            .build()
95            .unwrap();
96        api::exec_with_empty(client, endpoint).await
97    }
98}
99
100pub mod role {
101    use crate::api;
102    use crate::api::database::{
103        requests::{
104            DeleteRoleRequest, GenerateCredentialsRequest, ListRolesRequest, ReadRoleRequest,
105            SetRoleRequest, SetRoleRequestBuilder,
106        },
107        responses::{GenerateCredentialsResponse, ListRolesResponse, ReadRoleResponse},
108    };
109    use crate::client::Client;
110    use crate::error::ClientError;
111
112    /// Generates credentials from a role
113    ///
114    /// See [GenerateCredentialsRequest]
115    pub async fn creds(
116        client: &impl Client,
117        mount: &str,
118        name: &str,
119    ) -> Result<GenerateCredentialsResponse, ClientError> {
120        let endpoint = GenerateCredentialsRequest::builder()
121            .mount(mount)
122            .name(name)
123            .build()
124            .unwrap();
125        api::exec_with_result(client, endpoint).await
126    }
127
128    /// Deletes a role
129    ///
130    /// See [DeleteRoleRequest]
131    pub async fn delete(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
132        let endpoint = DeleteRoleRequest::builder()
133            .mount(mount)
134            .name(name)
135            .build()
136            .unwrap();
137        api::exec_with_empty(client, endpoint).await
138    }
139
140    /// Lists all roles
141    ///
142    /// See [ListRolesRequest]
143    pub async fn list(client: &impl Client, mount: &str) -> Result<ListRolesResponse, ClientError> {
144        let endpoint = ListRolesRequest::builder().mount(mount).build().unwrap();
145        api::exec_with_result(client, endpoint).await
146    }
147
148    /// Reads a role
149    ///
150    /// See [ReadRoleRequest]
151    pub async fn read(
152        client: &impl Client,
153        mount: &str,
154        name: &str,
155    ) -> Result<ReadRoleResponse, ClientError> {
156        let endpoint = ReadRoleRequest::builder()
157            .mount(mount)
158            .name(name)
159            .build()
160            .unwrap();
161        api::exec_with_result(client, endpoint).await
162    }
163
164    /// Creates or updates a role
165    ///
166    /// See [SetRoleRequest]
167    pub async fn set(
168        client: &impl Client,
169        mount: &str,
170        name: &str,
171        opts: Option<&mut SetRoleRequestBuilder>,
172    ) -> Result<(), ClientError> {
173        let mut t = SetRoleRequest::builder();
174        let endpoint = opts
175            .unwrap_or(&mut t)
176            .mount(mount)
177            .name(name)
178            .build()
179            .unwrap();
180        api::exec_with_empty(client, endpoint).await
181    }
182}
183
184pub mod static_role {
185    use crate::api;
186    use crate::api::database::{
187        requests::{
188            DeleteStaticRoleRequest, GetStaticCredentialsRequest, ListStaticRolesRequest,
189            ReadStaticRoleRequest, RotateStaticRoleRequest, SetStaticRoleRequest,
190            SetStaticRoleRequestBuilder,
191        },
192        responses::{
193            GetStaticCredentialsResponse, ListStaticRolesResponse, ReadStaticRoleResponse,
194        },
195    };
196    use crate::client::Client;
197    use crate::error::ClientError;
198
199    /// Generates credentials from a static role
200    ///
201    /// See [GetStaticCredentialsRequest]
202    pub async fn creds(
203        client: &impl Client,
204        mount: &str,
205        name: &str,
206    ) -> Result<GetStaticCredentialsResponse, ClientError> {
207        let endpoint = GetStaticCredentialsRequest::builder()
208            .mount(mount)
209            .name(name)
210            .build()
211            .unwrap();
212        api::exec_with_result(client, endpoint).await
213    }
214
215    /// Deletes a static role
216    ///
217    /// See [DeleteStaticRoleRequest]
218    pub async fn delete(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
219        let endpoint = DeleteStaticRoleRequest::builder()
220            .mount(mount)
221            .name(name)
222            .build()
223            .unwrap();
224        api::exec_with_empty(client, endpoint).await
225    }
226
227    /// Lists all static roles
228    ///
229    /// See [ListStaticRolesRequest]
230    pub async fn list(
231        client: &impl Client,
232        mount: &str,
233    ) -> Result<ListStaticRolesResponse, ClientError> {
234        let endpoint = ListStaticRolesRequest::builder()
235            .mount(mount)
236            .build()
237            .unwrap();
238        api::exec_with_result(client, endpoint).await
239    }
240
241    /// Reads a static role
242    ///
243    /// See [ReadStaticRoleRequest]
244    pub async fn read(
245        client: &impl Client,
246        mount: &str,
247        name: &str,
248    ) -> Result<ReadStaticRoleResponse, ClientError> {
249        let endpoint = ReadStaticRoleRequest::builder()
250            .mount(mount)
251            .name(name)
252            .build()
253            .unwrap();
254        api::exec_with_result(client, endpoint).await
255    }
256
257    /// Rotates the credentials associated with a static role
258    ///
259    /// See [RotateStaticRoleRequest]
260    pub async fn rotate(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
261        let endpoint = RotateStaticRoleRequest::builder()
262            .mount(mount)
263            .name(name)
264            .build()
265            .unwrap();
266        api::exec_with_empty(client, endpoint).await
267    }
268
269    /// Creates or updates a static role
270    ///
271    /// See [SetStaticRoleRequest]
272    pub async fn set(
273        client: &impl Client,
274        mount: &str,
275        name: &str,
276        opts: Option<&mut SetStaticRoleRequestBuilder>,
277    ) -> Result<(), ClientError> {
278        let mut t = SetStaticRoleRequest::builder();
279        let endpoint = opts
280            .unwrap_or(&mut t)
281            .mount(mount)
282            .name(name)
283            .build()
284            .unwrap();
285        api::exec_with_empty(client, endpoint).await
286    }
287}