asterust-junos 0.3.14

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `autoinstallation`
//! `system { autoinstallation { ... } }`.
//!
//! # See also
//!
//! - [autoinstallation](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/autoinstallation-edit-system.html)

use super::archival::{redact_password, server_secret};
use crate::{active_children, child_block, has_leaf, unquote, Secret};
use asterust::config::ConfigLine;

/// An interface listed under `autoinstallation interfaces`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AutoinstallationInterface {
    pub name: String,
    pub bootp: bool,
    pub rarp: bool,
}

/// The `system autoinstallation` configuration. A bare `autoinstallation;` has no servers or
/// interfaces.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Autoinstallation {
    /// Configuration server URLs, without any password.
    pub configuration_servers: Vec<String>,
    /// The password of each configuration server, in the order of `configuration_servers`: its `password` option, else
    /// the one embedded in the URL. All `None` unless the `secrets` feature is enabled.
    pub configuration_server_passwords: Vec<Option<Secret>>,
    pub interfaces: Vec<AutoinstallationInterface>,
    /// `usb { disable; }`.
    pub usb_disabled: bool,
}

pub(super) fn parse_autoinstallation(autoinstallation: &ConfigLine) -> Autoinstallation {
    Autoinstallation {
        configuration_servers: child_block(autoinstallation, "configuration-servers")
            .map(|servers| {
                active_children(servers)
                    .filter_map(|server| server.text.split_whitespace().next())
                    .map(|url| redact_password(unquote(url)))
                    .collect()
            })
            .unwrap_or_default(),
        configuration_server_passwords: child_block(autoinstallation, "configuration-servers")
            .map(|servers| {
                active_children(servers)
                    .filter(|server| server.text.split_whitespace().next().is_some())
                    .map(server_secret)
                    .collect()
            })
            .unwrap_or_default(),
        interfaces: child_block(autoinstallation, "interfaces")
            .map(|interfaces| {
                active_children(interfaces)
                    .map(|interface| AutoinstallationInterface {
                        name: interface.text.clone(),
                        bootp: has_leaf(interface, "bootp"),
                        rarp: has_leaf(interface, "rarp"),
                    })
                    .collect()
            })
            .unwrap_or_default(),
        usb_disabled: child_block(autoinstallation, "usb").is_some_and(|usb| has_leaf(usb, "disable")),
    }
}

#[cfg(test)]
mod tests {
    use crate::system::system;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_autoinstallation_settings() {
        let autoinstallation = system(&juniper::parse_braces(
            r#"
system {
    autoinstallation {
        configuration-servers {
            tftp://192.0.2.10/config;
            "ftp://svc:hunter2@192.0.2.11/config" {
                password "$9$secret"; ## SECRET-DATA
            }
        }
        interfaces {
            ge-0/0/0 {
                bootp;
            }
            ge-0/0/1 {
                bootp;
                rarp;
            }
        }
        usb {
            disable;
        }
    }
}
"#,
        ))
        .unwrap()
        .autoinstallation
        .unwrap();
        assert_eq!(
            autoinstallation.configuration_servers,
            ["tftp://192.0.2.10/config", "ftp://svc@192.0.2.11/config"]
        );
        let passwords: Vec<_> = autoinstallation
            .configuration_server_passwords
            .iter()
            .map(|p| p.as_ref().map(crate::Secret::expose))
            .collect();
        assert_eq!(passwords, [None, crate::secret::expected("$9$secret")]);
        assert_eq!(autoinstallation.interfaces.len(), 2);
        assert_eq!(autoinstallation.interfaces[0].name, "ge-0/0/0");
        assert!(autoinstallation.interfaces[0].bootp);
        assert!(!autoinstallation.interfaces[0].rarp);
        assert!(autoinstallation.interfaces[1].rarp);
        assert!(autoinstallation.usb_disabled);
    }

    #[test]
    fn bare_autoinstallation_is_enabled_with_no_settings() {
        let autoinstallation = system(&juniper::parse_braces("system {\n    autoinstallation;\n}\n"))
            .unwrap()
            .autoinstallation
            .unwrap();
        assert!(autoinstallation.configuration_servers.is_empty());
        assert!(autoinstallation.interfaces.is_empty());
        assert!(!autoinstallation.usb_disabled);
    }

    #[test]
    fn no_autoinstallation_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.autoinstallation.is_none());
    }
}