Add traffic control configuration config with API

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer
2021-11-06 18:46:58 +01:00
parent 0c136bfab1
commit bfd12e871f
11 changed files with 577 additions and 10 deletions

View File

@ -14,6 +14,7 @@ pub mod changer;
pub mod media_pool;
pub mod tape_encryption_keys;
pub mod tape_backup_job;
pub mod traffic_control;
const SUBDIRS: SubdirMap = &[
("access", &access::ROUTER),
@ -26,6 +27,7 @@ const SUBDIRS: SubdirMap = &[
("sync", &sync::ROUTER),
("tape-backup-job", &tape_backup_job::ROUTER),
("tape-encryption-keys", &tape_encryption_keys::ROUTER),
("traffic-control", &traffic_control::ROUTER),
("verify", &verify::ROUTER),
];

View File

@ -0,0 +1,253 @@
use anyhow::{bail, Error};
use serde_json::Value;
use ::serde::{Deserialize, Serialize};
use proxmox_router::{ApiMethod, Router, RpcEnvironment, Permission};
use proxmox_schema::api;
use pbs_api_types::{
TrafficControlRule, TrafficControlRuleUpdater,
PROXMOX_CONFIG_DIGEST_SCHEMA, TRAFFIC_CONTROL_ID_SCHEMA,
PRIV_SYS_AUDIT, PRIV_SYS_MODIFY,
};
#[api(
input: {
properties: {},
},
returns: {
description: "The list of configured traffic control rules (with config digest).",
type: Array,
items: { type: TrafficControlRule },
},
access: {
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
},
)]
/// List traffic control rules
pub fn list_traffic_controls(
_param: Value,
_info: &ApiMethod,
mut rpcenv: &mut dyn RpcEnvironment,
) -> Result<Vec<TrafficControlRule>, Error> {
let (config, digest) = pbs_config::traffic_control::config()?;
let list: Vec<TrafficControlRule> = config.convert_to_typed_array("rule")?;
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
Ok(list)
}
#[api(
protected: true,
input: {
properties: {
config: {
type: TrafficControlRule,
flatten: true,
},
},
},
access: {
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
},
)]
/// Create new traffic control rule.
pub fn create_traffic_control(config: TrafficControlRule) -> Result<(), Error> {
let _lock = pbs_config::traffic_control::lock_config()?;
let (mut section_config, _digest) = pbs_config::traffic_control::config()?;
if section_config.sections.get(&config.name).is_some() {
bail!("traffic control rule '{}' already exists.", config.name);
}
section_config.set_data(&config.name, "rule", &config)?;
pbs_config::traffic_control::save_config(&section_config)?;
Ok(())
}
#[api(
input: {
properties: {
name: {
schema: TRAFFIC_CONTROL_ID_SCHEMA,
},
},
},
returns: { type: TrafficControlRule },
access: {
permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
}
)]
/// Read traffic control configuration data.
pub fn read_traffic_control(
name: String,
_info: &ApiMethod,
mut rpcenv: &mut dyn RpcEnvironment,
) -> Result<TrafficControlRule, Error> {
let (config, digest) = pbs_config::traffic_control::config()?;
let data: TrafficControlRule = config.lookup("rule", &name)?;
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
Ok(data)
}
#[api()]
#[derive(Serialize, Deserialize)]
#[allow(non_camel_case_types)]
/// Deletable property name
pub enum DeletableProperty {
/// Delete the rate_in property.
rate_in,
/// Delete the burst_in property.
burst_in,
/// Delete the rate_out property.
rate_out,
/// Delete the burst_out property.
burst_out,
/// Delete the comment property.
comment,
/// Delete the timeframe property
timeframe,
}
// fixme: use TrafficControlUpdater
#[api(
protected: true,
input: {
properties: {
name: {
schema: TRAFFIC_CONTROL_ID_SCHEMA,
},
update: {
type: TrafficControlRuleUpdater,
flatten: true,
},
delete: {
description: "List of properties to delete.",
type: Array,
optional: true,
items: {
type: DeletableProperty,
}
},
digest: {
optional: true,
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
},
},
},
access: {
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
},
)]
/// Update traffic control configuration.
pub fn update_traffic_control(
name: String,
update: TrafficControlRuleUpdater,
delete: Option<Vec<DeletableProperty>>,
digest: Option<String>,
) -> Result<(), Error> {
let _lock = pbs_config::traffic_control::lock_config()?;
let (mut config, expected_digest) = pbs_config::traffic_control::config()?;
if let Some(ref digest) = digest {
let digest = proxmox::tools::hex_to_digest(digest)?;
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
}
let mut data: TrafficControlRule = config.lookup("rule", &name)?;
if let Some(delete) = delete {
for delete_prop in delete {
match delete_prop {
DeletableProperty::rate_in => { data.rate_in = None; },
DeletableProperty::rate_out => { data.rate_out = None; },
DeletableProperty::burst_in => { data.burst_in = None; },
DeletableProperty::burst_out => { data.burst_out = None; },
DeletableProperty::comment => { data.comment = None; },
DeletableProperty::timeframe => { data.timeframe = None; },
}
}
}
if let Some(comment) = update.comment {
let comment = comment.trim().to_string();
if comment.is_empty() {
data.comment = None;
} else {
data.comment = Some(comment);
}
}
if update.rate_in.is_some() { data.rate_in = update.rate_in; }
if update.rate_out.is_some() { data.rate_out = update.rate_out; }
if update.burst_in.is_some() { data.burst_in = update.burst_in; }
if update.burst_out.is_some() { data.burst_out = update.burst_out; }
if let Some(network) = update.network { data.network = network; }
if update.timeframe.is_some() { data.timeframe = update.timeframe; }
config.set_data(&name, "rule", &data)?;
pbs_config::traffic_control::save_config(&config)?;
Ok(())
}
#[api(
protected: true,
input: {
properties: {
name: {
schema: TRAFFIC_CONTROL_ID_SCHEMA,
},
digest: {
optional: true,
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
},
},
},
access: {
permission: &Permission::Privilege(&[], PRIV_SYS_MODIFY, false),
},
)]
/// Remove a traffic control rule from the configuration file.
pub fn delete_traffic_control(name: String, digest: Option<String>) -> Result<(), Error> {
let _lock = pbs_config::traffic_control::lock_config()?;
let (mut config, expected_digest) = pbs_config::traffic_control::config()?;
if let Some(ref digest) = digest {
let digest = proxmox::tools::hex_to_digest(digest)?;
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
}
match config.sections.get(&name) {
Some(_) => { config.sections.remove(&name); },
None => bail!("traffic control rule '{}' does not exist.", name),
}
pbs_config::traffic_control::save_config(&config)?;
Ok(())
}
const ITEM_ROUTER: Router = Router::new()
.get(&API_METHOD_READ_TRAFFIC_CONTROL)
.put(&API_METHOD_UPDATE_TRAFFIC_CONTROL)
.delete(&API_METHOD_DELETE_TRAFFIC_CONTROL);
pub const ROUTER: Router = Router::new()
.get(&API_METHOD_LIST_TRAFFIC_CONTROLS)
.post(&API_METHOD_CREATE_TRAFFIC_CONTROL)
.match_all("name", &ITEM_ROUTER);

