icinga2_api/api/action/
remove_comment.rs

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