Skip to main content

icinga2_api/api/action/
execute_command.rs

1//! API Action execute-command
2//!
3//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#execute-command)
4
5use std::collections::BTreeMap;
6
7use serde::{Deserialize, Serialize};
8
9use crate::serde::{deserialize_seconds_as_duration, serialize_duration_as_seconds};
10use crate::types::action::ExecuteCommandResponse;
11use crate::types::enums::command_type::IcingaCommandType;
12use crate::types::names::IcingaUserName;
13use crate::types::query::ResultsWrapper;
14use crate::types::rest::{RestApiEndpoint, RestApiResponse};
15
16/// REST API Endpoint for the execute-command call
17#[derive(Debug, Clone, derive_builder::Builder, Serialize, Deserialize)]
18#[builder(build_fn(error = "crate::error::Error"), derive(Debug))]
19pub struct ExecuteCommand {
20    /// The time to live of the execution
21    #[serde(
22        serialize_with = "serialize_duration_as_seconds",
23        deserialize_with = "deserialize_seconds_as_duration"
24    )]
25    ttl: time::Duration,
26    /// the command type (check, event or notification)
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    command_type: Option<IcingaCommandType>,
29    /// The command to execute. Its type must the same as command_type. It can be a macro string. Default: depending on the command_type it’s either $check_command$, $event_command$ or $notification_command$
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    command: Option<String>,
32    /// The endpoint to execute the command on. It can be a macro string. Default: $command_endpoint$.
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    endpoint: Option<String>,
35    /// Macro overrides. Default: {}
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    macros: Option<BTreeMap<String, serde_json::Value>>,
38    /// The user used for the notification command.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    user: Option<IcingaUserName>,
41    /// The notification used for the notification command.
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    notification: Option<String>,
44}
45
46impl ExecuteCommand {
47    /// create a new builder for this endpoint
48    ///
49    /// this is usually the first step to calling this REST API endpoint
50    #[must_use]
51    pub fn builder() -> ExecuteCommandBuilder {
52        ExecuteCommandBuilder::default()
53    }
54}
55
56impl RestApiEndpoint for ExecuteCommand {
57    type RequestBody = Self;
58
59    fn method(&self) -> Result<reqwest::Method, crate::error::Error> {
60        Ok(reqwest::Method::POST)
61    }
62
63    fn url(&self, base_url: &url::Url) -> Result<url::Url, crate::error::Error> {
64        base_url
65            .join("v1/actions/execute-command")
66            .map_err(crate::error::Error::CouldNotParseUrlFragment)
67    }
68
69    fn request_body(
70        &self,
71    ) -> Result<Option<std::borrow::Cow<'_, Self::RequestBody>>, crate::error::Error>
72    where
73        Self::RequestBody: Clone + serde::Serialize + std::fmt::Debug,
74    {
75        Ok(Some(std::borrow::Cow::Borrowed(self)))
76    }
77}
78
79impl RestApiResponse<ExecuteCommand> for ResultsWrapper<ExecuteCommandResponse> {}