vaultrs/identity/
entity.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::fmt;

use crate::{
    api::{
        self,
        identity::entity::{
            requests::{
                BatchDeleteRequest, CreateEntityByNameRequest, CreateEntityByNameRequestBuilder,
                CreateEntityRequest, CreateEntityRequestBuilder, DeleteEntityByIdRequest,
                DeleteEntityByNameRequest, ListEntitiesByIdRequest, ListEntitiesByNameRequest,
                MergeEntitiesRequest, MergeEntitiesRequestBuilder, ReadEntityByIdRequest,
                ReadEntityByNameRequest, UpdateEntityByIdRequest, UpdateEntityByIdRequestBuilder,
            },
            responses::{
                CreateEntityResponse, ListEntitiesByIdResponse, ListEntitiesByNameResponse,
                ReadEntityByIdResponse, ReadEntityByNameResponse,
            },
        },
    },
    client::Client,
    error::ClientError,
};

/// Create an entity.
///
/// See [CreateEntityRequest]
pub async fn create(
    client: &impl Client,
    opts: Option<&mut CreateEntityRequestBuilder>,
) -> Result<CreateEntityResponse, ClientError> {
    let mut t = CreateEntityRequest::builder();
    let endpoint = opts.unwrap_or(&mut t).build().unwrap();
    api::exec_with_result(client, endpoint).await
}

/// Reads entity by `id`.
///
/// See [ReadEntityByIdRequest]
pub async fn read_by_id(
    client: &impl Client,
    id: &str,
) -> Result<ReadEntityByIdResponse, ClientError> {
    let endpoint = ReadEntityByIdRequest::builder().id(id).build().unwrap();

    api::exec_with_result(client, endpoint).await
}

/// Update entity by `id`.
///
/// See [UpdateEntityByIdRequest]
pub async fn update_by_id(
    client: &impl Client,
    id: &str,
    opts: Option<&mut UpdateEntityByIdRequestBuilder>,
) -> Result<(), ClientError> {
    let mut t = UpdateEntityByIdRequest::builder();
    let endpoint = opts.unwrap_or(&mut t).id(id).build().unwrap();
    api::exec_with_empty(client, endpoint).await
}

/// Delete entity by `id`.
///
/// See [DeleteEntityByIdRequest]
pub async fn delete_by_id(client: &impl Client, id: &str) -> Result<(), ClientError> {
    let endpoint = DeleteEntityByIdRequest::builder().id(id).build().unwrap();
    api::exec_with_empty(client, endpoint).await
}

/// Batch delete entity.
///
/// See [BatchDeleteRequest]
pub async fn batch_delete<T: fmt::Debug + Into<Vec<String>>>(
    client: &impl Client,
    entity_ids: T,
) -> Result<(), ClientError> {
    let endpoint = BatchDeleteRequest::builder()
        .entity_ids(entity_ids)
        .build()
        .unwrap();
    api::exec_with_empty(client, endpoint).await
}

/// List entities by ID.
///
/// See [ListEntitiesByIdRequest]
pub async fn list_by_id(client: &impl Client) -> Result<ListEntitiesByIdResponse, ClientError> {
    let endpoint = ListEntitiesByIdRequest::builder().build().unwrap();
    api::exec_with_result(client, endpoint).await
}

/// Creates or update an entity with the given `name`.
///
/// See [CreateEntityByNameRequest]
pub async fn create_or_update_by_name(
    client: &impl Client,
    name: &str,
    opts: Option<&mut CreateEntityByNameRequestBuilder>,
) -> Result<(), ClientError> {
    let mut t = CreateEntityByNameRequest::builder();
    let endpoint = opts.unwrap_or(&mut t).name(name).build().unwrap();
    api::exec_with_empty(client, endpoint).await
}

/// Reads entity by `name`.
///
/// See [ReadEntityByNameRequest]
pub async fn read_by_name(
    client: &impl Client,
    name: &str,
) -> Result<ReadEntityByNameResponse, ClientError> {
    let endpoint = ReadEntityByNameRequest::builder()
        .name(name)
        .build()
        .unwrap();

    api::exec_with_result(client, endpoint).await
}

/// Delete entity by `name`.
///
/// See [DeleteEntityByIdRequest]
pub async fn delete_by_name(client: &impl Client, name: &str) -> Result<(), ClientError> {
    let endpoint = DeleteEntityByNameRequest::builder()
        .name(name)
        .build()
        .unwrap();
    api::exec_with_empty(client, endpoint).await
}

/// List entities by Name.
///
/// See [ListEntitiesByNameRequest]
pub async fn list_by_name(client: &impl Client) -> Result<ListEntitiesByNameResponse, ClientError> {
    let endpoint = ListEntitiesByNameRequest::builder().build().unwrap();
    api::exec_with_result(client, endpoint).await
}

/// Merge entities.
///
/// See [MergeEntitiesRequest]
pub async fn merge(
    client: &impl Client,
    from_entity_ids: Vec<String>,
    to_entity_id: String,
    opts: Option<&mut MergeEntitiesRequestBuilder>,
) -> Result<(), ClientError> {
    let mut t = MergeEntitiesRequest::builder();
    let endpoint = opts
        .unwrap_or(&mut t)
        .from_entity_ids(from_entity_ids)
        .to_entity_id(to_entity_id)
        .build()
        .unwrap();
    api::exec_with_empty(client, endpoint).await
}