asterust_junos/system/services/
service_deployment.rs1use crate::{arg_of, named_children};
9use asterust::config::ConfigLine;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct ServiceDeploymentServer {
15 pub address: String,
16 pub port: Option<String>,
17}
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct ServiceDeployment {
23 pub servers: Vec<ServiceDeploymentServer>,
24 pub source_address: Option<String>,
25}
26
27pub(super) fn parse_service_deployment(deployment: &ConfigLine) -> ServiceDeployment {
28 ServiceDeployment {
29 servers: named_children(deployment, "servers")
30 .map(|(address, server)| ServiceDeploymentServer {
31 address: address.to_string(),
32 port: arg_of(server, "port").map(str::to_string),
33 })
34 .collect(),
35 source_address: arg_of(deployment, "source-address").map(str::to_string),
36 }
37}
38
39#[cfg(test)]
40mod tests {
41 use crate::system::system;
42 use asterust::config::juniper;
43
44 #[test]
46 fn extracts_service_deployment() {
47 let deployment = system(&juniper::parse_braces(
48 "system {\n services {\n service-deployment {\n servers 192.0.2.40 {\n port 3333;\n }\n servers 192.0.2.41;\n source-address 192.0.2.1;\n }\n }\n}\n",
49 ))
50 .unwrap()
51 .services
52 .unwrap()
53 .service_deployment
54 .unwrap();
55 assert_eq!(deployment.servers.len(), 2);
56 assert_eq!(deployment.servers[0].address, "192.0.2.40");
57 assert_eq!(deployment.servers[0].port.as_deref(), Some("3333"));
58 assert_eq!(deployment.servers[1].port, None);
59 assert_eq!(deployment.source_address.as_deref(), Some("192.0.2.1"));
60 }
61
62 #[test]
63 fn no_service_deployment_is_none() {
64 let services = system(&juniper::parse_braces(
65 "system {\n services {\n ssh;\n }\n}\n",
66 ))
67 .unwrap()
68 .services
69 .unwrap();
70 assert!(services.service_deployment.is_none());
71 }
72}