pub fn add_peer(
    peers: &[Peer],
    cidr_tree: &CidrTree,
) -> Result<Option<(PeerContents, KeyPair)>, Error> {
    let leaves = cidr_tree.leaves();

    let cidr = choose_cidr(&leaves[..], "Eligible CIDRs for peer")?;

    let mut available_ip = None;
    let candidate_ips = cidr.iter().filter(|ip| cidr.is_assignable(*ip));
    for ip in candidate_ips {
        if peers.iter().find(|peer| peer.ip == ip).is_none() {
            available_ip = Some(ip);
            break;
        }
    }

    let available_ip = available_ip.expect("No IPs in this CIDR are avavilable");

    let ip = Input::with_theme(&*THEME)
        .with_prompt("IP")
        .default(available_ip)
        .interact()?;

    let name: String = Input::with_theme(&*THEME)
        .with_prompt("Name")
        .validate_with(hostname_validator)
        .interact()?;

    let is_admin = Confirm::with_theme(&*THEME)
        .with_prompt(&format!("Make {} an admin?", name))
        .default(false)
        .interact()?;
    let default_keypair = KeyPair::generate();
    let peer_request = PeerContents {
        name,
        ip,
        cidr_id: cidr.id,
        public_key: default_keypair.public.to_base64(),
        endpoint: None,
        is_admin,
        is_disabled: false,
        is_redeemed: false,
        persistent_keepalive_interval: Some(PERSISTENT_KEEPALIVE_INTERVAL_SECS),
    };

    Ok(
        if Confirm::with_theme(&*THEME)
            .with_prompt(&format!("Create peer {}?", peer_request.name.yellow()))
            .default(false)
            .interact()?
        {
            Some((peer_request, default_keypair))
        } else {
            None
        },
    )
}
/// Bring up a prompt to create a new peer. Returns the peer request.
Bring up a prompt to create a new peer. Returns the peer request.
Bring up a prompt to create a new peer . Returns the peer request .
pub fn add_peer(
    peers: &[Peer],
    cidr_tree: &CidrTree,
) -> Result<Option<(PeerContents, KeyPair)>, Error> {
    let leaves = cidr_tree.leaves();
    let cidr = choose_cidr(&leaves[..], "Eligible CIDRs for peer")?;
    let mut available_ip = None;
    let candidate_ips = cidr.iter().filter(|ip| cidr.is_assignable(*ip));
    for ip in candidate_ips {
        if peers.iter().find(|peer| peer.ip == ip).is_none() {
            available_ip = Some(ip);
            break;
        }
    }
    let available_ip = available_ip.expect("No IPs in this CIDR are avavilable");
    let ip = Input::with_theme(&*THEME)
        .with_prompt("IP")
        .default(available_ip)
        .interact()?;
    let name: String = Input::with_theme(&*THEME)
        .with_prompt("Name")
        .validate_with(hostname_validator)
        .interact()?;
    let is_admin = Confirm::with_theme(&*THEME)
        .with_prompt(&format!("Make {} an admin?", name))
        .default(false)
        .interact()?;
    let default_keypair = KeyPair::generate();
    let peer_request = PeerContents {
        name,
        ip,
        cidr_id: cidr.id,
        public_key: default_keypair.public.to_base64(),
        endpoint: None,
        is_admin,
        is_disabled: false,
        is_redeemed: false,
        persistent_keepalive_interval: Some(PERSISTENT_KEEPALIVE_INTERVAL_SECS),
    };
    Ok(
        if Confirm::with_theme(&*THEME)
            .with_prompt(&format!("Create peer {}?", peer_request.name.yellow()))
            .default(false)
            .interact()?
        {
            Some((peer_request, default_keypair))
        } else {
            None
        },
    )
}
pub fn add_peer ( peers : & [ Peer ] , cidr_tree : & CidrTree , ) -> Result < Option < ( PeerContents , KeyPair ) > , Error > { let leaves = cidr_tree . leaves ( ) ; let cidr = choose_cidr ( & leaves [ .. ] , " " ) ? ; let mut available_ip = None ; let candidate_ips = cidr . iter ( ) . filter ( | ip | cidr . is_assignable ( * ip ) ) ; for ip in candidate_ips { if peers . iter ( ) . find ( | peer | peer . ip == ip ) . is_none ( ) { available_ip = Some ( ip ) ; break ; } } let available_ip = available_ip . expect ( " " ) ; let ip = Input :: with_theme ( & * THEME ) . with_prompt ( " " ) . default ( available_ip ) . interact ( ) ? ; let name : String = Input :: with_theme ( & * THEME ) . with_prompt ( " " ) . validate_with ( hostname_validator ) . interact ( ) ? ; let is_admin = Confirm :: with_theme ( & * THEME ) . with_prompt ( & format ! ( " " name ) ) . default ( false ) . interact ( ) ? ; let default_keypair = KeyPair :: generate ( ) ; let peer_request = PeerContents { name , ip , cidr_id : cidr . id , public_key : default_keypair . public . to_base64 ( ) , endpoint : None , is_admin , is_disabled : false , is_redeemed : false , persistent_keepalive_interval : Some ( PERSISTENT_KEEPALIVE_INTERVAL_SECS ) , } ; Ok ( if Confirm :: with_theme ( & * THEME ) . with_prompt ( & format ! ( " " peer_request name yellow ( ) ) ) . default ( false ) . interact ( ) ? { Some ( ( peer_request , default_keypair ) ) } else { None } , ) }
Bring up a prompt to create a new peer.
Bring up a prompt to create a new peer .
peers
&[Peer]
cidr_tree
&CidrTree
peers
&[Peer]
cidr_tree
&CidrTree