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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use super::Session;
use crate::session::session::SessionError;
use crate::session::RUNTIME;
use antimatter_api::apis::capsules_api::{self as api};
use antimatter_api::apis::internal_api;
use antimatter_api::apis::Error::ResponseError;
use antimatter_api::models::{
CapsuleInfo, CapsuleList, CapsuleOpenRequest, CapsuleOpenResponse, DeleteTags,
DomainUpsertCapsuleTagsRequest, Tag,
};
impl Session {
/// Lists the capsules for the session's domain, allowing for optional
/// filtering.
///
/// # Arguments
///
/// * `start_date` - An Option<String> to retrieve capsule after
/// (format "YYYY-MM-DD HH:MM:SS").
/// * `end_date` - An Option<String> to retrieve capsule before
/// (format "YYYY-MM-DD HH:MM:SS").
/// * `num_results` - An Option<i32> indicating the number of capsules to
/// retrieve.
/// * `span_tags` - An Option<&str> to filter by span tag name.
/// * `sort_on` - An Option<&str> to sort results on ("created", "size").
/// * `start_after` - An Option<&str> page key to get results after/before.
/// * `ascending` - An Option<bool> indicates result order (false=descending).
///
/// # Returns
///
/// A `Result` containing a `CapsuleList` with a vector of `CapsuleInfo`.
pub fn list_capsules(
&mut self,
start_date: Option<String>,
end_date: Option<String>,
num_results: Option<i32>,
span_tags: Option<&str>,
sort_on: Option<&str>,
start_after: Option<&str>,
ascending: Option<bool>,
) -> Result<CapsuleList, SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_list_capsules(
&conf,
self.get_domain_id().as_str(),
start_date,
end_date,
num_results,
span_tags,
sort_on,
start_after,
ascending,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
/// Gets details for a capsule in the session's domain.
///
/// # Arguments
///
/// * `capsule_id` - A &str containing the capsule's ID.
///
/// # Returns
///
/// A `Result` containing a `CapsuleInfo` detailing the capsule.
pub fn get_capsule_info(&mut self, capsule_id: &str) -> Result<CapsuleInfo, SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_get_capsule_info(
&conf,
self.get_domain_id().as_str(),
capsule_id,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
/// Upsert a capsule's capsule tags with the provided vector of tags.
///
/// # Arguments
///
/// * `capsule_id` - A &str containing the capsule's ID.
/// * `tags` - A Vec<Tag> containing capsule tags.
pub fn upsert_capsule_tags(
&mut self,
capsule_id: &str,
tags: Vec<Tag>,
) -> Result<(), SessionError> {
let conf = self.get_configuration()?;
RUNTIME
.block_on(api::domain_upsert_capsule_tags(
&conf,
self.get_domain_id().as_str(),
capsule_id,
DomainUpsertCapsuleTagsRequest { tags: Some(tags) },
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(())
}
/// Deletes capsule tags from a capsule's capsule tags.
///
/// # Arguments
///
/// * `capsule_id` - A &str containing the capsule's ID.
/// * `tags` - A DeleteTags containing capsule tags to delete.
pub fn delete_capsule_tags(
&mut self,
capsule_id: &str,
tags: DeleteTags,
) -> Result<(), SessionError> {
let conf = self.get_configuration()?;
let _ = RUNTIME
.block_on(api::domain_delete_capsule_tags(
&conf,
self.get_domain_id().as_str(),
capsule_id,
tags,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(())
}
/// Given a capsule ID, read context and encrypted DEK, open the capsule and return the decrypted DEK
///
/// # Arguments
///
/// * `capsule_id` - A &str containing the capsule's ID.
/// * `read_context` - A &str containing the read context to use when opening the capsule.
/// * `domain_id`. - An optional &str. If provided, will be used in the request. If not, the
/// default domain will be used instead.
/// * `req` - An open capsule request containing the encrypted REK, its key ID and any
/// optional read parameters.
pub fn open_capsule(
&mut self,
capsule_id: &str,
read_context: &str,
domain_id: Option<&str>,
req: CapsuleOpenRequest,
) -> Result<CapsuleOpenResponse, SessionError> {
let conf = self.get_configuration()?;
RUNTIME
.block_on(internal_api::domain_open_capsule(
&conf,
domain_id.unwrap_or(self.get_domain_id().as_str()),
capsule_id,
read_context,
req,
))
.map_err(|e| match e {
ResponseError(e) => match e.status {
reqwest::StatusCode::UNAUTHORIZED => {
SessionError::Status401(format!(" {}", e.content))
}
code => SessionError::APIError(format!(
"open request failed ({}): {}",
code, e.content
)),
},
e => SessionError::APIError(format!("unknown error opening capsule: {}", e)),
})
}
}