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
//! Icinga2 host
//!
//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#host)

crate::types::query::query_with_joins!(
    ListHosts,
    ListHostsBuilder,
    monitoring_objects,
    host,
    IcingaHost,
    IcingaHostJoinTypes,
    IcingaHostJoins,
    IcingaObjectType::Host,
    "v1/objects/hosts"
);

#[cfg(test)]
mod test {
    use super::*;
    use std::{collections::BTreeMap, error::Error};
    use tracing_test::traced_test;

    use crate::{
        api::async_client::Icinga2Async,
        api::blocking::Icinga2,
        types::{
            enums::{host_state::IcingaHostState, object_type::IcingaObjectType},
            filter::IcingaFilter,
            join_types::IcingaJoins,
            metadata::IcingaMetadataType,
        },
    };

    #[traced_test]
    #[test]
    fn test_hosts() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let icinga2 = Icinga2::from_config_file(std::path::Path::new(&std::env::var(
            "ICINGA_TEST_INSTANCE_CONFIG",
        )?))?;
        let api_endpoint = ListHosts::builder()
            .joins(IcingaJoins::AllJoins)
            .meta([IcingaMetadataType::UsedBy, IcingaMetadataType::Location])
            .build()?;
        let _: ResultsWrapper<QueryResultObjectWithJoins<IcingaHost, IcingaHostJoins>> =
            icinga2.rest(api_endpoint)?;
        Ok(())
    }

    #[traced_test]
    #[test]
    fn test_hosts_filtered() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let icinga2 = Icinga2::from_config_file(std::path::Path::new(&std::env::var(
            "ICINGA_TEST_INSTANCE_CONFIG",
        )?))?;
        let api_endpoint = ListHosts::builder()
            .filter(IcingaFilter {
                object_type: IcingaObjectType::Host,
                filter: "host.state == filter_state".to_string(),
                filter_vars: BTreeMap::from([(
                    "filter_state".to_string(),
                    serde_json::to_value(IcingaHostState::Up)?,
                )]),
            })
            .build()?;
        let _: ResultsWrapper<QueryResultObject<IcingaHost>> = icinga2.rest(api_endpoint)?;
        Ok(())
    }

    #[traced_test]
    #[tokio::test]
    async fn test_hosts_async() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let icinga2 = Icinga2Async::from_config_file(std::path::Path::new(&std::env::var(
            "ICINGA_TEST_INSTANCE_CONFIG",
        )?))?;
        let api_endpoint = ListHosts::builder()
            .joins(IcingaJoins::AllJoins)
            .meta([IcingaMetadataType::UsedBy, IcingaMetadataType::Location])
            .build()?;
        let _: ResultsWrapper<QueryResultObjectWithJoins<IcingaHost, IcingaHostJoins>> =
            icinga2.rest(api_endpoint).await?;
        Ok(())
    }
}