Skip to content

Commit

Permalink
add edit-config command
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-re-ka committed Oct 8, 2024
1 parent 9a96133 commit 36b91f2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
36 changes: 34 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ enum Commands {
confirm_timeout: Option<i32>,
},

/// Incrementally updates the config from the given config statements
EditConfig {
statement: String,
confirm_timeout: Option<i32>,
},

/// Confirm a previously applied configuration
Confirm,

Expand Down Expand Up @@ -114,7 +120,7 @@ fn main() {

netconf_session.lock_configuration().unwrap();

if let Err(e) = netconf_session.load_configuration(data) {
if let Err(e) = netconf_session.load_configuration(data, "update".into(), "text".into()) {
eprintln!("Config load failed: {}", e);
std::process::exit(1);
}
Expand All @@ -132,6 +138,32 @@ fn main() {

netconf_session.unlock_configuration().unwrap();
}
Commands::EditConfig {
statement,
confirm_timeout,
} => {
netconf_session.lock_configuration().unwrap();

for line in statement.split(";") {
if let Err(e) = netconf_session.load_configuration(line.into(), "set".into(), "set".into()) {
eprintln!("Config load failed: {}", e);
std::process::exit(1);
}
}

let diff_reply = netconf_session
.diff_configuration("text".to_string())
.unwrap();
println!("{}", diff_reply);

eprintln!("Applying configuration...");

netconf_session
.apply_configuration(confirm_timeout)
.unwrap();

netconf_session.unlock_configuration().unwrap();
}
Commands::Confirm => {
eprintln!("Confirming configuration");

Expand All @@ -142,7 +174,7 @@ fn main() {

let _ = netconf_session.lock_configuration().unwrap();

if let Err(e) = netconf_session.load_configuration(data) {
if let Err(e) = netconf_session.load_configuration(data, "update".into(), "text".into()) {
eprintln!("Config load failed: {}", e);
std::process::exit(1);
}
Expand Down
19 changes: 15 additions & 4 deletions src/netconf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,20 @@ impl NETCONFClient {
ok.ok_or(NETCONFError::MissingOk)
}

pub fn load_configuration(&mut self, cfg: String) -> NETCONFResult<()> {
pub fn load_configuration(&mut self, cfg: String, action: String, format: String) -> NETCONFResult<()> {
let mut cfg_text = None;
let mut cfg_set = None;
match format.as_str() {
"text" => cfg_text = Some(cfg),
"set" => cfg_set = Some(cfg),
_ => unimplemented!(),
}
let c = RPC {
rpc: RPCCommand::LoadConfiguration {
format: "text".to_string(),
action: "update".to_string(),
cfg,
format,
action,
cfg_text,
cfg_set,
},
};
let _ = self.send_rpc(c)?;
Expand All @@ -233,6 +241,9 @@ impl NETCONFClient {
return Err(error.into());
}
}
LoadConfigurationResultsEnum::LoadErrorCount(l) => {
eprintln!("{:?}", l);
}
LoadConfigurationResultsEnum::Ok => ok = Some(()),
}
}
Expand Down
26 changes: 19 additions & 7 deletions src/netconf/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ pub enum RPCCommand {
#[serde(rename = "@action")]
action: String,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "configuration-text")]
cfg: String,
cfg_text: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "configuration-set")]
cfg_set: Option<String>,
},

#[serde(rename = "commit-configuration")]
Expand All @@ -84,12 +89,6 @@ pub enum RPCCommand {
#[derive(Debug, Deserialize, Serialize)]
pub struct ConfigurationConfirmed {}

#[derive(Debug, Deserialize, Serialize)]
pub struct ConfigurationInformation {
#[serde(rename = "configuration-text")]
pub configuration_text: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename = "rpc-reply")]
pub struct RPCReply {
Expand Down Expand Up @@ -140,6 +139,16 @@ pub enum LoadConfigurationResultsEnum {

#[serde(rename = "rpc-error")]
RPCError(RPCError),

#[serde(rename = "load-error-count")]
LoadErrorCount(LoadErrorCount),
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct LoadErrorCount {
#[serde(rename = "$text")]
message: String,
}

impl Display for RPCReply {
Expand Down Expand Up @@ -181,6 +190,9 @@ impl Display for RPCReplyCommand {
LoadConfigurationResultsEnum::RPCError(error) => {
writeln!(f, "{}", error)?;
}
LoadConfigurationResultsEnum::LoadErrorCount(l)=> {
writeln!(f, "{:?}", l)?;
}
}
}

Expand Down

0 comments on commit 36b91f2

Please sign in to comment.