View File

@ -374,6 +374,7 @@ async fn run() -> Result<(), Error> {
.insert("user", user_commands())
.insert("openid", openid_commands())
.insert("remote", remote_commands())
.insert("traffic-control", traffic_control_commands())
.insert("garbage-collection", garbage_collection_commands())
.insert("acme", acme_mgmt_cli())
.insert("cert", cert_mgmt_cli())

View File

@ -26,3 +26,5 @@ mod node;
pub use node::*;
mod openid;
pub use openid::*;
mod traffic_control;
pub use traffic_control::*;

View File

@ -0,0 +1,107 @@
use anyhow::Error;
use serde_json::Value;
use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
use proxmox_schema::api;
use pbs_api_types::TRAFFIC_CONTROL_ID_SCHEMA;
use proxmox_backup::api2;
#[api(
input: {
properties: {
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}
}
)]
/// List configured traffic control rules.
fn list_traffic_controls(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
let output_format = get_output_format(&param);
let info = &api2::config::traffic_control::API_METHOD_LIST_TRAFFIC_CONTROLS;
let mut data = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
let options = default_table_format_options()
.column(ColumnConfig::new("name"))
.column(ColumnConfig::new("rate-in"))
.column(ColumnConfig::new("burst-in"))
.column(ColumnConfig::new("rate-out"))
.column(ColumnConfig::new("burst-out"))
.column(ColumnConfig::new("network"))
.column(ColumnConfig::new("timeframe"))
.column(ColumnConfig::new("comment"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(Value::Null)
}
#[api(
input: {
properties: {
name: {
schema: TRAFFIC_CONTROL_ID_SCHEMA,
},
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}
}
)]
/// Show traffic control configuration
fn show_traffic_control(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
let output_format = get_output_format(&param);
let info = &api2::config::traffic_control::API_METHOD_READ_TRAFFIC_CONTROL;
let mut data = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
let options = default_table_format_options();
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(Value::Null)
}
pub fn traffic_control_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert("list", CliCommand::new(&API_METHOD_LIST_TRAFFIC_CONTROLS))
.insert(
"show",
CliCommand::new(&API_METHOD_SHOW_TRAFFIC_CONTROL)
.arg_param(&["name"])
.completion_cb("name", pbs_config::traffic_control::complete_traffic_control_name)
)
.insert(
"create",
CliCommand::new(&api2::config::traffic_control::API_METHOD_CREATE_TRAFFIC_CONTROL)
.arg_param(&["name"])
)
.insert(
"update",
CliCommand::new(&api2::config::traffic_control::API_METHOD_UPDATE_TRAFFIC_CONTROL)
.arg_param(&["name"])
.completion_cb("name", pbs_config::traffic_control::complete_traffic_control_name)
)
.insert(
"remove",
CliCommand::new(&api2::config::traffic_control::API_METHOD_DELETE_TRAFFIC_CONTROL)
.arg_param(&["name"])
.completion_cb("name", pbs_config::traffic_control::complete_traffic_control_name)
);
cmd_def.into()
}