opsview 0.1.12

A Rust Opsview API Client Library with batteries included
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use super::{Host, HostRef};
use crate::{prelude::*, util::*};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Represents a [Host Group](https://docs.itrsgroup.com/docs/opsview/6.8.9/configuration/hosts-groups/hosts-groups-concept/index.html#overview) in Opsview.
///
/// Host groups are used to organize and categorize hosts, allowing for structured monitoring and
/// management. A [`Host`] can only belong to one host group, but host groups can be nested to
/// create a hierarchical structure. Only leaf nodes in the hierarchy can have hosts assigned to
/// them.
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct HostGroup {
    // Required fields ---------------------------------------------------------------------------//
    /// The name of the `HostGroup`.
    pub name: String,

    // Optional fields ---------------------------------------------------------------------------//
    /// [`ConfigRefMap`] of child `HostGroupRef` objects, representing a hierarchical structure.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub children: Option<ConfigRefMap<HostGroupRef>>,

    /// [`ConfigRefMap`] of [`HostRef`] objects that belong to this `HostGroup`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hosts: Option<ConfigRefMap<HostRef>>,

    /// The parent `HostGroupRef` of this group.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<HostGroupRef>,

    // Read-only fields --------------------------------------------------------------------------//
    /// The unique identifier of the `HostGroup`.
    #[serde(
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_string_or_number_to_u64",
        default
    )]
    pub id: Option<u64>,

    /// A boolean indicating whether this `HostGroup` is a leaf node (i.e., has no children).
    /// Only leaf nodes can have hosts assigned to them.
    #[serde(
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_string_or_number_to_option_bool",
        serialize_with = "serialize_option_bool_as_string",
        default
    )]
    pub is_leaf: Option<bool>,

    /// The materialized path representing the group's position in the hierarchy.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub matpath: Option<String>,

    /// A reference string unique to this `HostGroup`.
    #[serde(
        rename = "ref",
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_readonly",
        default
    )]
    pub ref_: Option<String>,

    /// A boolean indicating whether the `HostGroup` is uncommitted.
    #[serde(
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_string_or_number_to_option_bool",
        serialize_with = "serialize_option_bool_as_string",
        default
    )]
    pub uncommitted: Option<bool>,
}

/// Enables the creation of a [`HostGroup`] instance from a JSON representation.
/// Typically used when parsing JSON data from the Opsview API.
impl CreateFromJson for HostGroup {}

impl ConfigObject for HostGroup {
    type Builder = HostGroupBuilder;

    /// Returns a builder for constructing a [`HostGroup`] object.
    ///
    /// # Returns
    /// A [`HostGroupBuilder`] object.
    fn builder() -> Self::Builder {
        HostGroupBuilder::new()
    }

    /// Provides the configuration path for a [`HostGroup`] object within the Opsview system.
    ///
    /// # Returns
    /// A string representing the API path where host groups are configured.
    fn config_path() -> Option<String> {
        Some("/config/hostgroup".to_string())
    }

    /// Returns a minimal `HostGroup` object with only the name set.
    ///
    /// # Arguments
    /// * `name` - Name of the [`HostGroup`].
    ///
    /// # Returns
    /// A minimal `HostGroup` object with only the name set, and the rest of the fields in their
    /// default states.
    fn minimal(name: &str) -> Result<Self, OpsviewConfigError> {
        Ok(Self {
            name: validate_and_trim_hostgroup_name(name)?,
            ..Default::default()
        })
    }

    /// Returns the unique name of the [`HostGroup`] object. In this case the matpath, since they
    /// are unique while hostgroup names are not.
    ///
    /// This name is used to identify the `HostGroup` when building the `HashMap` for an
    /// [`ConfigObjectMap`].
    fn unique_name(&self) -> String {
        if let Some(matpath) = &self.matpath {
            matpath.to_string()
        } else {
            self.name.clone()
        }
    }
}

impl Persistent for HostGroup {
    /// Returns the unique identifier.
    fn id(&self) -> Option<u64> {
        self.id
    }

