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