use crate::client::OpsviewClient;
use crate::{config::*, prelude::*};
use serde::{Deserialize, Serialize};
use tokio::join;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpsviewInstance {
pub bsm_components: ConfigObjectMap<BSMComponent>,
pub bsm_services: ConfigObjectMap<BSMService>,
pub contacts: ConfigObjectMap<Contact>,
pub host_check_commands: ConfigObjectMap<HostCheckCommand>,
pub host_groups: ConfigObjectMap<HostGroup>,
pub host_templates: ConfigObjectMap<HostTemplate>,
pub hosts: ConfigObjectMap<Host>,
pub monitoring_clusters: ConfigObjectMap<MonitoringCluster>,
pub netflow_collectors: ConfigObjectMap<NetflowCollector>,
pub netflow_sources: ConfigObjectMap<NetflowSource>,
pub notification_methods: ConfigObjectMap<NotificationMethod>,
pub plugins: ConfigObjectMap<Plugin>,
pub service_checks: ConfigObjectMap<ServiceCheck>,
pub service_groups: ConfigObjectMap<ServiceGroup>,
pub shared_notification_profiles: ConfigObjectMap<SharedNotificationProfile>,
pub tenancies: ConfigObjectMap<Tenancy>,
pub time_periods: ConfigObjectMap<TimePeriod>,
pub variables: ConfigObjectMap<Variable>,
}
impl Default for OpsviewInstance {
fn default() -> Self {
OpsviewInstance {
bsm_components: ConfigObjectMap::<BSMComponent>::new(),
bsm_services: ConfigObjectMap::<BSMService>::new(),
contacts: ConfigObjectMap::<Contact>::new(),
host_check_commands: ConfigObjectMap::<HostCheckCommand>::new(),
host_groups: ConfigObjectMap::<HostGroup>::new(),
host_templates: ConfigObjectMap::<HostTemplate>::new(),
hosts: ConfigObjectMap::<Host>::new(),
monitoring_clusters: ConfigObjectMap::<MonitoringCluster>::new(),
netflow_collectors: ConfigObjectMap::<NetflowCollector>::new(),
netflow_sources: ConfigObjectMap::<NetflowSource>::new(),
notification_methods: ConfigObjectMap::<NotificationMethod>::new(),
plugins: ConfigObjectMap::<Plugin>::new(),
service_checks: ConfigObjectMap::<ServiceCheck>::new(),
service_groups: ConfigObjectMap::<ServiceGroup>::new(),
shared_notification_profiles: ConfigObjectMap::<SharedNotificationProfile>::new(),
tenancies: ConfigObjectMap::<Tenancy>::new(),
time_periods: ConfigObjectMap::<TimePeriod>::new(),
variables: ConfigObjectMap::<Variable>::new(),
}
}
}
impl OpsviewInstance {
pub async fn refresh(
self,
client: &OpsviewClient,
) -> Result<OpsviewInstance, OpsviewClientError> {
let bsm_components_future = client.get_all_bsmcomponent_configs(None);
let bsm_services_future = client.get_all_bsmservice_configs(None);
let contacts_future = client.get_all_contact_configs(None);
let host_check_commands_future = client.get_all_hostcheckcommand_configs(None);
let host_groups_future = client.get_all_hostgroup_configs(None);
let host_templates_future = client.get_all_hosttemplate_configs(None);
let hosts_future = client.get_all_host_configs(None);
let monitoring_clusters_future = client.get_all_monitoringcluster_configs(None);
let netflow_collectors_future = client.get_all_netflowcollector_configs(None);
let netflow_sources_future = client.get_all_netflowsource_configs(None);
let notification_methods_future = client.get_all_notificationmethod_configs(None);
let plugins_future = client.get_all_plugin_configs(None);
let service_checks_future = client.get_all_servicecheck_configs(None);
let service_groups_future = client.get_all_servicegroup_configs(None);
let shared_notification_profiles_future =
client.get_all_sharednotificationprofile_configs(None);
let tenancies_future = client.get_all_tenancy_configs(None);
let time_periods_future = client.get_all_timeperiod_configs(None);
let variables_future = client.get_all_variable_configs(None);
let (
bsm_components,
bsm_services,
contacts,
host_check_commands,
host_groups,
host_templates,
hosts,
monitoring_clusters,
netflow_collectors,
netflow_sources,
notification_methods,
plugins,
service_checks,
service_groups,
shared_notification_profiles,
tenancies,
time_periods,
variables,
) = join!(
bsm_components_future,
bsm_services_future,
contacts_future,
host_check_commands_future,
host_groups_future,
host_templates_future,
hosts_future,
monitoring_clusters_future,
netflow_collectors_future,
netflow_sources_future,
notification_methods_future,
plugins_future,
service_checks_future,
service_groups_future,
shared_notification_profiles_future,
tenancies_future,
time_periods_future,
variables_future,
);
Ok(OpsviewInstance {
bsm_components: bsm_components.unwrap(),
bsm_services: bsm_services.unwrap(),
contacts: contacts.unwrap(),
host_check_commands: host_check_commands.unwrap(),
host_groups: host_groups.unwrap(),
host_templates: host_templates.unwrap(),
hosts: hosts.unwrap(),
monitoring_clusters: monitoring_clusters.unwrap(),
netflow_collectors: netflow_collectors.unwrap(),
netflow_sources: netflow_sources.unwrap(),
notification_methods: notification_methods.unwrap(),
plugins: plugins.unwrap(),
service_checks: service_checks.unwrap(),
service_groups: service_groups.unwrap(),
shared_notification_profiles: shared_notification_profiles.unwrap(),
tenancies: tenancies.unwrap(),
time_periods: time_periods.unwrap(),
variables: variables.unwrap(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[ignore]
#[tokio::test]
async fn test_refresh() -> Result<(), OpsviewClientError> {
use std::env;
if env::var("OV_URL").is_err() {
return Ok(());
}
if env::var("OV_USERNAME").is_err() {
return Ok(());
}
if env::var("OV_PASSWORD").is_err() {
return Ok(());
}
let start_time = std::time::Instant::now();
let client = OpsviewClient::builder()
.url(std::env::var("OV_URL").unwrap().as_ref())
.username(std::env::var("OV_USERNAME").unwrap().as_ref())
.password(std::env::var("OV_PASSWORD").unwrap().as_ref())
.ignore_cert(true)
.build()
.await?;
let instance = OpsviewInstance::default();
let instance = instance.refresh(&client).await?;
println!("OpsviewInstance data:");
println!("BSM Components: {}", instance.bsm_components.len());
println!("BSM Services: {}", instance.bsm_services.len());
println!("Contacts: {}", instance.contacts.len());
println!(
"Host Check Commands: {}",
instance.host_check_commands.len()
);
println!("Host Groups: {}", instance.host_groups.len());
println!("Host Templates: {}", instance.host_templates.len());
println!("Hosts: {}", instance.hosts.len());
println!(
"Monitoring Clusters: {}",
instance.monitoring_clusters.len()
);
println!("Netflow Collectors: {}", instance.netflow_collectors.len());
println!("Netflow Sources: {}", instance.netflow_sources.len());
println!(
"Notification Methods: {}",
instance.notification_methods.len()
);
println!("Plugins: {}", instance.plugins.len());
println!("Service Checks: {}", instance.service_checks.len());
println!("Service Groups: {}", instance.service_groups.len());
println!(
"Shared Notification Profiles: {}",
instance.shared_notification_profiles.len()
);
println!("Tenancies: {}", instance.tenancies.len());
println!("Time Periods: {}", instance.time_periods.len());
println!("Variables: {}", instance.variables.len());
let elapsed = start_time.elapsed();
println!("Elapsed: {:?}", elapsed);
client.logout().await?;
Ok(())
}
}