asterust-junos 0.5.11

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `static-subscribers`
//! `system { services { static-subscribers { ... } } }`.
//!
//! # See also
//!
//! - [authentication (Static Subscribers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-edit-static-subscribers.html)
//! - [interface (Static Subscriber Group)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-static-subscribers-group.html)
//! - [interface (Static Subscriber Username)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-static-subscribers-username.html)
//! - [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)
//! - [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)
//! - [service-profile (Static Subscribers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/service-profile-edit-static-subscribers.html)
//! - [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)

use crate::{arg_of, args_of, child_block, has_leaf, named_children, secret_value, Secret};
use asterust::config::ConfigLine;

/// The `username-include` block of the static subscribers `authentication`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct StaticSubscriberUsername {
    pub delimiter: Option<String>,
    pub domain_name: Option<String>,
    pub interface: bool,
    pub logical_system_name: bool,
    pub routing_instance_name: bool,
    pub user_prefix: Option<String>,
    pub vlan_tags: bool,
}

/// A `dynamic-profile <name>` of a static-subscribers group.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct StaticSubscriberProfile {
    pub name: String,
    /// `merge` or `replace`.
    pub aggregate_clients: Option<String>,
}

/// A `group <name>` under `static-subscribers`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct StaticSubscriberGroup {
    pub name: String,
    pub access_profile: Option<String>,
    /// The `authentication { password ...; }`; `None` unless the `secrets` feature is enabled.
    pub password: Option<Secret>,
    pub dynamic_profiles: Vec<StaticSubscriberProfile>,
    pub service_profile: Option<String>,
    pub username_include: Option<StaticSubscriberUsername>,
    /// Each `interface` as written, e.g. `"ge-1/0/0.100 upto ge-1/0/0.200"` or `"ge-1/0/0.5
    /// exclude"`.
    pub interfaces: Vec<String>,
}

/// The `system services static-subscribers` settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct StaticSubscribers {
    pub access_profile: Option<String>,
    pub service_profile: Option<String>,
    /// The `authentication { password ...; }`; `None` unless the `secrets` feature is enabled.
    pub password: Option<Secret>,
    pub username_include: Option<StaticSubscriberUsername>,
    pub dynamic_profiles: Vec<StaticSubscriberProfile>,
    pub groups: Vec<StaticSubscriberGroup>,
}

/// The password of the `authentication` block of `line`.
fn password_of(line: &ConfigLine) -> Option<Secret> {
    secret_value(child_block(line, "authentication").and_then(|auth| arg_of(auth, "password")))
}

fn username_include(line: &ConfigLine) -> Option<StaticSubscriberUsername> {
    let include = child_block(child_block(line, "authentication")?, "username-include")?;
    Some(StaticSubscriberUsername {
        delimiter: arg_of(include, "delimiter").map(str::to_string),
        domain_name: arg_of(include, "domain-name").map(str::to_string),
        interface: has_leaf(include, "interface"),
        logical_system_name: has_leaf(include, "logical-system-name"),
        routing_instance_name: has_leaf(include, "routing-instance-name"),
        user_prefix: arg_of(include, "user-prefix").map(str::to_string),
        vlan_tags: has_leaf(include, "vlan-tags"),
    })
}

fn dynamic_profiles(line: &ConfigLine) -> Vec<StaticSubscriberProfile> {
    named_children(line, "dynamic-profile")
        .map(|(name, profile)| StaticSubscriberProfile {
            name: name.to_string(),
            aggregate_clients: arg_of(profile, "aggregate-clients").map(str::to_string),
        })
        .collect()
}

pub(super) fn parse_static_subscribers(subscribers: &ConfigLine) -> StaticSubscribers {
    StaticSubscribers {
        access_profile: arg_of(subscribers, "access-profile").map(str::to_string),
        service_profile: arg_of(subscribers, "service-profile").map(str::to_string),
        password: password_of(subscribers),
        username_include: username_include(subscribers),
        dynamic_profiles: dynamic_profiles(subscribers),
        groups: named_children(subscribers, "group")
            .map(|(name, group)| StaticSubscriberGroup {
                name: name.to_string(),
                access_profile: arg_of(group, "access-profile").map(str::to_string),
                password: password_of(group),
                dynamic_profiles: dynamic_profiles(group),
                service_profile: arg_of(group, "service-profile").map(str::to_string),
                username_include: username_include(group),
                interfaces: args_of(group, "interface").into_iter().map(str::to_string).collect(),
            })
            .collect(),
    }
}