icinga2_api/types/common/
command.rs

1//! Command - shared fields in the various command types
2//!
3//! [Definition in Icinga Source](https://github.com/Icinga/icinga2/blob/master/lib/icinga/command.ti)
4
5use std::collections::BTreeMap;
6
7use serde::{Deserialize, Serialize};
8
9use crate::serde::{
10    deserialize_optional_seconds_as_duration, serialize_optional_duration_as_seconds,
11};
12
13use super::{
14    custom_var_object::{CustomVarHolder, IcingaCustomVarObject},
15    function::IcingaFunction,
16};
17
18/// shared fields in the various command objects
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
20pub struct IcingaCommand {
21    /// shared config object and custom variable fields
22    #[serde(flatten)]
23    pub custom_var: IcingaCustomVarObject,
24    /// the descriptions of the command arguments
25    pub arguments: Option<BTreeMap<String, IcingaCommandArgumentDescription>>,
26    /// the actual command
27    pub command: Option<IcingaCommandLine>,
28    /// environment variables
29    pub env: Option<BTreeMap<String, String>>,
30    /// function for execution
31    pub execute: IcingaFunction,
32    /// command timeout
33    #[serde(default)]
34    #[serde(
35        serialize_with = "serialize_optional_duration_as_seconds",
36        deserialize_with = "deserialize_optional_seconds_as_duration"
37    )]
38    pub timeout: Option<time::Duration>,
39}
40
41impl CustomVarHolder for IcingaCommand {
42    fn custom_var_value(&self, name: &str) -> Option<&serde_json::Value> {
43        self.custom_var.custom_var_value(name)
44    }
45}
46
47/// command parameters (scalar values basically)
48#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
49#[serde(untagged)]
50pub enum IcingaCommandParameter {
51    /// string value
52    String(String),
53    /// Boolean
54    Boolean(bool),
55    /// Integer
56    Integer(i64),
57}
58
59/// command to execute with parameters
60#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
61#[serde(untagged)]
62pub enum IcingaCommandLine {
63    /// a single string for the whole command, will likely need a shell to do
64    /// word splitting
65    Shell(String),
66    /// an icinga function
67    Function(IcingaFunction),
68    /// individual command and parameters
69    Exec(Vec<IcingaCommandParameter>),
70}
71
72/// set_if condition in command argument description
73#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
74#[serde(untagged)]
75pub enum IcingaArgumentCondition {
76    /// a string condition, most likely a boolean variable
77    String(String),
78    /// a function condition
79    Function(IcingaFunction),
80}
81
82/// the description of a single command argument
83#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
84#[serde(untagged)]
85pub enum IcingaCommandArgumentDescription {
86    /// a simple string with the argument(s)
87    String(String),
88    /// an icinga function
89    Function(IcingaFunction),
90    /// a full description with details
91    FullDescription {
92        /// the description of this argument
93        description: Option<String>,
94        /// the default value for this argument
95        value: Option<String>,
96        /// name of an argument to set
97        key: Option<String>,
98        /// should the key be skipped
99        skip_key: Option<bool>,
100        /// should the key be repeated
101        repeat_key: Option<bool>,
102        /// condition when to set it
103        set_if: Option<IcingaArgumentCondition>,
104        /// is this argument required
105        required: Option<bool>,
106        /// determines the order in which the arguments are used
107        order: Option<u64>,
108        /// separator for multiple values
109        separator: Option<String>,
110    },
111}