    /// Returns the reference string if it's not empty.
    fn ref_(&self) -> Option<String> {
        if self.ref_.as_ref().is_some_and(|x| !x.is_empty()) {
            self.ref_.clone()
        } else {
            None
        }
    }

    /// Returns the name if it's not empty.
    fn name(&self) -> Option<String> {
        if self.name.is_empty() {
            None
        } else {
            Some(self.name.clone())
        }
    }

    fn name_regex(&self) -> Option<String> {
        Some(HOSTGROUP_NAME_REGEX_STR.to_string())
    }

    fn validated_name(&self, name: &str) -> Result<String, OpsviewConfigError> {
        validate_and_trim_hostgroup_name(name)
    }

    fn set_name(&mut self, new_name: &str) -> Result<String, OpsviewConfigError> {
        self.name = self.validated_name(new_name)?;
        Ok(self.name.clone())
    }

    fn clear_readonly(&mut self) {
        self.id = None;
        self.is_leaf = None;
        self.matpath = None;
        self.ref_ = None;
        self.uncommitted = None;
    }
}

/// Builder for creating instances of [`HostGroup`].
///
/// Provides a fluent interface for constructing a `HostGroup` object. This approach allows for
/// customizable construction, ensuring that the created object conforms to the required parameters
/// and defaults.
///
/// # Examples
/// ```
/// use opsview::config::HostGroup;
/// use opsview::prelude::*;
/// let existing_parent = HostGroup::minimal("Opsview").unwrap();
///
/// let builder = HostGroup::builder()
///     .name("Example Group")
///     .parent(existing_parent)
///     .build()
///     .unwrap();
///
/// assert_eq!(builder.name, "Example Group".to_string());
/// ```
#[derive(Clone, Debug, Default)]
pub struct HostGroupBuilder {
    children: Option<ConfigRefMap<HostGroupRef>>,
    hosts: Option<ConfigRefMap<HostRef>>,
    name: Option<String>,
    parent: Option<HostGroupRef>,
}

impl Builder for HostGroupBuilder {
    type ConfigObject = HostGroup;

    /// Initializes a new builder for creating a [`HostGroup`] object with all fields in their default
    /// state.
    ///
    /// # Returns
    /// A new instance of [`HostGroupBuilder`].
    fn new() -> Self {
        HostGroupBuilder::default()
    }

    /// Sets the name field.
    ///
    /// # Arguments
    /// * `name` - The name of the `HostGroup`.
    fn name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    /// Completes the construction of a [`HostGroup`] object.
    ///
    /// Validates the current state of the [`HostGroupBuilder`] and assembles a `HostGroup` object.
    ///
    /// # Errors
    /// Returns an error if the name is not set.
    ///
    /// # Returns
    /// On success, returns a `Result` containing the constructed `HostGroup` object.
    /// On failure, returns an error detailing the missing field or inconsistency.
    fn build(self) -> Result<Self::ConfigObject, OpsviewConfigError> {
        let name = require_field(&self.name, "name")?;

        Ok(HostGroup {
            name: validate_and_trim_hostgroup_name(&name)?,
            children: self.children,
            parent: self.parent,
            hosts: self.hosts,
            matpath: None,
            id: None,
            is_leaf: None,
            ref_: None,
            uncommitted: None,
        })
    }
}

impl HostGroupBuilder {
    /// Sets the children field.
    ///
    /// # Arguments
    /// * `children` - The children of the `HostGroup` as a collection of `HostGroup` objects.
    pub fn children(mut self, children: &ConfigObjectMap<HostGroup>) -> Self {
        self.children = Some(children.into());
        self
    }

    /// Clears the children field.
    pub fn clear_children(mut self) -> Self {
        self.children = None;
        self
    }

    /// Clears the hosts field.
    pub fn clear_hosts(mut self) -> Self {
        self.hosts = None;
        self
    }

    /// Clears the name field.
    pub fn clear_name(mut self) -> Self {
        self.name = None;
        self
    }

    /// Clears the parent field.
    pub fn clear_parent(mut self) -> Self {
        self.parent = None;
        self
    }

