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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use std::collections::HashMap;
use reqwest::Response;
use serde::{Deserialize, Serialize};
use super::SplunkClient;
#[derive(Debug)]
pub enum SearchJobBuilderError {
CreateFailed {
message: String,
},
}
#[derive(Debug, Clone)]
pub enum SearchExecMode {
Blocking,
OneShot,
Normal,
}
impl ToString for SearchExecMode {
fn to_string(&self) -> String {
match self {
SearchExecMode::Blocking => "blocking",
SearchExecMode::OneShot => "oneshot",
SearchExecMode::Normal => "normal",
}
.to_string()
}
}
#[derive(Clone, Debug)]
pub enum AdHocSearchLevel {
Fast,
Smart,
Verbose,
}
impl ToString for AdHocSearchLevel {
fn to_string(&self) -> String {
match self {
AdHocSearchLevel::Verbose => "verbose".to_string(),
AdHocSearchLevel::Fast => "fast".to_string(),
AdHocSearchLevel::Smart => "smart".to_string(),
}
}
}
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Clone, Debug)]
pub enum SearchOutputMode {
Atom,
Csv,
Json,
JsonCols,
JsonRows,
Raw,
Xml,
}
impl ToString for SearchOutputMode {
fn to_string(&self) -> String {
match self {
SearchOutputMode::Json => "json",
SearchOutputMode::Atom => "atom",
SearchOutputMode::Csv => "csv",
SearchOutputMode::JsonCols => "json_cols",
SearchOutputMode::JsonRows => "json_rows",
SearchOutputMode::Raw => "raw",
SearchOutputMode::Xml => "xml",
}
.to_string()
}
}
#[derive(Clone, Debug)]
pub struct SearchJobBuilder {
query: String,
count: Option<u64>,
earliest_time: String,
latest_time: String,
fields: Vec<String>,
adhoc_search_level: AdHocSearchLevel,
allow_partial_results: bool,
auto_cancel: u32,
auto_finalize_ec: u32,
auto_pause: u32,
output_mode: SearchOutputMode,
custom: Option<String>,
enable_lookups: bool,
exec_mode: SearchExecMode,
force_bundle_replication: bool,
id: Option<String>,
extra_options: HashMap<String, String>,
timeout: u32,
}
impl Default for SearchJobBuilder {
fn default() -> Self {
let default_extra_options: HashMap<String, String> = HashMap::new();
SearchJobBuilder {
query: "".to_string(),
count: Some(10000),
earliest_time: "-24h".to_string(),
latest_time: "now".to_string(),
fields: vec![],
adhoc_search_level: AdHocSearchLevel::Fast,
allow_partial_results: true,
auto_cancel: 0,
auto_finalize_ec: 0,
auto_pause: 0,
output_mode: SearchOutputMode::Json,
custom: None,
enable_lookups: true,
exec_mode: SearchExecMode::Normal,
force_bundle_replication: false,
id: None,
extra_options: default_extra_options,
timeout: 86400,
}
}
}
#[derive(Deserialize)]
pub struct XMLResponseWithSid {
#[allow(missing_docs)]
pub response: XMLResponseSid,
}
#[derive(Deserialize)]
pub struct XMLResponseSid {
#[allow(missing_docs)]
pub sid: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SearchResult {
preview: Option<bool>,
offset: usize,
lastrow: Option<bool>,
result: serde_json::Value,
}
impl SearchJobBuilder {
pub async fn create(
self,
client: &mut SplunkClient,
) -> Result<SearchJob, SearchJobBuilderError> {
let endpoint = "/services/search/jobs/export";
let mut payload: HashMap<&str, String> = HashMap::new();
self.extra_options.iter().for_each(|(key, value)| {
payload.insert(key.as_str(), value.to_owned());
});
payload.insert("adhoc_search_level", self.adhoc_search_level.to_string());
payload.insert(
"allow_partial_results",
self.allow_partial_results.to_string().to_ascii_lowercase(),
);
payload.insert("output_mode", self.output_mode.to_string());
payload.insert("auto_cancel", format!("{}", self.auto_cancel));
payload.insert("auto_finalize_ec", format!("{}", self.auto_finalize_ec));
payload.insert("auto_pause", format!("{}", self.auto_pause));
if let Some(custom) = self.custom {
payload.insert("custom", custom);
}
payload.insert("earliest_time", self.earliest_time.clone());
payload.insert("latest_time", self.latest_time.clone());
payload.insert("timeout", self.timeout.to_string());
payload.insert(
"enable_lookups",
self.enable_lookups.to_string().to_ascii_lowercase(),
);
payload.insert("exec_mode", self.exec_mode.to_string());
payload.insert(
"force_bundle_replication",
self.force_bundle_replication
.to_string()
.to_ascii_lowercase(),
);
if let Some(id) = self.id {
payload.insert("id", id);
}
payload.insert("search", self.query.clone());
let result = match client.do_post(endpoint, payload).await {
Err(err) => return Err(SearchJobBuilderError::CreateFailed { message: err }),
Ok(val) => val,
};
Ok(SearchJob {
query: self.query,
count: self.count.unwrap(),
earliest_time: self.earliest_time,
latest_time: self.latest_time,
fields: self.fields,
exec_mode: self.exec_mode,
sid: None,
creation_response: result,
})
}
pub fn adhoc_search_level(self, adhoc_search_level: AdHocSearchLevel) -> Self {
Self {
adhoc_search_level,
..self
}
}
pub fn mode(self, exec_mode: SearchExecMode) -> Self {
Self { exec_mode, ..self }
}
}
pub struct SearchJob {
pub query: String,
pub count: u64,
pub exec_mode: SearchExecMode,
pub earliest_time: String,
pub latest_time: String,
pub fields: Vec<String>,
pub sid: Option<String>,
pub creation_response: Response,
}
#[allow(unused_macros)]
macro_rules! get_lines {
($stream:expr) => {
StreamReader::new($stream.map_err(convert_err)).lines()
};
}
impl SearchJob {
pub fn create(query: impl Into<String>) -> SearchJobBuilder {
SearchJobBuilder {
query: query.into(),
..Default::default()
}
}
}