icinga2_api/api/action/
delay_notification.rs

1//! API Action delay-notification
2//!
3//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#delay-notification)
4
5use serde::{Deserialize, Serialize};
6
7use crate::serde::{deserialize_icinga_timestamp, serialize_icinga_timestamp};
8use crate::types::action::StatusResponse;
9use crate::types::enums::object_type::IcingaObjectType;
10use crate::types::filter::IcingaFilter;
11use crate::types::query::ResultsWrapper;
12use crate::types::rest::{RestApiEndpoint, RestApiResponse};
13
14/// REST API Endpoint for the delay-notification call
15#[derive(Debug, Clone, derive_builder::Builder, Serialize, Deserialize)]
16#[builder(
17    build_fn(error = "crate::error::Error", validate = "Self::validate"),
18    derive(Debug)
19)]
20pub struct DelayNotification {
21    /// delay notifications until this timestamp
22    #[serde(
23        serialize_with = "serialize_icinga_timestamp",
24        deserialize_with = "deserialize_icinga_timestamp"
25    )]
26    timestamp: time::OffsetDateTime,
27    /// filter to target which host and/or service for which to delay notifications
28    #[builder(default, setter(strip_option, into))]
29    #[serde(flatten)]
30    filter: Option<IcingaFilter>,
31}
32
33impl DelayNotification {
34    /// create a new builder for this endpoint
35    ///
36    /// this is usually the first step to calling this REST API endpoint
37    #[must_use]
38    pub fn builder() -> DelayNotificationBuilder {
39        DelayNotificationBuilder::default()
40    }
41}
42
43impl DelayNotificationBuilder {
44    /// makes sure the filter object type is valid for this call (either Host or Service)
45    ///
46    /// # Errors
47    ///
48    /// this returns an error if the filter field object type is not Host or Service
49    pub fn validate(&self) -> Result<(), crate::error::Error> {
50        if let Some(Some(filter)) = &self.filter {
51            if filter.object_type != IcingaObjectType::Host
52                && filter.object_type != IcingaObjectType::Service
53            {
54                Err(crate::error::Error::FilterObjectTypeMismatch(
55                    vec![IcingaObjectType::Host, IcingaObjectType::Service],
56                    filter.object_type.to_owned(),
57                ))
58            } else {
59                Ok(())
60            }
61        } else {
62            Ok(())
63        }
64    }
65}
66
67impl RestApiEndpoint for DelayNotification {
68    type RequestBody = DelayNotification;
69
70    fn method(&self) -> Result<reqwest::Method, crate::error::Error> {
71        Ok(reqwest::Method::POST)
72    }
73
74    fn url(&self, base_url: &url::Url) -> Result<url::Url, crate::error::Error> {
75        base_url
76            .join("v1/actions/delay-notification")
77            .map_err(crate::error::Error::CouldNotParseUrlFragment)
78    }
79
80    fn request_body(
81        &self,
82    ) -> Result<Option<std::borrow::Cow<'_, Self::RequestBody>>, crate::error::Error>
83    where
84        Self::RequestBody: Clone + serde::Serialize + std::fmt::Debug,
85    {
86        Ok(Some(std::borrow::Cow::Borrowed(self)))
87    }
88}
89
90impl RestApiResponse<DelayNotification> for ResultsWrapper<StatusResponse> {}