zabbix_api/host/
create.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    hostgroup::model::ZabbixHostGroupId, r#macro::model::ZabbixHostMacro,
7    template::model::ZabbixTemplateId,
8};
9
10use super::model::{ZabbixHostInterface, ZabbixHostTag};
11
12const PSK: u8 = 2;
13const CERT: u8 = 4;
14
15#[derive(Serialize, Debug)]
16pub struct TlsConfig {
17    tls_connect: u8,
18    tls_accept: u8,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    tls_psk_identity: Option<String>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    tls_psk: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    tls_issuer: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    tls_subject: Option<String>,
27}
28
29impl TlsConfig {
30    pub fn new_psk(psk_identity: String, psk: String) -> TlsConfig {
31        TlsConfig {
32            tls_connect: PSK,
33            tls_accept: PSK,
34            tls_psk_identity: Some(psk_identity),
35            tls_psk: Some(psk),
36            tls_issuer: None,
37            tls_subject: None,
38        }
39    }
40
41    pub fn new_cert(issuer: String, subject: String) -> TlsConfig {
42        TlsConfig {
43            tls_connect: CERT,
44            tls_accept: CERT,
45            tls_psk_identity: None,
46            tls_psk: None,
47            tls_issuer: Some(issuer),
48            tls_subject: Some(subject),
49        }
50    }
51}
52
53/// API: https://www.zabbix.com/documentation/6.0/en/manual/api/reference/host/create
54#[derive(Serialize, Debug)]
55pub struct CreateHostRequest {
56    pub host: String,
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub name: Option<String>,
59    pub groups: Vec<ZabbixHostGroupId>,
60    pub interfaces: Vec<ZabbixHostInterface>,
61    pub tags: Vec<ZabbixHostTag>,
62    pub templates: Vec<ZabbixTemplateId>,
63    pub macros: Vec<ZabbixHostMacro>,
64    pub inventory_mode: u8,
65    pub inventory: HashMap<String, String>,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    #[serde(flatten)]
68    pub tls_config: Option<TlsConfig>,
69}
70
71impl Default for CreateHostRequest {
72    fn default() -> CreateHostRequest {
73        CreateHostRequest {
74            host: "".to_string(),
75            name: None,
76            groups: Vec::new(),
77            interfaces: Vec::new(),
78            tags: Vec::new(),
79            templates: Vec::new(),
80            macros: Vec::new(),
81            inventory_mode: 0,
82            inventory: HashMap::new(),
83            tls_config: None,
84        }
85    }
86}
87
88#[derive(Deserialize, Debug)]
89pub struct CreateHostResponse {
90    #[serde(rename = "hostids")]
91    pub host_ids: Vec<String>,
92}