Skip to main content

icinga2_api/api/query/monitoring_objects/
host.rs

1//! Icinga2 host
2//!
3//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#host)
4
5crate::types::query::query_with_joins!(
6    ListHosts,
7    ListHostsBuilder,
8    monitoring_objects,
9    host,
10    IcingaHost,
11    IcingaHostJoinTypes,
12    IcingaHostJoins,
13    IcingaObjectType::Host,
14    "v1/objects/hosts"
15);
16
17#[cfg(test)]
18mod test {
19    use super::*;
20    use std::error::Error;
21    use tracing_test::traced_test;
22
23    use crate::{
24        api::async_client::Icinga2Async, api::blocking::Icinga2,
25        types::enums::host_state::IcingaHostState,
26    };
27
28    #[traced_test]
29    #[test]
30    fn test_hosts() -> Result<(), Box<dyn Error>> {
31        dotenvy::dotenv()?;
32        let icinga2 = Icinga2::from_config_file(std::path::Path::new(&std::env::var(
33            "ICINGA_TEST_INSTANCE_CONFIG",
34        )?))?;
35        let api_endpoint = ListHosts::builder()
36            .joins(IcingaJoins::AllJoins)
37            .meta([IcingaMetadataType::UsedBy, IcingaMetadataType::Location])
38            .build()?;
39        let _response: ResultsWrapper<QueryResultObjectWithJoins<IcingaHost, IcingaHostJoins>> =
40            icinga2.rest(api_endpoint)?;
41        Ok(())
42    }
43
44    #[traced_test]
45    #[test]
46    fn test_hosts_filtered() -> Result<(), Box<dyn Error>> {
47        dotenvy::dotenv()?;
48        let icinga2 = Icinga2::from_config_file(std::path::Path::new(&std::env::var(
49            "ICINGA_TEST_INSTANCE_CONFIG",
50        )?))?;
51        let api_endpoint = ListHosts::builder()
52            .filter(IcingaFilter {
53                object_type: IcingaObjectType::Host,
54                filter: "host.state == filter_state".to_string(),
55                filter_vars: BTreeMap::from([(
56                    "filter_state".to_string(),
57                    serde_json::to_value(IcingaHostState::Up)?,
58                )]),
59            })
60            .build()?;
61        let _response: ResultsWrapper<QueryResultObject<IcingaHost>> =
62            icinga2.rest(api_endpoint)?;
63        Ok(())
64    }
65
66    #[traced_test]
67    #[tokio::test]
68    async fn test_hosts_async() -> Result<(), Box<dyn Error>> {
69        dotenvy::dotenv()?;
70        let icinga2 = Icinga2Async::from_config_file(std::path::Path::new(&std::env::var(
71            "ICINGA_TEST_INSTANCE_CONFIG",
72        )?))?;
73        let api_endpoint = ListHosts::builder()
74            .joins(IcingaJoins::AllJoins)
75            .meta([IcingaMetadataType::UsedBy, IcingaMetadataType::Location])
76            .build()?;
77        let _response: ResultsWrapper<QueryResultObjectWithJoins<IcingaHost, IcingaHostJoins>> =
78            icinga2.rest(api_endpoint).await?;
79        Ok(())
80    }
81}