Skip to content

Commit 36b91f2

Browse files
committed
add edit-config command
1 parent 9a96133 commit 36b91f2

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

src/main.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ enum Commands {
5151
confirm_timeout: Option<i32>,
5252
},
5353

54+
/// Incrementally updates the config from the given config statements
55+
EditConfig {
56+
statement: String,
57+
confirm_timeout: Option<i32>,
58+
},
59+
5460
/// Confirm a previously applied configuration
5561
Confirm,
5662

@@ -114,7 +120,7 @@ fn main() {
114120

115121
netconf_session.lock_configuration().unwrap();
116122

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

133139
netconf_session.unlock_configuration().unwrap();
134140
}
141+
Commands::EditConfig {
142+
statement,
143+
confirm_timeout,
144+
} => {
145+
netconf_session.lock_configuration().unwrap();
146+
147+
for line in statement.split(";") {
148+
if let Err(e) = netconf_session.load_configuration(line.into(), "set".into(), "set".into()) {
149+
eprintln!("Config load failed: {}", e);
150+
std::process::exit(1);
151+
}
152+
}
153+
154+
let diff_reply = netconf_session
155+
.diff_configuration("text".to_string())
156+
.unwrap();
157+
println!("{}", diff_reply);
158+
159+
eprintln!("Applying configuration...");
160+
161+
netconf_session
162+
.apply_configuration(confirm_timeout)
163+
.unwrap();
164+
165+
netconf_session.unlock_configuration().unwrap();
166+
}
135167
Commands::Confirm => {
136168
eprintln!("Confirming configuration");
137169

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

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

145-
if let Err(e) = netconf_session.load_configuration(data) {
177+
if let Err(e) = netconf_session.load_configuration(data, "update".into(), "text".into()) {
146178
eprintln!("Config load failed: {}", e);
147179
std::process::exit(1);
148180
}

src/netconf/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,20 @@ impl NETCONFClient {
201201
ok.ok_or(NETCONFError::MissingOk)
202202
}
203203

204-
pub fn load_configuration(&mut self, cfg: String) -> NETCONFResult<()> {
204+
pub fn load_configuration(&mut self, cfg: String, action: String, format: String) -> NETCONFResult<()> {
205+
let mut cfg_text = None;
206+
let mut cfg_set = None;
207+
match format.as_str() {
208+
"text" => cfg_text = Some(cfg),
209+
"set" => cfg_set = Some(cfg),
210+
_ => unimplemented!(),
211+
}
205212
let c = RPC {
206213
rpc: RPCCommand::LoadConfiguration {
207-
format: "text".to_string(),
208-
action: "update".to_string(),
209-
cfg,
214+
format,
215+
action,
216+
cfg_text,
217+
cfg_set,
210218
},
211219
};
212220
let _ = self.send_rpc(c)?;
@@ -233,6 +241,9 @@ impl NETCONFClient {
233241
return Err(error.into());
234242
}
235243
}
244+
LoadConfigurationResultsEnum::LoadErrorCount(l) => {
245+
eprintln!("{:?}", l);
246+
}
236247
LoadConfigurationResultsEnum::Ok => ok = Some(()),
237248
}
238249
}

src/netconf/xml.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ pub enum RPCCommand {
6565
#[serde(rename = "@action")]
6666
action: String,
6767

68+
#[serde(skip_serializing_if = "Option::is_none")]
6869
#[serde(rename = "configuration-text")]
69-
cfg: String,
70+
cfg_text: Option<String>,
71+
72+
#[serde(skip_serializing_if = "Option::is_none")]
73+
#[serde(rename = "configuration-set")]
74+
cfg_set: Option<String>,
7075
},
7176

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

87-
#[derive(Debug, Deserialize, Serialize)]
88-
pub struct ConfigurationInformation {
89-
#[serde(rename = "configuration-text")]
90-
pub configuration_text: String,
91-
}
92-
9392
#[derive(Debug, Deserialize, Serialize)]
9493
#[serde(rename = "rpc-reply")]
9594
pub struct RPCReply {
@@ -140,6 +139,16 @@ pub enum LoadConfigurationResultsEnum {
140139

141140
#[serde(rename = "rpc-error")]
142141
RPCError(RPCError),
142+
143+
#[serde(rename = "load-error-count")]
144+
LoadErrorCount(LoadErrorCount),
145+
}
146+
147+
#[derive(Debug, Deserialize, Serialize)]
148+
#[serde(deny_unknown_fields)]
149+
pub struct LoadErrorCount {
150+
#[serde(rename = "$text")]
151+
message: String,
143152
}
144153

145154
impl Display for RPCReply {
@@ -181,6 +190,9 @@ impl Display for RPCReplyCommand {
181190
LoadConfigurationResultsEnum::RPCError(error) => {
182191
writeln!(f, "{}", error)?;
183192
}
193+
LoadConfigurationResultsEnum::LoadErrorCount(l)=> {
194+
writeln!(f, "{:?}", l)?;
195+
}
184196
}
185197
}
186198

0 commit comments

Comments
 (0)