use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::types::component::ComponentDescription;
use crate::types::provider::ProviderDescription;
use crate::Result;
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct Host {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) rpc_host: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) ctl_host: Option<String>,
#[serde(default)]
pub(crate) friendly_name: String,
#[serde(default)]
pub(crate) id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) js_domain: Option<String>,
#[serde(default)]
pub(crate) labels: BTreeMap<String, String>,
#[serde(default)]
pub(crate) lattice: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) uptime_human: Option<String>,
#[serde(default)]
pub(crate) uptime_seconds: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) version: Option<String>,
}
impl Host {
pub fn rpc_host(&self) -> Option<&str> {
self.rpc_host.as_deref()
}
pub fn ctl_host(&self) -> Option<&str> {
self.ctl_host.as_deref()
}
pub fn friendly_name(&self) -> &str {
&self.friendly_name
}
pub fn id(&self) -> &str {
&self.id
}
pub fn js_domain(&self) -> Option<&str> {
self.js_domain.as_deref()
}
pub fn labels(&self) -> &BTreeMap<String, String> {
&self.labels
}
pub fn lattice(&self) -> &str {
&self.lattice
}
pub fn uptime_human(&self) -> Option<&str> {
self.uptime_human.as_deref()
}
pub fn uptime_seconds(&self) -> u64 {
self.uptime_seconds
}
pub fn version(&self) -> Option<&str> {
self.version.as_deref()
}
#[must_use]
pub fn builder() -> HostBuilder {
HostBuilder::default()
}
}
#[derive(Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct HostBuilder {
rpc_host: Option<String>,
ctl_host: Option<String>,
friendly_name: Option<String>,
id: Option<String>,
js_domain: Option<String>,
labels: Option<BTreeMap<String, String>>,
lattice: Option<String>,
uptime_human: Option<String>,
uptime_seconds: Option<u64>,
version: Option<String>,
}
impl HostBuilder {
#[must_use]
pub fn rpc_host(mut self, v: String) -> Self {
self.rpc_host = Some(v);
self
}
#[must_use]
pub fn ctl_host(mut self, v: String) -> Self {
self.ctl_host = Some(v);
self
}
#[must_use]
pub fn friendly_name(mut self, v: String) -> Self {
self.friendly_name = Some(v);
self
}
#[must_use]
pub fn id(mut self, v: String) -> Self {
self.id = Some(v);
self
}
#[must_use]
pub fn js_domain(mut self, v: String) -> Self {
self.js_domain = Some(v);
self
}
#[must_use]
pub fn lattice(mut self, v: String) -> Self {
self.lattice = Some(v);
self
}
#[must_use]
pub fn labels(mut self, v: BTreeMap<String, String>) -> Self {
self.labels = Some(v);
self
}
#[must_use]
pub fn uptime_human(mut self, v: String) -> Self {
self.uptime_human = Some(v);
self
}
#[must_use]
pub fn uptime_seconds(mut self, v: u64) -> Self {
self.uptime_seconds = Some(v);
self
}
#[must_use]
pub fn version(mut self, v: String) -> Self {
self.version = Some(v);
self
}
pub fn build(self) -> Result<Host> {
Ok(Host {
friendly_name: self
.friendly_name
.ok_or_else(|| "friendly_name is required".to_string())?,
labels: self.labels.unwrap_or_default(),
uptime_human: self.uptime_human,
uptime_seconds: self
.uptime_seconds
.ok_or_else(|| "uptime_seconds is required".to_string())?,
rpc_host: self.rpc_host,
ctl_host: self.ctl_host,
id: self.id.ok_or_else(|| "id is required".to_string())?,
lattice: self
.lattice
.ok_or_else(|| "lattice is required".to_string())?,
js_domain: self.js_domain,
version: self.version,
})
}
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HostInventory {
#[serde(alias = "actors")]
pub(crate) components: Vec<ComponentDescription>,
pub(crate) providers: Vec<ProviderDescription>,
#[serde(default)]
pub(crate) host_id: String,
#[serde(default)]
pub(crate) friendly_name: String,
#[serde(default)]
pub(crate) labels: BTreeMap<String, String>,
#[serde(default)]
pub(crate) version: String,
#[serde(default)]
pub(crate) uptime_human: String,
#[serde(default)]
pub(crate) uptime_seconds: u64,
}
impl HostInventory {
pub fn components(&self) -> &Vec<ComponentDescription> {
self.components.as_ref()
}
pub fn providers(&self) -> &Vec<ProviderDescription> {
&self.providers
}
pub fn host_id(&self) -> &str {
&self.host_id
}
pub fn friendly_name(&self) -> &str {
&self.friendly_name
}
pub fn labels(&self) -> &BTreeMap<String, String> {
&self.labels
}
pub fn version(&self) -> &str {
&self.version
}
pub fn uptime_human(&self) -> &str {
&self.uptime_human
}
pub fn uptime_seconds(&self) -> u64 {
self.uptime_seconds
}
#[must_use]
pub fn builder() -> HostInventoryBuilder {
HostInventoryBuilder::default()
}
}
#[derive(Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct HostInventoryBuilder {
components: Option<Vec<ComponentDescription>>,
providers: Option<Vec<ProviderDescription>>,
host_id: Option<String>,
friendly_name: Option<String>,
labels: Option<BTreeMap<String, String>>,
version: Option<String>,
uptime_human: Option<String>,
uptime_seconds: Option<u64>,
}
impl HostInventoryBuilder {
#[must_use]
pub fn friendly_name(mut self, v: String) -> Self {
self.friendly_name = Some(v);
self
}
#[must_use]
pub fn host_id(mut self, v: String) -> Self {
self.host_id = Some(v);
self
}
#[must_use]
pub fn version(mut self, v: String) -> Self {
self.version = Some(v);
self
}
#[must_use]
pub fn components(mut self, v: Vec<ComponentDescription>) -> Self {
self.components = Some(v);
self
}
#[must_use]
pub fn providers(mut self, v: Vec<ProviderDescription>) -> Self {
self.providers = Some(v);
self
}
#[must_use]
pub fn uptime_human(mut self, v: String) -> Self {
self.uptime_human = Some(v);
self
}
#[must_use]
pub fn uptime_seconds(mut self, v: u64) -> Self {
self.uptime_seconds = Some(v);
self
}
#[must_use]
pub fn labels(mut self, v: BTreeMap<String, String>) -> Self {
self.labels = Some(v);
self
}
pub fn build(self) -> Result<HostInventory> {
Ok(HostInventory {
components: self.components.unwrap_or_default(),
providers: self.providers.unwrap_or_default(),
host_id: self
.host_id
.ok_or_else(|| "host_id is required".to_string())?,
friendly_name: self
.friendly_name
.ok_or_else(|| "friendly_name is required".to_string())?,
labels: self.labels.unwrap_or_default(),
version: self
.version
.ok_or_else(|| "version is required".to_string())?,
uptime_human: self
.uptime_human
.ok_or_else(|| "uptime_human is required".to_string())?,
uptime_seconds: self
.uptime_seconds
.ok_or_else(|| "uptime_seconds is required".to_string())?,
})
}
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HostLabel {
pub(crate) key: String,
pub(crate) value: String,
}
impl HostLabel {
pub fn from_kv(key: &str, value: &str) -> Self {
Self {
key: key.into(),
value: value.into(),
}
}
pub fn key(&self) -> &str {
&self.key
}
pub fn value(&self) -> &str {
&self.value
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use crate::{ComponentDescription, ProviderDescription};
use super::{Host, HostInventory};
#[test]
fn host_builder() {
assert_eq!(
Host {
rpc_host: Some("rpc_host".into()),
ctl_host: Some("ctl_host".into()),
friendly_name: "friendly_name".into(),
id: "id".into(),
js_domain: Some("js_domain".into()),
labels: BTreeMap::from([("a".into(), "b".into())]),
lattice: "lattice".into(),
uptime_human: Some("t".into()),
uptime_seconds: 1,
version: Some("1.0.0".into()),
},
Host::builder()
.rpc_host("rpc_host".into())
.ctl_host("ctl_host".into())
.friendly_name("friendly_name".into())
.id("id".into())
.js_domain("js_domain".into())
.labels(BTreeMap::from([("a".into(), "b".into())]))
.lattice("lattice".into())
.uptime_human("t".into())
.uptime_seconds(1)
.version("1.0.0".into())
.build()
.unwrap()
)
}
#[test]
fn host_inventory_builder() {
assert_eq!(
HostInventory {
components: Vec::from([ComponentDescription::default()]),
providers: Vec::from([ProviderDescription::default()]),
host_id: "host_id".into(),
friendly_name: "friendly_name".into(),
labels: BTreeMap::from([("a".into(), "b".into())]),
version: "1.0.0".into(),
uptime_human: "t".into(),
uptime_seconds: 1
},
HostInventory::builder()
.components(Vec::from([ComponentDescription::default()]))
.providers(Vec::from([ProviderDescription::default()]))
.host_id("host_id".into())
.friendly_name("friendly_name".into())
.labels(BTreeMap::from([("a".into(), "b".into())]))
.version("1.0.0".into())
.uptime_human("t".into())
.uptime_seconds(1)
.build()
.unwrap()
)
}
}