icinga2_api/types/
metadata.rs

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
//! structs related to the query metadata parameter
//! and the result of queries including metadata
use serde::{Deserialize, Serialize};

use super::common::{object::IcingaObject, source_location::IcingaSourceLocation};

/// possible meta parameter values
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum IcingaMetadataType {
    /// includes information about the other icinga objects using each returned object
    UsedBy,
    /// includes information about the config file location of each returned object
    Location,
}

impl std::fmt::Display for IcingaMetadataType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IcingaMetadataType::UsedBy => write!(f, "used_by"),
            IcingaMetadataType::Location => write!(f, "location"),
        }
    }
}

/// metadata
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct IcingaMetadata {
    /// which other icinga objects use this object
    pub used_by: Option<Vec<IcingaObject>>,
    /// where in the config file this object is defined
    pub location: Option<IcingaSourceLocation>,
}

/// shared code for all handlers that have a meta parameter
pub(crate) fn add_meta_to_url(
    url: &mut url::Url,
    meta: &[IcingaMetadataType],
) -> Result<(), crate::error::Error> {
    if !meta.is_empty() {
        for v in meta {
            url.query_pairs_mut().append_pair("meta", &v.to_string());
        }
    }
    Ok(())
}