use crate::mac_address::MacAddress;
use crate::schema::redfish::network_device_function::NetworkDeviceFunction as NetworkDeviceFunctionSchema;
use crate::schema::redfish::network_device_function_collection::NetworkDeviceFunctionCollection as NetworkDeviceFunctionCollectionSchema;
use crate::Error;
use crate::NvBmc;
use crate::Resource;
use crate::ResourceSchema;
use nv_redfish_core::Bmc;
use nv_redfish_core::NavProperty;
use std::marker::PhantomData;
use std::sync::Arc;
pub struct NetworkDeviceFunctionCollection<B: Bmc> {
bmc: NvBmc<B>,
collection: Arc<NetworkDeviceFunctionCollectionSchema>,
}
impl<B: Bmc> NetworkDeviceFunctionCollection<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<NetworkDeviceFunctionCollectionSchema>,
) -> Result<Self, Error<B>> {
let collection = bmc.expand_property(nav).await?;
Ok(Self {
bmc: bmc.clone(),
collection,
})
}
pub async fn members(&self) -> Result<Vec<NetworkDeviceFunction<B>>, Error<B>> {
let mut members = Vec::new();
for m in &self.collection.members {
members.push(NetworkDeviceFunction::new(&self.bmc, m).await?);
}
Ok(members)
}
}
pub struct NetworkDeviceFunction<B: Bmc> {
data: Arc<NetworkDeviceFunctionSchema>,
_marker: PhantomData<B>,
}
impl<B: Bmc> NetworkDeviceFunction<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<NetworkDeviceFunctionSchema>,
) -> Result<Self, Error<B>> {
nav.get(bmc.as_ref())
.await
.map_err(crate::Error::Bmc)
.map(|data| Self {
data,
_marker: PhantomData,
})
}
#[must_use]
pub fn raw(&self) -> Arc<NetworkDeviceFunctionSchema> {
self.data.clone()
}
pub fn ethernet_permanent_mac_address(&self) -> Option<MacAddress<'_>> {
self.data
.ethernet
.as_ref()
.and_then(|eth| eth.permanent_mac_address.as_ref())
.and_then(Option::as_deref)
.map(MacAddress::new)
}
}
impl<B: Bmc> Resource for NetworkDeviceFunction<B> {
fn resource_ref(&self) -> &ResourceSchema {
&self.data.as_ref().base
}
}