switch id model to this
the wakey-core id model is (seemingly) ephemeral
This commit is contained in:
@@ -103,7 +103,7 @@ const fn presence_rank(presence: Presence) -> u8 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use wakey_core::{DhcpLease, InventoryQueryBuilder, NeighborState};
|
||||
use wakey_core::{DeviceId, DhcpLease, InventoryQueryBuilder, NeighborState};
|
||||
|
||||
fn sample_neighbors() -> Vec<NeighborEntry> {
|
||||
vec![NeighborEntry {
|
||||
@@ -154,4 +154,25 @@ mod tests {
|
||||
let out = merge_devices(sample_neighbors(), sample_leases(), &query);
|
||||
assert_eq!(out.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merge_devices_uses_ip_observed_id_when_mac_is_absent() {
|
||||
let query = InventoryQueryBuilder::new().build();
|
||||
let out = merge_devices(
|
||||
vec![NeighborEntry {
|
||||
ip: IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20)),
|
||||
dev: Some("br-lan".to_string()),
|
||||
mac: None,
|
||||
state: NeighborState::Stale,
|
||||
}],
|
||||
Vec::new(),
|
||||
&query,
|
||||
);
|
||||
|
||||
assert_eq!(out.len(), 1);
|
||||
assert_eq!(
|
||||
out[0].id,
|
||||
Some(DeviceId::Ip(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 20))))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use macaddr::MacAddr;
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_with::skip_serializing_none;
|
||||
use std::net::IpAddr;
|
||||
|
||||
@@ -32,11 +32,16 @@ impl From<NeighborState> for Presence {
|
||||
}
|
||||
}
|
||||
|
||||
/// MAC-first identifier for a device aggregate.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize)]
|
||||
pub struct DeviceId {
|
||||
/// Observed identifier for a discovered device aggregate.
|
||||
///
|
||||
/// This is not a durable, user-approved identity. The control plane may attach
|
||||
/// many observed identifiers to one saved device.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
|
||||
pub enum DeviceId {
|
||||
#[serde(with = "mac")]
|
||||
pub mac: MacAddr,
|
||||
Mac(MacAddr),
|
||||
Ip(IpAddr),
|
||||
}
|
||||
|
||||
/// Merged view of one discovered network identity.
|
||||
@@ -91,10 +96,16 @@ impl Device {
|
||||
}
|
||||
|
||||
let macs: Vec<MacAddr> = macs.into_iter().collect();
|
||||
let ips: Vec<IpAddr> = ips.into_iter().collect();
|
||||
let id = macs
|
||||
.first()
|
||||
.copied()
|
||||
.map(DeviceId::Mac)
|
||||
.or_else(|| ips.first().copied().map(DeviceId::Ip));
|
||||
Self {
|
||||
id: macs.first().copied().map(|mac| DeviceId { mac }),
|
||||
id,
|
||||
names: names.into_iter().map(|n| n.to_owned()).collect(),
|
||||
ips: ips.into_iter().collect(),
|
||||
ips,
|
||||
macs,
|
||||
interfaces: interfaces.into_iter().map(|d| d.to_owned()).collect(),
|
||||
neighbors,
|
||||
|
||||
Reference in New Issue
Block a user