Import current project code
This commit is contained in:
@@ -9,15 +9,14 @@
|
||||
|
||||
use core::panic;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
collections::{BTreeMap, HashMap}, sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use anyhow::bail;
|
||||
use futures::future::join_all;
|
||||
use mikrotik_rs::{CommandBuilder, Event, MikrotikDevice};
|
||||
use mikrotik_rs::{CommandBuilder, Event, MikrotikDevice, QueryOperator, proto::word::Word::Attribute};
|
||||
|
||||
use crate::config::{Config, ConfigHost};
|
||||
use crate::config::{Config, ConfigHost, cli::{FilterOp, MtCommand}};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref MTC: Arc<Mutex<MtCommanderInternal>> = MtCommanderInternal::new_mutex();
|
||||
@@ -38,7 +37,7 @@ impl MtCommander {
|
||||
|
||||
pub fn run_command_on_hosts(
|
||||
hosts: &[ConfigHost],
|
||||
command: &str,
|
||||
command: &MtCommand
|
||||
) -> impl Future<
|
||||
Output = anyhow::Result<
|
||||
HashMap<ConfigHost, anyhow::Result<Vec<HashMap<String, Option<String>>>>>,
|
||||
@@ -57,7 +56,7 @@ impl MtCommander {
|
||||
}
|
||||
pub async fn run_command_on_host(
|
||||
host: &ConfigHost,
|
||||
command: &str,
|
||||
command: &MtCommand
|
||||
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
|
||||
let mtc = {
|
||||
MTC.lock()
|
||||
@@ -124,7 +123,7 @@ impl MtCommanderInternal {
|
||||
async fn run_command(
|
||||
&self,
|
||||
host: &ConfigHost,
|
||||
cmd: &str,
|
||||
command: &MtCommand
|
||||
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
|
||||
let addr = format!("{}:{}", &host.ip, host.port(&self.cfg()));
|
||||
let dev_res = MikrotikDevice::builder(&addr)
|
||||
@@ -143,8 +142,36 @@ impl MtCommanderInternal {
|
||||
}
|
||||
};
|
||||
|
||||
let mut cmd = CommandBuilder::new().command(&command.cmd);
|
||||
for filter in &command.filter {
|
||||
cmd = match filter.op {
|
||||
FilterOp::Eq => cmd.query_equal(&filter.key, &filter.value),
|
||||
FilterOp::Neq => cmd
|
||||
.query_equal(&filter.key, &filter.value)
|
||||
.query_operations([QueryOperator::Not].into_iter()),
|
||||
FilterOp::Le => cmd
|
||||
.query_lt(&filter.key, &filter.value)
|
||||
.query_equal(&filter.key, &filter.value)
|
||||
.query_operations([QueryOperator::Or].into_iter()),
|
||||
FilterOp::Ge => cmd
|
||||
.query_gt(&filter.key, &filter.value)
|
||||
.query_equal(&filter.key, &filter.value)
|
||||
.query_operations([QueryOperator::Or].into_iter()),
|
||||
FilterOp::Lt => cmd.query_lt(&filter.key, &filter.value),
|
||||
FilterOp::Gt => cmd.query_gt(&filter.key, &filter.value),
|
||||
}
|
||||
}
|
||||
|
||||
for (attr_k, attr_v) in &command.attr {
|
||||
cmd=cmd.attribute(attr_k, attr_v.as_deref())
|
||||
}
|
||||
let cmd = cmd.build();
|
||||
|
||||
log::debug!("Query with filters: {:?}", cmd);
|
||||
|
||||
|
||||
let mut rx = dev
|
||||
.send_command(CommandBuilder::new().command(cmd).build())
|
||||
.send_command(cmd)
|
||||
.await?;
|
||||
|
||||
let mut res = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user