Skip to main content

icinga2_api/api/action/
send_custom_notification.rs

1//! API Action send-custom-notification
2//!
3//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#send-custom-notification)
4
5use serde::{Deserialize, Serialize};
6
7use crate::types::action::StatusResponse;
8use crate::types::enums::object_type::IcingaObjectType;
9use crate::types::filter::IcingaFilter;
10use crate::types::query::ResultsWrapper;
11use crate::types::rest::{RestApiEndpoint, RestApiResponse};
12
13/// REST API Endpoint for the reschedule-check call
14#[derive(Debug, Clone, derive_builder::Builder, Serialize, Deserialize)]
15#[builder(
16    build_fn(error = "crate::error::Error", validate = "Self::validate"),
17    derive(Debug)
18)]
19pub struct SendCustomNotification {
20    /// the author of the custom notification
21    author: String,
22    /// the body of the custom notification
23    comment: String,
24    /// Default: false. If true, the notification is sent regardless of downtimes or whether notifications are enabled or not.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    force: Option<bool>,
27    /// filter to target which host and/or service for which to send the custom notification
28    #[builder(default, setter(strip_option, into))]
29    #[serde(flatten)]
30    filter: Option<IcingaFilter>,
31}
32
33impl SendCustomNotification {
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() -> SendCustomNotificationBuilder {
39        SendCustomNotificationBuilder::default()
40    }
41}
42
43impl SendCustomNotificationBuilder {
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 SendCustomNotification {
68    type RequestBody = Self;
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/send-custom-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<SendCustomNotification> for ResultsWrapper<StatusResponse> {}