santati 0.3.0

Official Rust SDK for the Santati audit-log API
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! The schema operations: event definitions, their schema versions and the
//! standard packs.

use std::collections::VecDeque;
use std::future::Future;

use futures_util::stream::{self, Stream};
use reqwest::RequestBuilder;
use serde::de::DeserializeOwned;
use serde_json::{Map, Value};

use crate::client::Santati;
use crate::error::Error;
use crate::http::{cursor_from_url, encode_segment, send, RawResponse};
use crate::models;
use crate::retry::{run as run_with_retries, RetryPolicy};
use crate::types::{
    DefinitionInput, DefinitionPage, DefinitionUpdate, PageParams, SchemaVersionPage,
    SchemaVersionResult,
};

const DEFINITIONS_PATH: &str = "/api/v0/event-definitions/";
const STANDARD_PATH: &str = "/api/v0/standard-events/";

/// The client's schema operations, borrowed from a [`Santati`].
///
/// Every operation but [`Schemas::create_version`] retries like the events
/// operations; `create_version` is sent once, because a repeat would create a
/// second draft. An empty `action` fails with [`Error::Validation`] (field
/// `action`) before any request; everything else is forwarded as given and
/// the server decides.
#[derive(Clone, Copy)]
pub struct Schemas<'a> {
    client: &'a Santati,
}

impl<'a> Schemas<'a> {
    pub(crate) fn new(client: &'a Santati) -> Schemas<'a> {
        Schemas { client }
    }

    /// Fetch one page of the team's event definitions.
    pub async fn list_definitions(&self, params: PageParams) -> Result<DefinitionPage, Error> {
        let query = page_query(&params);
        let page: models::PaginatedEventDefinitionList = self
            .call(
                true,
                200,
                || self.get(DEFINITIONS_PATH.to_string(), &query),
                decode,
            )
            .await?;
        Ok(DefinitionPage {
            results: page.results,
            next_cursor: next_cursor(page.next),
        })
    }

    /// Walk every page of event definitions: a lazy stream that starts on the
    /// first poll. An error on a later page is yielded after the earlier
    /// definitions.
    pub fn iterate_definitions(
        self,
        params: PageParams,
    ) -> impl Stream<Item = Result<models::EventDefinition, Error>> + 'a {
        let limit = params.limit;
        pages(move |cursor| async move {
            let page = self.list_definitions(PageParams { limit, cursor }).await?;
            Ok((page.results, page.next_cursor))
        })
    }

    /// Fetch one event definition.
    pub async fn get_definition(&self, action: &str) -> Result<models::EventDefinition, Error> {
        require_action(action)?;
        let path = definition_path(action);
        self.call(true, 200, || self.get(path.clone(), &[]), decode)
            .await
    }

    /// Define an action; only the members that are `Some` are sent.
    pub async fn create_definition(
        &self,
        definition: DefinitionInput,
    ) -> Result<models::EventDefinition, Error> {
        require_action(&definition.action)?;
        let body = models::EventDefinitionWriteRequest {
            action: definition.action,
            description: definition.description,
            allowed_target_types: definition.allowed_target_types,
            is_active: definition.is_active,
        };
        self.call(
            true,
            201,
            || {
                self.request(reqwest::Method::POST, DEFINITIONS_PATH.to_string())
                    .json(&body)
            },
            decode,
        )
        .await
    }

    /// Change a definition; only the members that are `Some` are sent, and
    /// `new_action` renames the action.
    pub async fn update_definition(
        &self,
        action: &str,
        update: DefinitionUpdate,
    ) -> Result<models::EventDefinition, Error> {
        require_action(action)?;
        let path = definition_path(action);
        let body = models::PatchedEventDefinitionWriteRequest {
            action: update.new_action,
            description: update.description,
            allowed_target_types: update.allowed_target_types,
            is_active: update.is_active,
        };
        self.call(
            true,
            200,
            || {
                self.request(reqwest::Method::PATCH, path.clone())
                    .json(&body)
            },
            decode,
        )
        .await
    }

    /// Delete an event definition.
    pub async fn delete_definition(&self, action: &str) -> Result<(), Error> {
        require_action(action)?;
        let path = definition_path(action);
        self.call(
            true,
            204,
            || self.request(reqwest::Method::DELETE, path.clone()),
            |_| Ok(()),
        )
        .await
    }

