Skip to main content

elasticctl_api/
observe.rs

1use elasticctl_core::{Error, ErrorKind, Result};
2
3#[derive(Debug, Clone, PartialEq, serde::Serialize)]
4pub struct Observed {
5    pub object: &'static str,
6    pub id: String,
7    pub state: Option<serde_json::Value>,
8}
9
10#[derive(Debug, Clone, PartialEq, Default, serde::Serialize)]
11pub struct Preconditions(pub Vec<Observed>);
12
13impl Preconditions {
14    pub fn sorted(mut self) -> Self {
15        self.0
16            .sort_by(|a, b| a.object.cmp(b.object).then_with(|| a.id.cmp(&b.id)));
17        self
18    }
19}
20
21pub fn require_rule_identity(seen: &Preconditions) -> Result<()> {
22    for entry in &seen.0 {
23        if entry.object == "rule"
24            && let Some(state) = &entry.state
25            && state.get("rule_id").and_then(serde_json::Value::as_str) != Some(&entry.id)
26        {
27            return Err(Error::new(
28                ErrorKind::Conflict,
29                "a rule read returned a different rule_id",
30            ));
31        }
32    }
33    Ok(())
34}