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    command_type: Option<IcingaCommandType>,
28    /// 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$
29    command: Option<String>,
30    /// The endpoint to execute the command on. It can be a macro string. Default: $command_endpoint$.
31    endpoint: Option<String>,
32    /// Macro overrides. Default: {}
33    macros: Option<BTreeMap<String, serde_json::Value>>,
34    /// The user used for the notification command.
35    user: Option<IcingaUserName>,
36    /// The notification used for the notification command.
37    notification: Option<String>,
38}
39
40impl ExecuteCommand {
41    /// create a new builder for this endpoint
42    ///
43    /// this is usually the first step to calling this REST API endpoint
44    #[must_use]
45    pub fn builder() -> ExecuteCommandBuilder {
46        ExecuteCommandBuilder::default()
47    }
48}
49
50impl RestApiEndpoint for ExecuteCommand {
51    type RequestBody = ExecuteCommand;
52
53    fn method(&self) -> Result<reqwest::Method, crate::error::Error> {
54        Ok(reqwest::Method::POST)
55    }
56
57    fn url(&self, base_url: &url::Url) -> Result<url::Url, crate::error::Error> {
58        base_url
59            .join("v1/actions/execute-command")
60            .map_err(crate::error::Error::CouldNotParseUrlFragment)
61    }
62
63    fn request_body(
64        &self,
65    ) -> Result<Option<std::borrow::Cow<Self::RequestBody>>, crate::error::Error>
66    where
67        Self::RequestBody: Clone + serde::Serialize + std::fmt::Debug,
68    {
69        Ok(Some(std::borrow::Cow::Borrowed(self)))
70    }
71}
72
73impl RestApiResponse<ExecuteCommand> for ResultsWrapper<ExecuteCommandResponse> {}