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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use super::{BSMComponent, BSMComponentRef, MonitoringCluster, MonitoringClusterRef};
use crate::{prelude::*, util::*};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Represents a [Business Service Management (BSM)](https://docs.itrsgroup.com/docs/opsview/current/monitoring/business-service-monitoring/business-service-monitoring/index.html) service in Opsview.
///
/// The `BSMService` struct defines the structure for a [BSM
/// Service](https://docs.itrsgroup.com/docs/opsview/current/monitoring/business-service-monitoring/create-business-service/index.html#create-a-business-service)
/// entity as used in Opsview. BSM services are used to group components and represent higher-level
/// business services for monitoring purposes.
///
/// # Example
/// ```rust
/// use opsview::config::{BSMComponent, BSMService, Host, HostGroup, HostTemplate, MonitoringCluster};
/// use opsview::prelude::*;
///
/// let host_template = HostTemplate::minimal("My Host Template")
///     .expect("Failed to create a minimal HostTemplate with the name 'My Host Template'");
///
/// let mut host_templates = ConfigObjectMap::<HostTemplate>::new();
///
/// host_templates.add(host_template.clone());
///
/// // Shadow the host_template variable to make it immutable.
/// let host_templates = host_templates;
///
/// let parent_group = HostGroup::minimal("Opsview")
///     .expect("Failed to create a minimal HostGroup with the name 'Opsview'");
///
/// let host_group = HostGroup::builder()
///   .name("My Host Group")
///   .parent(parent_group)
///   .build()
///   .unwrap();
///
/// let cluster_1 = MonitoringCluster::minimal("My Monitoring Cluster")
///     .expect("Failed to create a minimal MonitoringCluster with the name 'My Monitoring Cluster'");
///
/// let host = Host::builder()
///   .name("My_Host")
///   .alias("My Host")
///   .ip("127.0.0.1")
///   .hostgroup(host_group)
///   .hosttemplates(&host_templates)
///   .monitored_by(cluster_1)
///   .build()
///   .unwrap();
///
/// let mut hosts = ConfigObjectMap::<Host>::new();
/// hosts.add(host);;
///
/// // Shadow the hosts variable to make it immutable.
/// let hosts = hosts;
///
/// let bsm_component = BSMComponent::builder()
///  .name("My BSM Component")
///  .host_template(host_template)
///  .hosts(&hosts)
///  .quorum_pct("100.00")
///  .build()
///  .unwrap();
///
///  let mut components = ConfigObjectMap::<BSMComponent>::new();
///  components.add(bsm_component);
///
///  // Shadow the components variable to make it immutable.
///  let components = components;
///
/// let bsm_service = BSMService::builder()
///  .name("My BSM Service")
///  .components(&components)
///  .build()
///  .unwrap();
///
/// assert_eq!(bsm_service.name, "My BSM Service".to_string());
/// ```
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct BSMService {
    // Required fields ---------------------------------------------------------------------------//
    /// The name of the BSMService.
    pub name: String,

    // Semi-optional fields ----------------------------------------------------------------------//
    /// A collection of [`BSMComponent`] objects that are part of this service.
    #[serde(alias = "business_components")]
    pub components: Option<ConfigRefMap<BSMComponentRef>>,

    // Optional fields ---------------------------------------------------------------------------//
    /// A reference to the [`MonitoringCluster`] which will handle the notifications for the
    /// service.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub monitoring_cluster: Option<MonitoringClusterRef>,

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

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

    /// A boolean indicating whether the BSMService 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 [`BSMService`] instance from a JSON representation.
/// Typically used when parsing JSON data from the Opsview API.
impl CreateFromJson for BSMService {}

impl ConfigObject for BSMService {
    type Builder = BSMServiceBuilder;

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

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

    /// Returns a minimal `BSMService` object with only the name set.
    ///
    /// # Arguments
    /// * `name` - Name of the [`BSMService`].
    ///
    /// # Returns
    /// A minimal `BSMService` 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_bsmservice_name(name)?,
            ..Default::default()
        })
    }

    /// Returns the unique name of the [`BSMService`] object.
    ///
    /// This name is used to identify the `BSMService` when building the `HashMap` for a
    /// [`ConfigObjectMap`].
    ///
    /// Since names are not required to be unique for BSMService objects in Opsview, we have to
    /// add the id at the end of the string, if it's present.
    ///
    /// If the id is not present, but the ref_ is, use the ref_ instead as is.
    ///
    /// # Returns
    /// A string representing the unique name of the BSM service.
    fn unique_name(&self) -> String {
        let name = self.name.clone();
        match (self.id.as_ref(), self.ref_.as_ref()) {
            (Some(id), _) => format!("{}-{}", name, id),
            (_, Some(ref_)) => ref_.clone(),
            _ => name,
        }
    }
}

