zabbix_api/host/
create.rs

1use super::model::{ZabbixHostInterface, ZabbixHostInventory, ZabbixHostTag};
2use crate::r#macro::create::CreateZabbixHostMacro;
3use crate::{hostgroup::model::ZabbixHostGroupId, template::model::ZabbixTemplateId};
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7const PSK: u8 = 2;
8const CERT: u8 = 4;
9
10#[skip_serializing_none]
11#[derive(Serialize, Debug)]
12pub struct TlsConfig {
13    tls_connect: u8,
14    tls_accept: u8,
15    tls_psk_identity: Option<String>,
16    tls_psk: Option<String>,
17    tls_issuer: Option<String>,
18    tls_subject: Option<String>,
19}
20
21impl TlsConfig {
22    pub fn new_psk(psk_identity: String, psk: String) -> TlsConfig {
23        TlsConfig {
24            tls_connect: PSK,
25            tls_accept: PSK,
26            tls_psk_identity: Some(psk_identity),
27            tls_psk: Some(psk),
28            tls_issuer: None,
29            tls_subject: None,
30        }
31    }
32
33    pub fn new_cert(issuer: String, subject: String) -> TlsConfig {
34        TlsConfig {
35            tls_connect: CERT,
36            tls_accept: CERT,
37            tls_psk_identity: None,
38            tls_psk: None,
39            tls_issuer: Some(issuer),
40            tls_subject: Some(subject),
41        }
42    }
43}
44
45/// API: https://www.zabbix.com/documentation/6.0/en/manual/api/reference/host/create
46#[skip_serializing_none]
47#[derive(Serialize, Debug, Default)]
48pub struct CreateHostRequest {
49    pub host: String,
50    pub name: Option<String>,
51    pub groups: Vec<ZabbixHostGroupId>,
52    pub interfaces: Vec<ZabbixHostInterface>,
53    pub tags: Vec<ZabbixHostTag>,
54    pub templates: Vec<ZabbixTemplateId>,
55    pub macros: Vec<CreateZabbixHostMacro>,
56    pub inventory_mode: InventoryMode,
57    pub inventory: ZabbixHostInventory,
58    #[serde(flatten)]
59    pub tls_config: Option<TlsConfig>,
60}
61
62#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize)]
63pub enum InventoryMode {
64    #[default]
65    #[serde(rename = "-1")]
66    Disabled,
67    #[serde(rename = "0")]
68    Manual,
69    #[serde(rename = "1")]
70    Automatic,
71}
72
73impl CreateHostRequest {
74    pub fn builder(host: impl ToString) -> CreateHostRequestBuilder {
75        CreateHostRequestBuilder::new(host)
76    }
77}
78
79pub struct CreateHostRequestBuilder {
80    inner: CreateHostRequest,
81}
82
83impl CreateHostRequestBuilder {
84    pub fn new(host: impl ToString) -> Self {
85        Self {
86            inner: CreateHostRequest {
87                host: host.to_string(),
88                ..Default::default()
89            },
90        }
91    }
92
93    pub fn name(mut self, name: impl ToString) -> Self {
94        self.inner.name = Some(name.to_string());
95        self
96    }
97
98    pub fn group(mut self, group_id: impl ToString) -> Self {
99        self.inner.groups.push(ZabbixHostGroupId {
100            group_id: group_id.to_string(),
101        });
102        self
103    }
104
105    pub fn groups(mut self, groups: Vec<ZabbixHostGroupId>) -> Self {
106        self.inner.groups = groups;
107        self
108    }
109
110    pub fn interface(mut self, interface: ZabbixHostInterface) -> Self {
111        self.inner.interfaces.push(interface);
112        self
113    }
114
115    pub fn interfaces(mut self, interfaces: Vec<ZabbixHostInterface>) -> Self {
116        self.inner.interfaces = interfaces;
117        self
118    }
119
120    pub fn tag(mut self, tag: impl ToString, value: impl ToString) -> Self {
121        self.inner.tags.push(ZabbixHostTag {
122            tag: tag.to_string(),
123            value: value.to_string(),
124        });
125        self
126    }
127
128    pub fn tags(mut self, tags: Vec<ZabbixHostTag>) -> Self {
129        self.inner.tags = tags;
130        self
131    }
132
133    pub fn template(mut self, template_id: impl ToString) -> Self {
134        self.inner.templates.push(ZabbixTemplateId {
135            template_id: template_id.to_string(),
136        });
137        self
138    }
139
140    pub fn templates(mut self, templates: Vec<ZabbixTemplateId>) -> Self {
141        self.inner.templates = templates;
142        self
143    }
144
145    pub fn macro_entry(mut self, macro_entry: CreateZabbixHostMacro) -> Self {
146        self.inner.macros.push(macro_entry);
147        self
148    }
149
150    pub fn macros(mut self, macros: Vec<CreateZabbixHostMacro>) -> Self {
151        self.inner.macros = macros;
152        self
153    }
154
155    pub fn inventory_mode(mut self, mode: InventoryMode) -> Self {
156        self.inner.inventory_mode = mode;
157        self
158    }
159
160    pub fn inventory_disabled(mut self) -> Self {
161        self.inner.inventory_mode = InventoryMode::Disabled;
162        self
163    }
164
165    pub fn inventory_manual(mut self) -> Self {
166        self.inner.inventory_mode = InventoryMode::Manual;
167        self
168    }
169
170    pub fn inventory_automatic(mut self) -> Self {
171        self.inner.inventory_mode = InventoryMode::Automatic;
172        self
173    }
174
175    pub fn inventory(mut self, inventory: ZabbixHostInventory) -> Self {
176        self.inner.inventory = inventory;
177        self
178    }
179
180    pub fn tls_psk(mut self, psk_identity: impl ToString, psk: impl ToString) -> Self {
181        self.inner.tls_config = Some(TlsConfig::new_psk(
182            psk_identity.to_string(),
183            psk.to_string(),
184        ));
185        self
186    }
187
188    pub fn tls_cert(mut self, issuer: impl ToString, subject: impl ToString) -> Self {
189        self.inner.tls_config = Some(TlsConfig::new_cert(issuer.to_string(), subject.to_string()));
190        self
191    }
192
193    pub fn tls_config(mut self, tls_config: TlsConfig) -> Self {
194        self.inner.tls_config = Some(tls_config);
195        self
196    }
197
198    pub fn build(self) -> CreateHostRequest {
199        self.inner
200    }
201}
202
203#[derive(Deserialize, Debug)]
204pub struct CreateHostResponse {
205    #[serde(rename = "hostids")]
206    pub host_ids: Vec<String>,
207}