Skip to main content

asterust_junos/system/services/
static_subscribers.rs

1//! `static-subscribers`
2//! `system { services { static-subscribers { ... } } }`.
3//!
4//! # See also
5//!
6//! - [authentication (Static Subscribers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-edit-static-subscribers.html)
7//! - [interface (Static Subscriber Group)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-static-subscribers-group.html)
8//! - [interface (Static Subscriber Username)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-static-subscribers-username.html)
9//! - [logical-system-name (Static Subscribers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/logical-system-edit-static-subscribers.html)
10//! - [routing-instance-name (Static Subscribers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/routing-instance-name-edit-static-subscribers.html)
11//! - [service-profile (Static Subscribers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/service-profile-edit-static-subscribers.html)
12//! - [static-subscribers (Dynamic Service Provisioning)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-subscribers-edit-system-services.html)
13
14use crate::{arg_of, args_of, child_block, has_leaf, named_children, secret_value, Secret};
15use asterust::config::ConfigLine;
16
17/// The `username-include` block of the static subscribers `authentication`.
18#[derive(Clone, Debug, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct StaticSubscriberUsername {
21    pub delimiter: Option<String>,
22    pub domain_name: Option<String>,
23    pub interface: bool,
24    pub logical_system_name: bool,
25    pub routing_instance_name: bool,
26    pub user_prefix: Option<String>,
27    pub vlan_tags: bool,
28}
29
30/// A `dynamic-profile <name>` of a static-subscribers group.
31#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct StaticSubscriberProfile {
34    pub name: String,
35    /// `merge` or `replace`.
36    pub aggregate_clients: Option<String>,
37}
38
39/// A `group <name>` under `static-subscribers`.
40#[derive(Clone, Debug, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct StaticSubscriberGroup {
43    pub name: String,
44    pub access_profile: Option<String>,
45    /// The `authentication { password ...; }`; `None` unless the `secrets` feature is enabled.
46    pub password: Option<Secret>,
47    pub dynamic_profiles: Vec<StaticSubscriberProfile>,
48    pub service_profile: Option<String>,
49    pub username_include: Option<StaticSubscriberUsername>,
50    /// Each `interface` as written, e.g. `"ge-1/0/0.100 upto ge-1/0/0.200"` or `"ge-1/0/0.5
51    /// exclude"`.
52    pub interfaces: Vec<String>,
53}
54
55/// The `system services static-subscribers` settings.
56#[derive(Clone, Debug, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct StaticSubscribers {
59    pub access_profile: Option<String>,
60    pub service_profile: Option<String>,
61    /// The `authentication { password ...; }`; `None` unless the `secrets` feature is enabled.
62    pub password: Option<Secret>,
63    pub username_include: Option<StaticSubscriberUsername>,
64    pub dynamic_profiles: Vec<StaticSubscriberProfile>,
65    pub groups: Vec<StaticSubscriberGroup>,
66}
67
68/// The password of the `authentication` block of `line`.
69fn password_of(line: &ConfigLine) -> Option<Secret> {
70    secret_value(child_block(line, "authentication").and_then(|auth| arg_of(auth, "password")))
71}
72
73fn username_include(line: &ConfigLine) -> Option<StaticSubscriberUsername> {
74    let include = child_block(child_block(line, "authentication")?, "username-include")?;
75    Some(StaticSubscriberUsername {
76        delimiter: arg_of(include, "delimiter").map(str::to_string),
77        domain_name: arg_of(include, "domain-name").map(str::to_string),
78        interface: has_leaf(include, "interface"),
79        logical_system_name: has_leaf(include, "logical-system-name"),
80        routing_instance_name: has_leaf(include, "routing-instance-name"),
81        user_prefix: arg_of(include, "user-prefix").map(str::to_string),
82        vlan_tags: has_leaf(include, "vlan-tags"),
83    })
84}
85
86fn dynamic_profiles(line: &ConfigLine) -> Vec<StaticSubscriberProfile> {
87    named_children(line, "dynamic-profile")
88        .map(|(name, profile)| StaticSubscriberProfile {
89            name: name.to_string(),
90            aggregate_clients: arg_of(profile, "aggregate-clients").map(str::to_string),
91        })
92        .collect()
93}
94
95pub(super) fn parse_static_subscribers(subscribers: &ConfigLine) -> StaticSubscribers {
96    StaticSubscribers {
97        access_profile: arg_of(subscribers, "access-profile").map(str::to_string),
98        service_profile: arg_of(subscribers, "service-profile").map(str::to_string),
99        password: password_of(subscribers),
100        username_include: username_include(subscribers),
101        dynamic_profiles: dynamic_profiles(subscribers),
102        groups: named_children(subscribers, "group")
103            .map(|(name, group)| StaticSubscriberGroup {
104                name: name.to_string(),
105                access_profile: arg_of(group, "access-profile").map(str::to_string),
106                password: password_of(group),
107                dynamic_profiles: dynamic_profiles(group),
108                service_profile: arg_of(group, "service-profile").map(str::to_string),
109                username_include: username_include(group),
110                interfaces: args_of(group, "interface").into_iter().map(str::to_string).collect(),
111            })
112            .collect(),
113    }
114}