Skip to main content

icinga2_api/api/action/
add_comment.rs

1//! API Action add-comment
2//!
3//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#add-comment)
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 add-comment 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 AddComment {
21    /// the author of the comment
22    author: String,
23    /// the body of the comment
24    comment: String,
25    /// expiry time for the comment
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    /// filter to target which host and/or service to attach the comment to
33    #[builder(default, setter(strip_option, into))]
34    #[serde(flatten)]
35    filter: Option<IcingaFilter>,
36}
37
38impl AddComment {
39    /// create a new builder for this endpoint
40    ///
41    /// this is usually the first step to calling this REST API endpoint
42    #[must_use]
43    pub fn builder() -> AddCommentBuilder {
44        AddCommentBuilder::default()
45    }
46}
47
48impl AddCommentBuilder {
49    /// makes sure the filter object type is valid for this call (either Host or Service)
50    ///
51    /// # Errors
52    ///
53    /// this returns an error if the filter field object type is not Host or Service
54    pub fn validate(&self) -> Result<(), crate::error::Error> {
55        if let Some(Some(filter)) = &self.filter {
56            if filter.object_type != IcingaObjectType::Host
57                && filter.object_type != IcingaObjectType::Service
58            {
59                Err(crate::error::Error::FilterObjectTypeMismatch(
60                    vec![IcingaObjectType::Host, IcingaObjectType::Service],
61                    filter.object_type.to_owned(),
62                ))
63            } else {
64                Ok(())
65            }
66        } else {
67            Ok(())
68        }
69    }
70}
71
72impl RestApiEndpoint for AddComment {
73    type RequestBody = Self;
74
75    fn method(&self) -> Result<reqwest::Method, crate::error::Error> {
76        Ok(reqwest::Method::POST)
77    }
78
79    fn url(&self, base_url: &url::Url) -> Result<url::Url, crate::error::Error> {
80        base_url
81            .join("v1/actions/add-comment")
82            .map_err(crate::error::Error::CouldNotParseUrlFragment)
83    }
84
85    fn request_body(
86        &self,
87    ) -> Result<Option<std::borrow::Cow<'_, Self::RequestBody>>, crate::error::Error>
88    where
89        Self::RequestBody: Clone + serde::Serialize + std::fmt::Debug,
90    {
91        Ok(Some(std::borrow::Cow::Borrowed(self)))
92    }
93}
94
95impl RestApiResponse<AddComment> for ResultsWrapper<StatusResponse> {}