Skip to content

Commit

Permalink
make validator function
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Oct 11, 2023
1 parent 47daf4c commit b920073
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
10 changes: 6 additions & 4 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ use zenoh_protocol::{
use zenoh_result::{bail, zerror, ZResult};
use zenoh_util::LibLoader;

pub type ValidationFunction = std::sync::Arc<
type ValidationFunction = std::sync::Arc<
dyn Fn(
&str,
&serde_json::Map<String, serde_json::Value>,
&serde_json::Map<String, serde_json::Value>,
&str, // plugin name
&str, // `path`, the relative path from the plugin's configuration root to the changed value.
&serde_json::Map<String, serde_json::Value>, // `current`, the current configuration of the plugin (from its root).
&serde_json::Map<String, serde_json::Value>, // `new`, the proposed new configuration of the plugin.
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>>
+ Send
+ Sync,
Expand Down Expand Up @@ -949,6 +950,7 @@ impl PluginsConfig {
}
let new_conf = if let Some(validator) = validator {
match validator(
plugin,
&key[("plugins/".len() + plugin.len())..],
old_conf.as_object().unwrap(),
new_conf.as_object().unwrap(),
Expand Down
36 changes: 19 additions & 17 deletions zenoh/src/net/runtime/adminspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use super::routing::face::Face;
use super::Runtime;
use crate::key_expr::KeyExpr;
use crate::plugins::sealed as plugins;
use crate::plugins::sealed::{self as plugins, ValidationFunction};
use crate::prelude::sync::{Sample, SyncResolve};
use crate::queryable::Query;
use crate::queryable::QueryInner;
Expand Down Expand Up @@ -64,6 +64,18 @@ enum PluginDiff {
Start(crate::config::PluginLoad),
}

fn make_plugin_validator(admin: &Arc<AdminSpace>) -> ValidationFunction {
let admin = admin.clone();
Arc::new(move |name: &_, path: &_, old: &_, new: &_| {
let plugins_mgr = zlock!(admin.context.plugins_mgr);
if let Some(plugin) = plugins_mgr.plugin(name) {
plugin.config_checker(path, old, new)
} else {
Err(format!("Plugin {name} not found").into())
}
})
}

impl AdminSpace {
pub async fn start(runtime: &Runtime, plugins_mgr: plugins::PluginsManager, version: String) {
let zid_str = runtime.state.zid.to_string();
Expand Down Expand Up @@ -128,6 +140,11 @@ impl AdminSpace {
context,
});

let mut config_guard = admin.context.runtime.state.config.lock();
for (name, (_, plugin)) in plugins.running_plugins() {
config_guard.add_plugin_validator(name, make_plugin_validator(&admin))
}

let cfg_rx = admin.context.runtime.state.config.subscribe();
task::spawn({
let admin = admin.clone();
Expand Down Expand Up @@ -196,24 +213,9 @@ impl AdminSpace {
active_plugins.insert(name.into(), path.into());
let mut cfg_guard =
admin.context.runtime.state.config.lock();
let validation_function = {
let name = name.clone();
let admin = admin.clone();
Arc::new(move |path: &_, old: &_, new: &_| {
let plugins_mgr =
zlock!(admin.context.plugins_mgr);
if let Some(plugin) =
plugins_mgr.plugin(name.as_str())
{
plugin.config_checker(path, old, new)
} else {
Err("Plugin not found".into())
}
})
};
cfg_guard.add_plugin_validator(
name,
validation_function,
make_plugin_validator(&admin),
);
log::info!(
"Successfully started plugin `{}` from {}",
Expand Down
8 changes: 0 additions & 8 deletions zenohd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,6 @@ clap::Arg::new("adminspace-permissions").long("adminspace-permissions").value_na
}
log::info!("Finished loading plugins");

{
let mut config_guard = runtime.config().lock();
for (name, (_, plugin)) in plugins.running_plugins() {
let hook = plugin.config_checker();
config_guard.add_plugin_validator(name, hook)
}
}

AdminSpace::start(&runtime, plugins, LONG_VERSION.clone()).await;

future::pending::<()>().await;
Expand Down

0 comments on commit b920073

Please sign in to comment.