vaultrs/identity/
group.rs

1use crate::{
2    api::{
3        self,
4        identity::group::{
5            requests::{
6                CreateGroupByNameRequest, CreateGroupByNameRequestBuilder, CreateGroupRequest,
7                CreateGroupRequestBuilder, DeleteGroupByIdRequest, DeleteGroupByNameRequest,
8                ListGroupsByIdRequest, ListGroupsByNameRequest, ReadGroupByIdRequest,
9                ReadGroupByNameRequest, UpdateGroupByIdRequest, UpdateGroupByIdRequestBuilder,
10            },
11            responses::{
12                CreateGroupResponse, ListGroupsByIdResponse, ListGroupsByNameResponse,
13                ReadGroupByIdResponse, ReadGroupByNameResponse,
14            },
15        },
16    },
17    client::Client,
18    error::ClientError,
19};
20
21/// Creates a group.
22///
23/// See [CreateGroupRequest]
24pub async fn create(
25    client: &impl Client,
26    opts: Option<&mut CreateGroupRequestBuilder>,
27) -> Result<CreateGroupResponse, ClientError> {
28    let mut t = CreateGroupRequest::builder();
29    let endpoint = opts.unwrap_or(&mut t).build().unwrap();
30    api::exec_with_result(client, endpoint).await
31}
32
33/// Reads group by `id`.
34///
35/// See [ReadGroupByIdRequest]
36pub async fn read_by_id(
37    client: &impl Client,
38    id: &str,
39) -> Result<ReadGroupByIdResponse, ClientError> {
40    let endpoint = ReadGroupByIdRequest::builder().id(id).build().unwrap();
41
42    api::exec_with_result(client, endpoint).await
43}
44
45/// Reads group by `name`.
46///
47/// See [ReadGroupByNameRequest]
48pub async fn read_by_name(
49    client: &impl Client,
50    name: &str,
51) -> Result<ReadGroupByNameResponse, ClientError> {
52    let endpoint = ReadGroupByNameRequest::builder()
53        .name(name)
54        .build()
55        .unwrap();
56
57    api::exec_with_result(client, endpoint).await
58}
59/// Update group by `id`.
60///
61/// See [UpdateGroupByIdRequest]
62pub async fn update_by_id(
63    client: &impl Client,
64    id: &str,
65    opts: Option<&mut UpdateGroupByIdRequestBuilder>,
66) -> Result<(), ClientError> {
67    let mut t = UpdateGroupByIdRequest::builder();
68    let endpoint = opts.unwrap_or(&mut t).id(id).build().unwrap();
69    api::exec_with_empty(client, endpoint).await
70}
71
72/// Delete group by `id`.
73///
74/// See [DeleteGroupByIdRequest]
75pub async fn delete_by_id(client: &impl Client, id: &str) -> Result<(), ClientError> {
76    let endpoint = DeleteGroupByIdRequest::builder().id(id).build().unwrap();
77    api::exec_with_empty(client, endpoint).await
78}
79
80/// List groups by ID.
81///
82/// See [ListGroupsByIdRequest]
83pub async fn list_by_id(client: &impl Client) -> Result<ListGroupsByIdResponse, ClientError> {
84    let endpoint = ListGroupsByIdRequest::builder().build().unwrap();
85    api::exec_with_result(client, endpoint).await
86}
87/// Creates or update an group with the given `name`.
88///
89/// See [CreateGroupByNameRequest]
90pub async fn create_or_update_by_name(
91    client: &impl Client,
92    name: &str,
93    opts: Option<&mut CreateGroupByNameRequestBuilder>,
94) -> Result<(), ClientError> {
95    let mut t = CreateGroupByNameRequest::builder();
96    let endpoint = opts.unwrap_or(&mut t).name(name).build().unwrap();
97    api::exec_with_empty(client, endpoint).await
98}
99
100/// Delete group by `name`.
101///
102/// See [DeleteGroupByIdRequest]
103pub async fn delete_by_name(client: &impl Client, name: &str) -> Result<(), ClientError> {
104    let endpoint = DeleteGroupByNameRequest::builder()
105        .name(name)
106        .build()
107        .unwrap();
108    api::exec_with_empty(client, endpoint).await
109}
110
111/// List entities by Name.
112///
113/// See [ListGroupsByNameRequest]
114pub async fn list_by_name(client: &impl Client) -> Result<ListGroupsByNameResponse, ClientError> {
115    let endpoint = ListGroupsByNameRequest::builder().build().unwrap();
116    api::exec_with_result(client, endpoint).await
117}