use crate::{arg_of, args_of, child_block, list_values, named_children};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct GrpcTunnelServer {
pub name: String,
pub address: Option<String>,
pub port: Option<String>,
pub tls_certificate_id: Option<String>,
pub targets: Vec<String>,
pub routing_instance: Option<String>,
pub source_address: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct GrpcTunnel {
pub retry_interval: Option<String>,
pub servers: Vec<GrpcTunnelServer>,
pub target_string_pattern: Vec<String>,
pub target_string_custom: Option<String>,
pub target_string_delimiter: Option<String>,
}
pub(super) fn parse_grpc_tunnel(tunnel: &ConfigLine) -> GrpcTunnel {
let servers = child_block(tunnel, "servers");
let target_string = child_block(tunnel, "target-string-option");
GrpcTunnel {
retry_interval: servers.and_then(|s| arg_of(s, "retry-interval")).map(str::to_string),
servers: servers
.map(|servers| {
named_children(servers, "server")
.map(|(name, server)| GrpcTunnelServer {
name: name.to_string(),
address: arg_of(server, "address").map(str::to_string),
port: arg_of(server, "port").map(str::to_string),
tls_certificate_id: child_block(server, "credentials")
.and_then(|credentials| child_block(credentials, "tls"))
.and_then(|tls| arg_of(tls, "certificate-id"))
.map(str::to_string),
targets: args_of(server, "targets").into_iter().flat_map(list_values).collect(),
routing_instance: arg_of(server, "routing-instance").map(str::to_string),
source_address: arg_of(server, "source-address").map(str::to_string),
})
.collect()
})
.unwrap_or_default(),
target_string_pattern: target_string
.map(|t| args_of(t, "pattern").into_iter().flat_map(list_values).collect())
.unwrap_or_default(),
target_string_custom: target_string
.and_then(|t| arg_of(t, "custom-string"))
.map(str::to_string),
target_string_delimiter: target_string.and_then(|t| arg_of(t, "delimiter")).map(str::to_string),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_grpc_tunnel() {
let tunnel = system(&juniper::parse_braces(
r#"
system {
services {
grpc-tunnel {
servers {
retry-interval 30;
server collector1 {
address 192.0.2.40;
port 8443;
credentials {
tls {
certificate-id tunnel-cert;
}
}
targets [ ssh gnmi-gnoi ];
routing-instance mgmt_junos;
source-address 192.0.2.1;
}
}
target-string-option {
pattern [ hostname model ];
custom-string lab;
delimiter "-";
}
}
}
}
"#,
))
.unwrap()
.services
.unwrap()
.grpc_tunnel
.unwrap();
assert_eq!(tunnel.retry_interval.as_deref(), Some("30"));
let server = &tunnel.servers[0];
assert_eq!(server.name, "collector1");
assert_eq!(server.address.as_deref(), Some("192.0.2.40"));
assert_eq!(server.port.as_deref(), Some("8443"));
assert_eq!(server.tls_certificate_id.as_deref(), Some("tunnel-cert"));
assert_eq!(server.targets, ["ssh", "gnmi-gnoi"]);
assert_eq!(server.routing_instance.as_deref(), Some("mgmt_junos"));
assert_eq!(server.source_address.as_deref(), Some("192.0.2.1"));
assert_eq!(tunnel.target_string_pattern, ["hostname", "model"]);
assert_eq!(tunnel.target_string_custom.as_deref(), Some("lab"));
assert_eq!(tunnel.target_string_delimiter.as_deref(), Some("-"));
}
#[test]
fn no_grpc_tunnel_is_none() {
let services = system(&juniper::parse_braces(
"system {\n services {\n telnet;\n }\n}\n",
))
.unwrap()
.services
.unwrap();
assert!(services.grpc_tunnel.is_none());
}
}