Skip to main content

icinga2_api/api/action/
remove_downtime.rs

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