cleaning up
This commit is contained in:
@@ -2,8 +2,7 @@ use anyhow::Result;
|
||||
use tracing::{debug, info, instrument};
|
||||
|
||||
use crate::protocol::{
|
||||
AgentCommand, CommandResult, DevsRequest, InventoryRequest, LeasesRequest, StatusRequest,
|
||||
WakeRequest,
|
||||
AgentCommand, CommandResult, DevsRequest, InventoryRequest, LeasesRequest, WakeRequest,
|
||||
};
|
||||
|
||||
#[instrument(skip_all)]
|
||||
@@ -11,7 +10,6 @@ pub async fn dispatch_command(command: AgentCommand) -> Result<CommandResult> {
|
||||
let kind = command_kind(&command);
|
||||
info!(command = %kind, "dispatching command into local wakey services");
|
||||
match command {
|
||||
AgentCommand::Status(req) => dispatch_status(req).await,
|
||||
AgentCommand::Leases(req) => dispatch_leases(req).await,
|
||||
AgentCommand::Devs(req) => dispatch_devs(req).await,
|
||||
AgentCommand::Inventory(req) => dispatch_inventory(req).await,
|
||||
@@ -19,22 +17,6 @@ pub async fn dispatch_command(command: AgentCommand) -> Result<CommandResult> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn dispatch_status(req: StatusRequest) -> Result<CommandResult> {
|
||||
let query = req.into_device_query();
|
||||
let status = if query.name.is_some()
|
||||
&& query.filter.ips.is_empty()
|
||||
&& query.filter.devs.is_empty()
|
||||
&& query.filter.nuds.is_empty()
|
||||
&& query.filter.macs.is_empty()
|
||||
{
|
||||
wakey::get_status_for_input(query.name.clone().unwrap_or_default()).await?
|
||||
} else {
|
||||
wakey::get_status(query).await?
|
||||
};
|
||||
debug!(devices = status.devices.len(), "dispatched status command");
|
||||
Ok(CommandResult::Status(status))
|
||||
}
|
||||
|
||||
async fn dispatch_leases(req: LeasesRequest) -> Result<CommandResult> {
|
||||
let leases = wakey::get_leases(wakey_core::LeaseQuery {
|
||||
include_state: req.include_state,
|
||||
@@ -90,7 +72,6 @@ async fn dispatch_wake(req: WakeRequest) -> Result<CommandResult> {
|
||||
|
||||
fn command_kind(command: &AgentCommand) -> &'static str {
|
||||
match command {
|
||||
AgentCommand::Status(_) => "status",
|
||||
AgentCommand::Leases(_) => "leases",
|
||||
AgentCommand::Devs(_) => "devs",
|
||||
AgentCommand::Inventory(_) => "inventory",
|
||||
|
||||
+35
-53
@@ -66,48 +66,6 @@ pub struct ErrorPayload {
|
||||
pub retryable: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct StatusRequest {
|
||||
pub query: Option<String>,
|
||||
pub name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub ips: Vec<IpAddr>,
|
||||
#[serde(default)]
|
||||
pub devs: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub nuds: Vec<wakey_core::NeighborState>,
|
||||
#[serde(default)]
|
||||
#[serde(with = "mac::vec_mac")]
|
||||
pub macs: Vec<MacAddr>,
|
||||
}
|
||||
|
||||
impl StatusRequest {
|
||||
pub fn into_device_query(self) -> DeviceQuery {
|
||||
if let Some(query) = self.query.as_ref()
|
||||
&& self.name.is_none()
|
||||
&& self.ips.is_empty()
|
||||
&& self.devs.is_empty()
|
||||
&& self.nuds.is_empty()
|
||||
&& self.macs.is_empty()
|
||||
{
|
||||
return DeviceQuery {
|
||||
name: Some(query.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
DeviceQuery {
|
||||
name: self.name.or(self.query),
|
||||
filter: DeviceFilters {
|
||||
ips: self.ips,
|
||||
devs: self.devs,
|
||||
nuds: self.nuds,
|
||||
macs: self.macs,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LeasesRequest {
|
||||
#[serde(default)]
|
||||
@@ -138,15 +96,41 @@ pub struct InventoryRequest {
|
||||
|
||||
impl InventoryRequest {
|
||||
pub fn into_device_query(self) -> DeviceQuery {
|
||||
StatusRequest {
|
||||
query: self.query,
|
||||
name: self.name,
|
||||
ips: self.ips,
|
||||
devs: self.devs,
|
||||
nuds: self.nuds,
|
||||
macs: self.macs,
|
||||
}
|
||||
.into_device_query()
|
||||
into_device_query(
|
||||
self.query, self.name, self.ips, self.devs, self.nuds, self.macs,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn into_device_query(
|
||||
query: Option<String>,
|
||||
name: Option<String>,
|
||||
ips: Vec<IpAddr>,
|
||||
devs: Vec<String>,
|
||||
nuds: Vec<wakey_core::NeighborState>,
|
||||
macs: Vec<MacAddr>,
|
||||
) -> DeviceQuery {
|
||||
if let Some(q) = query.as_ref()
|
||||
&& name.is_none()
|
||||
&& ips.is_empty()
|
||||
&& devs.is_empty()
|
||||
&& nuds.is_empty()
|
||||
&& macs.is_empty()
|
||||
{
|
||||
return DeviceQuery {
|
||||
name: Some(q.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
DeviceQuery {
|
||||
name: name.or(query),
|
||||
filter: DeviceFilters {
|
||||
ips,
|
||||
devs,
|
||||
nuds,
|
||||
macs,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,7 +147,6 @@ pub struct WakeRequest {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum AgentCommand {
|
||||
Status(StatusRequest),
|
||||
Leases(LeasesRequest),
|
||||
Devs(DevsRequest),
|
||||
Inventory(InventoryRequest),
|
||||
@@ -173,7 +156,6 @@ pub enum AgentCommand {
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum CommandResult {
|
||||
Status(DeviceInventory),
|
||||
Leases { rows: Vec<DhcpLeaseWithState> },
|
||||
Devs { rows: Vec<InterfaceSummary> },
|
||||
Inventory(DeviceInventory),
|
||||
|
||||
@@ -237,7 +237,6 @@ where
|
||||
|
||||
fn command_kind(command: &AgentCommand) -> &'static str {
|
||||
match command {
|
||||
AgentCommand::Status(_) => "status",
|
||||
AgentCommand::Leases(_) => "leases",
|
||||
AgentCommand::Devs(_) => "devs",
|
||||
AgentCommand::Inventory(_) => "inventory",
|
||||
|
||||
Reference in New Issue
Block a user