    /// Fetch one page of an action's schema versions, newest first.
    pub async fn list_versions(
        &self,
        action: &str,
        params: PageParams,
    ) -> Result<SchemaVersionPage, Error> {
        require_action(action)?;
        let path = versions_path(action);
        let query = page_query(&params);
        let page: models::PaginatedEventSchemaVersionList = self
            .call(true, 200, || self.get(path.clone(), &query), decode)
            .await?;
        Ok(SchemaVersionPage {
            results: page.results,
            next_cursor: next_cursor(page.next),
        })
    }

    /// Walk every page of an action's schema versions: a lazy stream that
    /// starts on the first poll. An error on a later page is yielded after
    /// the earlier versions.
    pub fn iterate_versions(
        self,
        action: impl Into<String>,
        params: PageParams,
    ) -> impl Stream<Item = Result<models::EventSchemaVersion, Error>> + 'a {
        let action = action.into();
        let limit = params.limit;
        pages(move |cursor| {
            let action = action.clone();
            async move {
                let page = self
                    .list_versions(&action, PageParams { limit, cursor })
                    .await?;
                Ok((page.results, page.next_cursor))
            }
        })
    }

    /// Fetch one schema version and its `ETag`.
    pub async fn get_version(
        &self,
        action: &str,
        version: i32,
    ) -> Result<SchemaVersionResult, Error> {
        require_action(action)?;
        let path = version_path(action, version);
        self.call(true, 200, || self.get(path.clone(), &[]), versioned)
            .await
    }

    /// Create a draft from a JSON Schema document. Sent once: it is never
    /// retried.
    pub async fn create_version(
        &self,
        action: &str,
        schema: Map<String, Value>,
    ) -> Result<SchemaVersionResult, Error> {
        require_action(action)?;
        let path = versions_path(action);
        let body = document(schema);
        self.call(
            false,
            201,
            || {
                self.request(reqwest::Method::POST, path.clone())
                    .json(&body)
            },
            versioned,
        )
        .await
    }

    /// Replace a draft's document. With `if_match` (an `ETag` you read) a
    /// concurrent edit fails with [`Error::Api`] of status 412 instead of
    /// being overwritten.
    pub async fn update_version(
        &self,
        action: &str,
        version: i32,
        schema: Map<String, Value>,
        if_match: Option<&str>,
    ) -> Result<SchemaVersionResult, Error> {
        require_action(action)?;
        let path = version_path(action, version);
        let body = document(schema);
        self.call(
            true,
            200,
            || {
                let request = self.request(reqwest::Method::PUT, path.clone()).json(&body);
                match if_match {
                    Some(etag) => request.header(reqwest::header::IF_MATCH, etag),
                    None => request,
                }
            },
            versioned,
        )
        .await
    }

    /// Delete a draft; a published version fails with [`Error::Api`] of
    /// status 409.
    pub async fn delete_version(&self, action: &str, version: i32) -> Result<(), Error> {
        require_action(action)?;
        let path = version_path(action, version);
        self.call(
            true,
            204,
            || self.request(reqwest::Method::DELETE, path.clone()),
            |_| Ok(()),
        )
        .await
    }

    /// Publish a draft: it becomes immutable and validates ingest.
    pub async fn publish_version(
        &self,
        action: &str,
        version: i32,
    ) -> Result<SchemaVersionResult, Error> {
        require_action(action)?;
        let path = format!("{}publish/", version_path(action, version));
        self.call(
            true,
            200,
            || self.request(reqwest::Method::POST, path.clone()),
            versioned,
        )
        .await
    }

    /// Start the migration window of a superseded version.
    pub async fn deprecate_version(
        &self,
        action: &str,
        version: i32,
    ) -> Result<SchemaVersionResult, Error> {
        require_action(action)?;
        let path = format!("{}deprecate/", version_path(action, version));
        self.call(
            true,
            200,
            || self.request(reqwest::Method::POST, path.clone()),
            versioned,
        )
        .await
    }

    /// Dry-run a document against the action's newest stored events; nothing
    /// is stored.
    pub async fn check_schema(
        &self,
        action: &str,
        schema: Map<String, Value>,
    ) -> Result<models::SchemaCheck, Error> {
        require_action(action)?;
        let path = format!("{}check/", versions_path(action));
        let body = document(schema);
        self.call(
            true,
            200,
            || {
                self.request(reqwest::Method::POST, path.clone())
                    .json(&body)
            },
            decode,
        )
        .await
    }

    /// The standard catalog: every pack and its actions.
    pub async fn list_standard_packs(&self) -> Result<models::StandardEventCatalog, Error> {
        self.call(
            true,
            200,
            || self.get(STANDARD_PATH.to_string(), &[]),
            decode,
        )
        .await
    }

    /// Install packs by slug. The slugs are forwarded unchanged and the server
    /// judges them.
    pub async fn install_standard_packs(
        &self,
        packs: Vec<String>,
    ) -> Result<models::StandardPackInstallResult, Error> {
        let body = models::StandardPackInstallRequest { packs };
        self.call(
            true,
            200,
            || {
                self.request(reqwest::Method::POST, format!("{STANDARD_PATH}install/"))
                    .json(&body)
            },
            decode,
        )
        .await
    }

    fn request(&self, method: reqwest::Method, path: String) -> RequestBuilder {
        self.client
            .http()
            .request(method, self.client.endpoint(&path))
    }

    fn get(&self, path: String, query: &[(&'static str, String)]) -> RequestBuilder {
        let request = self.request(reqwest::Method::GET, path);
        if query.is_empty() {
            request
        } else {
            request.query(query)
        }
    }

    /// One operation: `build` makes the identical request for every attempt,
    /// the `expected` 2xx status is the only success, and `result` reads the
    /// answer. Retried like the events operations unless `retries` is false.
    async fn call<T>(
        &self,
        retries: bool,
        expected: u16,
        build: impl Fn() -> RequestBuilder,
        result: impl Fn(&RawResponse) -> Result<T, Error>,
    ) -> Result<T, Error> {
        let policy = if retries {
            self.client.retry_policy()
        } else {
            RetryPolicy {
                max_retries: 0,
                ..self.client.retry_policy()
            }
        };
        run_with_retries(policy, || async {
            let response = send(build()).await?;
            if response.status == expected {
                result(&response)
            } else if (200..300).contains(&response.status) {
                Err(Error::api(response.status))
            } else {
                Err(Error::from_http(
                    response.status,
                    &response.headers,
                    &response.body,
                ))
            }
        })
        .await
    }
}

fn require_action(action: &str) -> Result<(), Error> {
    if action.is_empty() {
        return Err(Error::validation("action", "action must not be empty"));
    }
    Ok(())
}

fn definition_path(action: &str) -> String {
    format!("{DEFINITIONS_PATH}{}/", encode_segment(action))
}

fn versions_path(action: &str) -> String {
    format!("{}schema-versions/", definition_path(action))
}

fn version_path(action: &str, version: i32) -> String {
    format!("{}{version}/", versions_path(action))
}

/// The page selectors as query pairs in spec order; absent ones are not sent.
fn page_query(params: &PageParams) -> Vec<(&'static str, String)> {
    let mut pairs = Vec::new();
    if let Some(cursor) = &params.cursor {
        pairs.push(("cursor", cursor.clone()));
    }
    if let Some(limit) = params.limit {
        pairs.push(("limit", limit.to_string()));
    }
    pairs
}

fn document(schema: Map<String, Value>) -> models::EventSchemaDocumentRequest {
    models::EventSchemaDocumentRequest {
        schema: schema.into_iter().collect(),
    }
}

/// A 2xx body decoded with the generated model, or [`Error::Api`].
fn decode<T: DeserializeOwned>(response: &RawResponse) -> Result<T, Error> {
    serde_json::from_slice(&response.body).map_err(|_| Error::api(response.status))
}

fn versioned(response: &RawResponse) -> Result<SchemaVersionResult, Error> {
    Ok(SchemaVersionResult {
        schema_version: decode(response)?,
        etag: response
            .headers
            .get(reqwest::header::ETAG)
            .and_then(|value| value.to_str().ok())
            .map(str::to_string),
    })
}

fn next_cursor(next: Option<Option<String>>) -> Option<String> {
    next.flatten().as_deref().and_then(cursor_from_url)
}

/// A lazy stream over a paged operation: `fetch(cursor)` answers one page's
/// results and the next cursor, and the stream ends after the page whose next
/// cursor is `None`.
fn pages<'a, T, F, Fut>(fetch: F) -> impl Stream<Item = Result<T, Error>> + 'a
where
    T: 'a,
    F: Fn(Option<String>) -> Fut + Clone + 'a,
    Fut: Future<Output = Result<(Vec<T>, Option<String>), Error>> + 'a,
{
    let state = (VecDeque::<T>::new(), None::<String>, false);
    stream::try_unfold(state, move |(mut pending, mut cursor, mut done)| {
        let fetch = fetch.clone();
        async move {
            loop {
                if let Some(item) = pending.pop_front() {
                    return Ok(Some((item, (pending, cursor, done))));
                }
                if done {
                    return Ok(None);
                }
                let (results, next) = fetch(cursor.take()).await?;
                done = next.is_none();
                cursor = next;
                pending = results.into();
            }
        }
    })
}