Skip to main content

asterust_junos/system/services/
service_deployment.rs

1//! `system { services { service-deployment { ... } } }`.
2
3use crate::{arg_of, named_children};
4use asterust::config::ConfigLine;
5
6/// A `servers <address>` entry of `service-deployment`.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct ServiceDeploymentServer {
10    pub address: String,
11    pub port: Option<String>,
12}
13
14/// The `system services service-deployment` configuration, which connects Junos OS to SRC software.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct ServiceDeployment {
18    pub servers: Vec<ServiceDeploymentServer>,
19    pub source_address: Option<String>,
20}
21
22pub(super) fn parse_service_deployment(deployment: &ConfigLine) -> ServiceDeployment {
23    ServiceDeployment {
24        servers: named_children(deployment, "servers")
25            .map(|(address, server)| ServiceDeploymentServer {
26                address: address.to_string(),
27                port: arg_of(server, "port").map(str::to_string),
28            })
29            .collect(),
30        source_address: arg_of(deployment, "source-address").map(str::to_string),
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::system::system;
37    use asterust::config::juniper;
38
39    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
40    #[test]
41    fn extracts_service_deployment() {
42        let deployment = system(&juniper::parse_braces(
43            "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",
44        ))
45        .unwrap()
46        .services
47        .unwrap()
48        .service_deployment
49        .unwrap();
50        assert_eq!(deployment.servers.len(), 2);
51        assert_eq!(deployment.servers[0].address, "192.0.2.40");
52        assert_eq!(deployment.servers[0].port.as_deref(), Some("3333"));
53        assert_eq!(deployment.servers[1].port, None);
54        assert_eq!(deployment.source_address.as_deref(), Some("192.0.2.1"));
55    }
56
57    #[test]
58    fn no_service_deployment_is_none() {
59        let services = system(&juniper::parse_braces(
60            "system {\n    services {\n        ssh;\n    }\n}\n",
61        ))
62        .unwrap()
63        .services
64        .unwrap();
65        assert!(services.service_deployment.is_none());
66    }
67}