Skip to main content

asterust_junos/system/
extensions.rs

1//! `system { extensions { ... } }`.
2
3use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, split_name};
4use asterust::config::ConfigLine;
5
6/// A `file <name>` of `extension-service application`. `cert-file`, `checksum`, `source`, `refresh` and `start`
7/// options are not extracted.
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub struct ExtensionApplication {
11    pub name: String,
12    pub arguments: Option<String>,
13    pub daemonize: bool,
14    /// `bash`, `python` or `python3`.
15    pub interpreter: Option<String>,
16    pub optional: bool,
17    pub routing_instance: Option<String>,
18    pub username: Option<String>,
19}
20
21/// A `license-type <license> deployment-scope [ ... ]` line of a provider.
22#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct ProviderLicense {
25    pub license_type: String,
26    pub deployment_scopes: Vec<String>,
27}
28
29/// A provider under `extensions providers`.
30#[derive(Clone, Debug, PartialEq, Eq)]
31#[non_exhaustive]
32pub struct ExtensionProvider {
33    pub id: String,
34    pub licenses: Vec<ProviderLicense>,
35}
36
37/// The `system extensions` configuration.
38#[derive(Clone, Debug, PartialEq, Eq)]
39#[non_exhaustive]
40pub struct Extensions {
41    /// Files from `extension-service { application { file ... } }`.
42    pub applications: Vec<ExtensionApplication>,
43    pub max_datasize: Option<String>,
44    pub providers: Vec<ExtensionProvider>,
45}
46
47pub(super) fn parse_extensions(extensions: &ConfigLine) -> Extensions {
48    let application =
49        child_block(extensions, "extension-service").and_then(|service| child_block(service, "application"));
50    Extensions {
51        applications: application
52            .map(|application| {
53                named_children(application, "file")
54                    .map(|(name, file)| ExtensionApplication {
55                        name: name.to_string(),
56                        arguments: arg_of(file, "arguments").map(str::to_string),
57                        daemonize: has_leaf(file, "daemonize"),
58                        interpreter: arg_of(file, "interpreter").map(str::to_string),
59                        optional: has_leaf(file, "optional"),
60                        routing_instance: arg_of(file, "routing-instance").map(str::to_string),
61                        username: arg_of(file, "username").map(str::to_string),
62                    })
63                    .collect()
64            })
65            .unwrap_or_default(),
66        max_datasize: application
67            .and_then(|application| arg_of(application, "max-datasize"))
68            .map(str::to_string),
69        providers: child_block(extensions, "providers")
70            .map(|providers| {
71                active_children(providers)
72                    .map(|provider| ExtensionProvider {
73                        id: provider.text.clone(),
74                        licenses: args_of(provider, "license-type")
75                            .into_iter()
76                            .map(|arg| {
77                                let (license_type, rest) = split_name(arg);
78                                ProviderLicense {
79                                    license_type: license_type.to_string(),
80                                    deployment_scopes: rest
81                                        .strip_prefix("deployment-scope")
82                                        .map(list_values)
83                                        .unwrap_or_default(),
84                                }
85                            })
86                            .collect(),
87                    })
88                    .collect()
89            })
90            .unwrap_or_default(),
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use crate::system::system;
97    use asterust::config::juniper;
98
99    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
100    const EXTENSIONS: &str = r#"
101system {
102    extensions {
103        extension-service {
104            application {
105                file monitor.py {
106                    arguments "--verbose";
107                    daemonize;
108                    interpreter python3;
109                    optional;
110                    routing-instance mgmt_junos;
111                    username jet;
112                    checksum sha-256 abc123;
113                }
114                file plain.sh;
115                max-datasize 64m;
116            }
117        }
118        providers {
119            ISV1 {
120                license-type juniper deployment-scope [ commercial ];
121                license-type customer deployment-scope [ poc commercial ];
122            }
123        }
124    }
125}
126"#;
127
128    #[test]
129    fn extracts_extension_applications() {
130        let extensions = system(&juniper::parse_braces(EXTENSIONS)).unwrap().extensions.unwrap();
131        assert_eq!(extensions.max_datasize.as_deref(), Some("64m"));
132        let names: Vec<_> = extensions.applications.iter().map(|a| a.name.as_str()).collect();
133        assert_eq!(names, ["monitor.py", "plain.sh"]);
134
135        let monitor = &extensions.applications[0];
136        assert_eq!(monitor.arguments.as_deref(), Some("--verbose"));
137        assert!(monitor.daemonize);
138        assert!(monitor.optional);
139        assert_eq!(monitor.interpreter.as_deref(), Some("python3"));
140        assert_eq!(monitor.routing_instance.as_deref(), Some("mgmt_junos"));
141        assert_eq!(monitor.username.as_deref(), Some("jet"));
142
143        let plain = &extensions.applications[1];
144        assert!(!plain.daemonize);
145        assert_eq!(plain.interpreter, None);
146    }
147
148    #[test]
149    fn extracts_extension_providers() {
150        let extensions = system(&juniper::parse_braces(EXTENSIONS)).unwrap().extensions.unwrap();
151        assert_eq!(extensions.providers.len(), 1);
152        let provider = &extensions.providers[0];
153        assert_eq!(provider.id, "ISV1");
154        assert_eq!(provider.licenses.len(), 2);
155        assert_eq!(provider.licenses[0].license_type, "juniper");
156        assert_eq!(provider.licenses[0].deployment_scopes, ["commercial"]);
157        assert_eq!(provider.licenses[1].deployment_scopes, ["poc", "commercial"]);
158    }
159
160    #[test]
161    fn extensions_with_only_providers() {
162        let extensions = system(&juniper::parse_braces(
163            "system {\n    extensions {\n        providers {\n            ISV2;\n        }\n    }\n}\n",
164        ))
165        .unwrap()
166        .extensions
167        .unwrap();
168        assert!(extensions.applications.is_empty());
169        assert_eq!(extensions.max_datasize, None);
170        assert_eq!(extensions.providers[0].id, "ISV2");
171        assert!(extensions.providers[0].licenses.is_empty());
172    }
173
174    #[test]
175    fn no_extensions_is_none() {
176        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
177        assert!(system.extensions.is_none());
178    }
179}