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