icinga2_api/types/
join_types.rs

1//! The possible Joins for each API query type supporting them
2
3use std::collections::BTreeMap;
4
5use serde::{Deserialize, Serialize};
6
7pub mod dependency;
8pub mod host;
9pub mod notification;
10pub mod service;
11pub mod user;
12pub mod zone;
13
14/// a marker trait for all the various join types for the different objects
15pub trait IcingaJoinType {}
16
17/// joins
18#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
19pub enum IcingaJoins<'a, JT>
20where
21    JT: IcingaJoinType + Ord + std::fmt::Display,
22{
23    /// do not include any joins
24    NoJoins,
25    /// include specific joins
26    SpecificJoins {
27        /// include the full objects for these joins
28        full: Vec<JT>,
29        /// include just the specified fields for these joins
30        partial: BTreeMap<JT, Vec<&'a str>>,
31    },
32    /// include full objects for all possible joins
33    AllJoins,
34}
35
36/// return type for joins, either full or partial
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
38#[serde(untagged)]
39pub enum IcingaJoinResult<T> {
40    /// a full result we get if we just specified e.g. joins=host
41    Full(T),
42    /// a partial result we get if we specified individual fields, e.g. joins=host.name
43    Partial(BTreeMap<String, serde_json::Value>),
44}
45
46/// shared code for all the handlers that have a joins parameters
47pub(crate) fn add_joins_to_url<JT: IcingaJoinType + Ord + std::fmt::Display>(
48    url: &mut url::Url,
49    joins: &IcingaJoins<JT>,
50) -> Result<(), crate::error::Error> {
51    match joins {
52        IcingaJoins::NoJoins => (),
53        IcingaJoins::AllJoins => {
54            url.query_pairs_mut().append_pair("all_joins", "1");
55        }
56        IcingaJoins::SpecificJoins { full, partial } => {
57            for j in full {
58                url.query_pairs_mut().append_pair("joins", &j.to_string());
59            }
60            for (j, fields) in partial {
61                for f in fields {
62                    url.query_pairs_mut()
63                        .append_pair("joins", &format!("{}.{}", &j.to_string(), &f));
64                }
65            }
66        }
67    }
68    Ok(())
69}