use crate::mac_address::MacAddress;
use crate::schema::redfish::ethernet_interface::EthernetInterface as EthernetInterfaceSchema;
use crate::schema::redfish::ethernet_interface_collection::EthernetInterfaceCollection as EthernetInterfaceCollectionSchema;
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;
use tagged_types::TaggedType;
#[doc(inline)]
pub use crate::schema::redfish::ethernet_interface::LinkStatus;
pub struct EthernetInterfaceCollection<B: Bmc> {
bmc: NvBmc<B>,
collection: Arc<EthernetInterfaceCollectionSchema>,
}
impl<B: Bmc> EthernetInterfaceCollection<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<EthernetInterfaceCollectionSchema>,
) -> Result<Self, Error<B>> {
let collection = bmc.expand_property(nav).await?;
Ok(Self {
bmc: bmc.clone(),
collection,
})
}
pub async fn members(&self) -> Result<Vec<EthernetInterface<B>>, Error<B>> {
let mut members = Vec::new();
for m in &self.collection.members {
members.push(EthernetInterface::new(&self.bmc, m).await?);
}
Ok(members)
}
}
pub type UefiDevicePath<T> = TaggedType<T, UefiDevicePathTag>;
#[doc(hidden)]
#[derive(tagged_types::Tag)]
#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
#[capability(inner_access)]
pub enum UefiDevicePathTag {}
pub struct EthernetInterface<B: Bmc> {
data: Arc<EthernetInterfaceSchema>,
_marker: PhantomData<B>,
}
impl<B: Bmc> EthernetInterface<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<EthernetInterfaceSchema>,
) -> 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<EthernetInterfaceSchema> {
self.data.clone()
}
#[must_use]
pub fn interface_enabled(&self) -> Option<bool> {
self.data
.interface_enabled
.as_ref()
.and_then(Option::as_ref)
.copied()
}
#[must_use]
pub fn link_status(&self) -> Option<LinkStatus> {
self.data
.link_status
.as_ref()
.and_then(Option::as_ref)
.copied()
}
#[must_use]
pub fn mac_address(&self) -> Option<MacAddress<'_>> {
self.data
.mac_address
.as_ref()
.and_then(Option::as_ref)
.map(String::as_str)
.map(MacAddress::new)
}
#[must_use]
pub fn permanent_mac_address(&self) -> Option<MacAddress<'_>> {
self.data
.permanent_mac_address
.as_ref()
.and_then(Option::as_ref)
.map(String::as_str)
.map(MacAddress::new)
}
#[must_use]
pub fn uefi_device_path(&self) -> Option<UefiDevicePath<&str>> {
self.data
.uefi_device_path
.as_ref()
.and_then(Option::as_ref)
.map(String::as_str)
.map(UefiDevicePath::new)
}
}
impl<B: Bmc> Resource for EthernetInterface<B> {
fn resource_ref(&self) -> &ResourceSchema {
&self.data.as_ref().base
}
}