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::{collections::BTreeMap, error::Error};
21    use tracing_test::traced_test;
22
23    use crate::{
24        api::async_client::Icinga2Async,
25        api::blocking::Icinga2,
26        types::{
27            enums::{host_state::IcingaHostState, object_type::IcingaObjectType},
28            filter::IcingaFilter,
29            join_types::IcingaJoins,
30            metadata::IcingaMetadataType,
31        },
32    };
33
34    #[traced_test]
35    #[test]
36    fn test_hosts() -> Result<(), Box<dyn Error>> {
37        dotenvy::dotenv()?;
38        let icinga2 = Icinga2::from_config_file(std::path::Path::new(&std::env::var(
39            "ICINGA_TEST_INSTANCE_CONFIG",
40        )?))?;
41        let api_endpoint = ListHosts::builder()
42            .joins(IcingaJoins::AllJoins)
43            .meta([IcingaMetadataType::UsedBy, IcingaMetadataType::Location])
44            .build()?;
45        let _: ResultsWrapper<QueryResultObjectWithJoins<IcingaHost, IcingaHostJoins>> =
46            icinga2.rest(api_endpoint)?;
47        Ok(())
48    }
49
50    #[traced_test]
51    #[test]
52    fn test_hosts_filtered() -> Result<(), Box<dyn Error>> {
53        dotenvy::dotenv()?;
54        let icinga2 = Icinga2::from_config_file(std::path::Path::new(&std::env::var(
55            "ICINGA_TEST_INSTANCE_CONFIG",
56        )?))?;
57        let api_endpoint = ListHosts::builder()
58            .filter(IcingaFilter {
59                object_type: IcingaObjectType::Host,
60                filter: "host.state == filter_state".to_string(),
61                filter_vars: BTreeMap::from([(
62                    "filter_state".to_string(),
63                    serde_json::to_value(IcingaHostState::Up)?,
64                )]),
65            })
66            .build()?;
67        let _: ResultsWrapper<QueryResultObject<IcingaHost>> = icinga2.rest(api_endpoint)?;
68        Ok(())
69    }
70
71    #[traced_test]
72    #[tokio::test]
73    async fn test_hosts_async() -> Result<(), Box<dyn Error>> {
74        dotenvy::dotenv()?;
75        let icinga2 = Icinga2Async::from_config_file(std::path::Path::new(&std::env::var(
76            "ICINGA_TEST_INSTANCE_CONFIG",
77        )?))?;
78        let api_endpoint = ListHosts::builder()
79            .joins(IcingaJoins::AllJoins)
80            .meta([IcingaMetadataType::UsedBy, IcingaMetadataType::Location])
81            .build()?;
82        let _: ResultsWrapper<QueryResultObjectWithJoins<IcingaHost, IcingaHostJoins>> =
83            icinga2.rest(api_endpoint).await?;
84        Ok(())
85    }
86}