use std::sync::Arc;
use crate::core::client::{VimClient, Result};
#[derive(Clone)]
pub struct EventManager {
client: Arc<dyn VimClient>,
mo_id: String,
}
impl EventManager {
pub fn new(client: Arc<dyn VimClient>, mo_id: &str) -> Self {
Self {
client,
mo_id: mo_id.to_string(),
}
}
pub async fn query_events(&self, filter: &crate::types::structs::EventFilterSpec, event_view_spec: Option<&dyn crate::types::traits::EventManagerEventViewSpecTrait>) -> Result<Option<Vec<crate::types::structs::Event>>> {
let input = QueryEventsRequestType {filter, event_view_spec, };
let bytes_opt = self.client.invoke_optional("", "EventManager", &self.mo_id, "QueryEvents", Some(&input)).await?;
match bytes_opt {
Some(ref b) => Ok(Some(crate::core::client::unmarshal_array(self.client.transport(), b)?)),
None => Ok(None),
}
}
pub async fn create_collector_for_events(&self, filter: &crate::types::structs::EventFilterSpec) -> Result<crate::types::structs::ManagedObjectReference> {
let input = CreateCollectorForEventsRequestType {filter, };
let bytes = self.client.invoke("", "EventManager", &self.mo_id, "CreateCollectorForEvents", Some(&input)).await?;
let result: crate::types::structs::ManagedObjectReference = crate::core::client::unmarshal(self.client.transport(), &bytes)?;
Ok(result)
}
pub async fn log_user_event(&self, entity: &crate::types::structs::ManagedObjectReference, msg: &str) -> Result<()> {
let input = LogUserEventRequestType {entity, msg, };
self.client.invoke_void("", "EventManager", &self.mo_id, "LogUserEvent", Some(&input)).await
}
pub async fn post_event(&self, event_to_post: &crate::types::structs::Event, task_info: Option<&crate::types::structs::TaskInfo>) -> Result<()> {
let input = PostEventRequestType {event_to_post, task_info, };
self.client.invoke_void("", "EventManager", &self.mo_id, "PostEvent", Some(&input)).await
}
pub async fn retrieve_argument_description(&self, event_type_id: &str) -> Result<Option<Vec<crate::types::structs::EventArgDesc>>> {
let input = RetrieveArgumentDescriptionRequestType {event_type_id, };
let bytes_opt = self.client.invoke_optional("", "EventManager", &self.mo_id, "RetrieveArgumentDescription", Some(&input)).await?;
match bytes_opt {
Some(ref b) => Ok(Some(crate::core::client::unmarshal_array(self.client.transport(), b)?)),
None => Ok(None),
}
}
pub async fn description(&self) -> Result<crate::types::structs::EventDescription> {
let pv_opt = self.client.fetch_property_raw("", "EventManager", &self.mo_id, "description").await?;
let pv = pv_opt.ok_or_else(|| crate::core::client::VimError::ParseError("property description was empty".to_string()))?;
let result: crate::types::structs::EventDescription = crate::core::client::extract_property(pv)?;
Ok(result)
}
pub async fn latest_event(&self) -> Result<Option<crate::types::structs::Event>> {
let pv_opt = self.client.fetch_property_raw("", "EventManager", &self.mo_id, "latestEvent").await?;
match pv_opt {
Some(pv) => Ok(Some(crate::core::client::extract_property(pv)?)),
None => Ok(None),
}
}
pub async fn max_collector(&self) -> Result<i32> {
let pv_opt = self.client.fetch_property_raw("", "EventManager", &self.mo_id, "maxCollector").await?;
let pv = pv_opt.ok_or_else(|| crate::core::client::VimError::ParseError("property maxCollector was empty".to_string()))?;
let result: i32 = crate::core::client::extract_property(pv)?;
Ok(result)
}
}
struct QueryEventsRequestType<'a> {
filter: &'a crate::types::structs::EventFilterSpec,
event_view_spec: Option<&'a dyn crate::types::traits::EventManagerEventViewSpecTrait>,
}
impl<'a> miniserde::Serialize for QueryEventsRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(QueryEventsRequestTypeSer { data: self, seq: 0 }))
}
}
struct QueryEventsRequestTypeSer<'b, 'a> {
data: &'b QueryEventsRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for QueryEventsRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
loop {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"QueryEventsRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("filter"), &self.data.filter as &dyn miniserde::Serialize)),
2 => {
let Some(ref val) = self.data.event_view_spec else { continue; };
return Some((std::borrow::Cow::Borrowed("eventViewSpec"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}
struct CreateCollectorForEventsRequestType<'a> {
filter: &'a crate::types::structs::EventFilterSpec,
}
impl<'a> miniserde::Serialize for CreateCollectorForEventsRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(CreateCollectorForEventsRequestTypeSer { data: self, seq: 0 }))
}
}
struct CreateCollectorForEventsRequestTypeSer<'b, 'a> {
data: &'b CreateCollectorForEventsRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for CreateCollectorForEventsRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"CreateCollectorForEventsRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("filter"), &self.data.filter as &dyn miniserde::Serialize)),
_ => return None,
}
}
}
struct LogUserEventRequestType<'a> {
entity: &'a crate::types::structs::ManagedObjectReference,
msg: &'a str,
}
impl<'a> miniserde::Serialize for LogUserEventRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(LogUserEventRequestTypeSer { data: self, seq: 0 }))
}
}
struct LogUserEventRequestTypeSer<'b, 'a> {
data: &'b LogUserEventRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for LogUserEventRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"LogUserEventRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("entity"), &self.data.entity as &dyn miniserde::Serialize)),
2 => return Some((std::borrow::Cow::Borrowed("msg"), &self.data.msg as &dyn miniserde::Serialize)),
_ => return None,
}
}
}
struct PostEventRequestType<'a> {
event_to_post: &'a crate::types::structs::Event,
task_info: Option<&'a crate::types::structs::TaskInfo>,
}
impl<'a> miniserde::Serialize for PostEventRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(PostEventRequestTypeSer { data: self, seq: 0 }))
}
}
struct PostEventRequestTypeSer<'b, 'a> {
data: &'b PostEventRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for PostEventRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
loop {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"PostEventRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("eventToPost"), &self.data.event_to_post as &dyn miniserde::Serialize)),
2 => {
let Some(ref val) = self.data.task_info else { continue; };
return Some((std::borrow::Cow::Borrowed("taskInfo"), val as &dyn miniserde::Serialize));
}
_ => return None,
}
}
}
}
struct RetrieveArgumentDescriptionRequestType<'a> {
event_type_id: &'a str,
}
impl<'a> miniserde::Serialize for RetrieveArgumentDescriptionRequestType<'a> {
fn begin(&self) -> miniserde::ser::Fragment<'_> {
miniserde::ser::Fragment::Map(Box::new(RetrieveArgumentDescriptionRequestTypeSer { data: self, seq: 0 }))
}
}
struct RetrieveArgumentDescriptionRequestTypeSer<'b, 'a> {
data: &'b RetrieveArgumentDescriptionRequestType<'a>,
seq: usize,
}
impl<'b, 'a> miniserde::ser::Map for RetrieveArgumentDescriptionRequestTypeSer<'b, 'a> {
fn next(&mut self) -> Option<(std::borrow::Cow<'_, str>, &dyn miniserde::Serialize)> {
let seq = self.seq;
self.seq += 1;
match seq {
0 => return Some((std::borrow::Cow::Borrowed("_typeName"), &"RetrieveArgumentDescriptionRequestType")),
1 => return Some((std::borrow::Cow::Borrowed("eventTypeId"), &self.data.event_type_id as &dyn miniserde::Serialize)),
_ => return None,
}
}
}