impl Persistent for BSMService {
    /// 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(INLINE_FREE_TEXT_REGEX_STR.to_string())
    }

    fn validated_name(&self, name: &str) -> Result<String, OpsviewConfigError> {
        validate_and_trim_bsmservice_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.ref_ = None;
        self.uncommitted = None;
    }
}

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

/// Builder for creating instances of [`BSMService`].
///
/// Provides a fluent interface for constructing a `BSMService` object with optional fields.
#[derive(Clone, Debug, Default)]
pub struct BSMServiceBuilder {
    name: Option<String>,
    components: Option<ConfigRefMap<BSMComponentRef>>,
    monitoring_cluster: Option<MonitoringClusterRef>,
}

impl Builder for BSMServiceBuilder {
    type ConfigObject = BSMService;

    /// Creates a new instance of [`BSMServiceBuilder`] with default values.
    ///
    /// Initializes a new builder for creating a [`BSMService`] object with all fields in their
    /// default state.
    fn new() -> Self {
        BSMServiceBuilder::default()
    }

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

    /// Builds the [`BSMService`] object with the specified properties.
    ///
    /// Constructs a new `BSMService` object based on the current state of the builder.
    /// Returns an error if any required field is not set.
    ///
    /// # Returns
    /// A `Result` containing the constructed `BSMService` object or an error if the object
    /// could not be built due to missing required fields.
    fn build(self) -> Result<Self::ConfigObject, OpsviewConfigError> {
        let name = require_field(&self.name, "name")?;
        let components = require_field(&self.components, "components")?;

        Ok(BSMService {
            name: validate_and_trim_bsmservice_name(&name)?,
            components: Some(components),
            monitoring_cluster: self.monitoring_cluster,
            id: None,
            ref_: None,
            uncommitted: None,
        })
    }
}

impl BSMServiceBuilder {
    /// Clears the components field.
    pub fn clear_components(mut self) -> Self {
        self.components = None;
        self
    }

    /// Clears the `monitoring_cluster` field.
    pub fn clear_monitoring_cluster(mut self) -> Self {
        self.monitoring_cluster = None;
        self
    }

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

    /// Sets the components field.
    ///
    /// # Arguments
    /// * `components` - Collection of `BSMComponent` objects for the service.
    pub fn components(mut self, components: &ConfigObjectMap<BSMComponent>) -> Self {
        self.components = Some(components.into());
        self
    }

    /// A fluent method that sets the `monitoring_cluster` field and returns Self, allowing for
    /// method chaining.
    ///
    /// # Arguments
    /// * `monitoring_cluster` - [`MonitoringCluster`] which will handle the notifications for the service.
    pub fn monitoring_cluster(mut self, monitoring_cluster: MonitoringCluster) -> Self {
        self.monitoring_cluster = Some(MonitoringClusterRef::from(monitoring_cluster));
        self
    }
}

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

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

impl ConfigRef for BSMServiceRef {
    type FullObject = BSMService;

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

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

    /// Returns the unique name of the [`BSMServiceRef`].
    ///
    /// This name is used to identify the `BSMServiceRef` when building the `HashMap` for a
    /// [`ConfigRefMap`].
    ///
    /// If the name is not present, but the ref_ is, use the ref_ instead as is.
    ///
    /// If neither is present, use the name and hope for the best. // TODO: Investigate a better approach.
    ///
    /// # Returns
    /// A string representing the unique name of the [`BSMServiceRef`].
    fn unique_name(&self) -> String {
        let name = self.name.clone();
        match self.ref_.as_ref() {
            Some(ref_) => ref_.clone(),
            _ => name,
        }
    }
}

impl From<BSMService> for BSMServiceRef {
    fn from(full_object: BSMService) -> Self {
        Self {
            name: full_object.name.clone(),
            ref_: full_object.ref_.clone(),
        }
    }
}

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

impl From<&ConfigObjectMap<BSMService>> for ConfigRefMap<BSMServiceRef> {
    fn from(services: &ConfigObjectMap<BSMService>) -> Self {
        ref_map_from(services)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default() {
        let service = BSMService::default();

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

    #[test]
    fn test_minimal() {
        let service = BSMService::minimal("My BSM Service");

        assert_eq!(service.unwrap().name, "My BSM Service".to_string());
    }

    #[test]
    fn test_is_valid_bsmservice_name() {
        // Test valid names
        assert!(validate_and_trim_bsmservice_name("Valid Service Name").is_ok());
        assert!(validate_and_trim_bsmservice_name("Another-Valid_Service.Name123").is_ok());
        assert!(validate_and_trim_bsmservice_name(&"A".repeat(255)).is_ok()); // Max length

        // Test invalid names
        assert!(validate_and_trim_bsmservice_name("").is_err()); // Empty name
        assert!(validate_and_trim_bsmservice_name(&"A".repeat(256)).is_err());
        // Exceeds max length
    }
}