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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use super::Session;
use crate::session::session::SessionError;
use crate::session::RUNTIME;
use antimatter_api::apis::policy_api::{self as api};
use antimatter_api::models::{
DomainFactList, Fact, FactList, FactTuple, FactTypeDefinition, NewFact, NewFactTypeDefinition,
};
impl Session {
/// Fetches the list of fact types in the session's domain.
///
/// # Returns
///
/// A `Result` containing a `DomainFactList` with a vector of fact types.
pub fn list_fact_types(&mut self) -> Result<DomainFactList, SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_list_fact_types(
&conf,
self.get_domain_id().as_str(),
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
/// Fetches the list of facts in the session's domain for the given fact
/// type.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
///
/// # Returns
///
/// A `Result` containing a `FactList` with a vector of facts of the given
/// fact type.
pub fn list_facts(&mut self, fact_type: &str) -> Result<FactList, SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_list_facts(
&conf,
self.get_domain_id().as_str(),
fact_type,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
/// Creates a new fact type for the session's domain.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
/// * `arguments` - A NewFactTypeDefinition containing the new fact type's
/// configuration.
pub fn add_fact_type(
&mut self,
fact_type: &str,
arguments: NewFactTypeDefinition,
) -> Result<(), SessionError> {
let conf = self.get_configuration()?;
RUNTIME
.block_on(api::domain_put_fact_type(
&conf,
self.get_domain_id().as_str(),
fact_type,
arguments,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(())
}
/// Creates a new fact of fact type for the session's domain.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
/// * `arguments` - A NewFact containing the new fact's configuration.
pub fn add_fact(&mut self, fact_type: &str, arguments: NewFact) -> Result<Fact, SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_upsert_fact(
&conf,
self.get_domain_id().as_str(),
fact_type,
arguments,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
/// Fetches the fact type's information in the session's domain.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
///
/// Returns:
///
/// A `Result` containing a `FactTypeDefinition` with information about the
/// fact type.
pub fn get_fact_type(&mut self, fact_type: &str) -> Result<FactTypeDefinition, SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_get_fact_type(
&conf,
self.get_domain_id().as_str(),
fact_type,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
/// Fetches the fact of fact type's information in the session's domain.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
/// * `fact_id` - A &str containing the fact.
///
/// Returns:
///
/// A `Result` containing a `Fact` with information about the fact.
pub fn get_fact(&mut self, fact_type: &str, fact_id: &str) -> Result<Fact, SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_get_fact_by_id(
&conf,
self.get_domain_id().as_str(),
fact_type,
fact_id,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
/// Delete the fact type from the session's domain.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
/// * `confirm` - A &str containing the confirmation string (this is the
/// fact type).
pub fn delete_fact_type(&mut self, fact_type: &str, confirm: &str) -> Result<(), SessionError> {
let conf = self.get_configuration()?;
RUNTIME
.block_on(api::domain_delete_fact_type(
&conf,
self.get_domain_id().as_str(),
fact_type,
confirm,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(())
}
/// Delete the fact of fact type from the session's domain. This is
/// performed either by the fact's ID (single), or by matching fact
/// arguments (many).
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
/// * `arguments` - A Option<Vec<String>> containing the arguments to match
/// a fact on.
/// * `fact_id` - A Option<&str> containing the fact ID.
pub fn delete_fact(
&mut self,
fact_type: &str,
arguments: Option<Vec<String>>,
fact_id: Option<&str>,
) -> Result<(), SessionError> {
let conf = self.get_configuration()?;
if fact_id.is_none() && arguments.is_some() {
self.delete_fact_by_tuple(
fact_type,
FactTuple {
arguments: arguments.unwrap(),
},
)
.map_err(|e| SessionError::APIError(format!("{}", e)))
} else {
RUNTIME
.block_on(api::domain_delete_fact_by_id(
&conf,
self.get_domain_id().as_str(),
fact_type,
fact_id.unwrap_or_default(),
))
.map_err(|e| SessionError::APIError(format!("{}", e)))
}
}
/// Delete all fact of fact type from the session's domain.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
pub fn delete_all_facts(&mut self, fact_type: &str) -> Result<(), SessionError> {
let conf = self.get_configuration()?;
let fact_list = self
.list_facts(fact_type)
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
for matching_fact in fact_list.facts.iter() {
let _ = RUNTIME
.block_on(api::domain_delete_fact_by_id(
&conf,
self.get_domain_id().as_str(),
fact_type,
matching_fact.id.as_str(),
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
}
Ok(())
}
/// Delete a fact of fact type from the session's domain that matches the supplied tuple.
///
/// Arguments:
///
/// * `fact_type` - A &str containing the fact type.
/// * `fact_tuple` - A list of &str containing the fact arguments.
pub fn delete_fact_by_tuple(
&mut self,
fact_type: &str,
fact_tuple: FactTuple,
) -> Result<(), SessionError> {
let conf = self.get_configuration()?;
let res = RUNTIME
.block_on(api::domain_delete_fact_by_tuple(
&conf,
self.get_domain_id().as_str(),
fact_type,
fact_tuple,
))
.map_err(|e| SessionError::APIError(format!("{}", e)))?;
Ok(res)
}
}