    /// Sets the hosts field.
    ///
    /// # Arguments
    /// * `hosts` - A reference to a [`ConfigObjectMap`] of [`Host`] objects that belong to this `HostGroup`.
    pub fn hosts(mut self, hosts: &ConfigObjectMap<Host>) -> Self {
        self.hosts = Some(hosts.into());
        self
    }

    /// Sets the parent field.
    ///
    /// # Arguments
    /// * `parent` - The parent of the `HostGroup`.
    pub fn parent(mut self, parent: HostGroup) -> Self {
        self.parent = Some(HostGroupRef::from(parent));
        self
    }
}

/// A reference version of [`HostGroup`] that is used when passing or retrieving a [`HostGroup`]
/// object as part of another object.
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct HostGroupRef {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    matpath: Option<String>,
    #[serde(
        rename = "ref",
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_readonly",
        default
    )]
    ref_: Option<String>,
}

/// Enables the creation of a [`HostGroupRef`] instance from a JSON representation.
/// Typically used when parsing JSON data from the Opsview API.
impl CreateFromJson for HostGroupRef {}

impl ConfigRef for HostGroupRef {
    type FullObject = HostGroup;

    /// Returns the reference string of the [`HostGroupRef`] object.
    fn ref_(&self) -> Option<String> {
        self.ref_.clone()
    }

    /// Returns the name of the [`HostGroupRef`] object.
    fn name(&self) -> String {
        self.name.clone()
    }

    /// Returns the unique name of the [`HostGroupRef`] object.
    /// This name is used to identify the `HostGroupRef` when building the `HashMap` for a
    /// [`ConfigRefMap`].
    fn unique_name(&self) -> String {
        if let Some(matpath) = &self.matpath {
            matpath.to_string()
        } else {
            self.name.clone()
        }
    }
}

impl PersistentMap for ConfigObjectMap<HostGroup> {
    fn config_path() -> Option<String> {
        Some("/config/hostgroup".to_string())
    }
}

impl From<HostGroup> for HostGroupRef {
    fn from(hostgroup: HostGroup) -> Self {
        HostGroupRef {
            name: hostgroup.name.clone(),
            matpath: hostgroup.matpath.clone(),
            ref_: hostgroup.ref_.clone(),
        }
    }
}

impl From<Arc<HostGroup>> for HostGroupRef {
    fn from(item: Arc<HostGroup>) -> Self {
        let hostgroup: HostGroup = Arc::try_unwrap(item).unwrap_or_else(|arc| (*arc).clone());
        HostGroupRef::from(hostgroup)
    }
}

impl From<&ConfigObjectMap<HostGroup>> for ConfigRefMap<HostGroupRef> {
    fn from(host_groups: &ConfigObjectMap<HostGroup>) -> Self {
        ref_map_from(host_groups)
    }
}

impl HostGroupRef {
    /// Returns the materialized path of the [`HostGroupRef`].
    pub fn matpath(&self) -> Option<String> {
        self.matpath.clone()
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_default() {
        let hostgroup = HostGroup::default();

        assert!(hostgroup.name.is_empty());
    }

    #[test]
    fn test_minimal() {
        let hostgroup = HostGroup::minimal("My HostGroup");

        assert_eq!(hostgroup.unwrap().name, "My HostGroup".to_string());
    }

    #[test]
    fn test_is_valid_hostgroup_name() {
        // Test valid names
        assert!(validate_and_trim_hostgroup_name("Host Group 1").is_ok());
        assert!(validate_and_trim_hostgroup_name("Another-Valid_HostGroup/Name+123").is_ok());
        assert!(validate_and_trim_hostgroup_name(&"A".repeat(128)).is_ok());

        // Test invalid names
        assert!(validate_and_trim_hostgroup_name("").is_err()); // Empty name
        assert!(validate_and_trim_hostgroup_name(&"A".repeat(129)).is_err());
        assert!(validate_and_trim_hostgroup_name("//foo").is_err());
        assert!(validate_and_trim_hostgroup_name("/").is_err());
    }
}