Skip to main content

icinga2_api/api/action/
acknowledge_problem.rs

1//! API Action acknowledge-problem
2//!
3//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#acknowledge-problem)
4
5use serde::{Deserialize, Serialize};
6
7use crate::serde::{deserialize_optional_icinga_timestamp, serialize_optional_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 acknowledge-problem 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 AcknowledgeProblem {
21    /// the author of the acknowledgement
22    author: String,
23    /// the body of the acknowledgement comment
24    comment: String,
25    /// expiry time for the acknowledgement
26    #[serde(
27        serialize_with = "serialize_optional_icinga_timestamp",
28        deserialize_with = "deserialize_optional_icinga_timestamp"
29    )]
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    expiry: Option<time::OffsetDateTime>,
32    /// Whether the acknowledgement will be set until the service or host fully recovers. Defaults to false.
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    sticky: Option<bool>,
35    /// Whether a notification of the Acknowledgement type will be sent. Defaults to false.
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    notify: Option<bool>,
38    /// Whether the acknowledgement comment persists after the end of the acknowledgement
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    persistent: Option<bool>,
41    /// filter to target which host and/or service problem to acknowledge
42    #[builder(default, setter(strip_option, into))]
43    #[serde(flatten)]
44    filter: Option<IcingaFilter>,
45}
46
47impl AcknowledgeProblem {
48    /// create a new builder for this endpoint
49    ///
50    /// this is usually the first step to calling this REST API endpoint
51    #[must_use]
52    pub fn builder() -> AcknowledgeProblemBuilder {
53        AcknowledgeProblemBuilder::default()
54    }
55}
56
57impl AcknowledgeProblemBuilder {
58    /// makes sure the filter object type is valid for this call (either Host or Service)
59    ///
60    /// # Errors
61    ///
62    /// this returns an error if the filter field object type is not Host or Service
63    pub fn validate(&self) -> Result<(), crate::error::Error> {
64        if let Some(Some(filter)) = &self.filter {
65            if filter.object_type != IcingaObjectType::Host
66                && filter.object_type != IcingaObjectType::Service
67            {
68                Err(crate::error::Error::FilterObjectTypeMismatch(
69                    vec![IcingaObjectType::Host, IcingaObjectType::Service],
70                    filter.object_type.to_owned(),
71                ))
72            } else {
73                Ok(())
74            }
75        } else {
76            Ok(())
77        }
78    }
79}
80
81impl RestApiEndpoint for AcknowledgeProblem {
82    type RequestBody = Self;
83
84    fn method(&self) -> Result<reqwest::Method, crate::error::Error> {
85        Ok(reqwest::Method::POST)
86    }
87
88    fn url(&self, base_url: &url::Url) -> Result<url::Url, crate::error::Error> {
89        base_url
90            .join("v1/actions/acknowledge-problem")
91            .map_err(crate::error::Error::CouldNotParseUrlFragment)
92    }
93
94    fn request_body(
95        &self,
96    ) -> Result<Option<std::borrow::Cow<'_, Self::RequestBody>>, crate::error::Error>
97    where
98        Self::RequestBody: Clone + serde::Serialize + std::fmt::Debug,
99    {
100        Ok(Some(std::borrow::Cow::Borrowed(self)))
101    }
102}
103
104impl RestApiResponse<AcknowledgeProblem> for ResultsWrapper<StatusResponse> {}