from ipaddress import IPv4Network

AS_NUMBERS = {
    #               4290xxxxxx
    'home':         4290000138,
    'htz-cloud':    4290000137,
    'ovh':          4290000001,
}

def get_subnet_for_connection(repo, peer_a, peer_b):
    # XXX this assumes there are never more than 128 nodes which match that expression
    nodes = sorted({node.name for node in repo.nodes if node.has_bundle('wireguard')})

    assert peer_a in nodes
    assert peer_b in nodes

    pos_peer_a = nodes.index(peer_a)
    pos_peer_b = nodes.index(peer_b)

    vpn_subnet = list(IPv4Network('169.254.0.0/16').subnets(new_prefix=24))[pos_peer_a]
    return list(IPv4Network(vpn_subnet).subnets(new_prefix=31))[pos_peer_b]
