Skip to content
Snippets Groups Projects
ipam.py 3.46 KiB
Newer Older
Erik Reid's avatar
Erik Reid committed
import ipaddress
from pydantic import BaseSettings
from gso.services import _ipam
Erik Reid's avatar
Erik Reid committed

class V4ServiceNetwork(BaseSettings):
Erik Reid's avatar
Erik Reid committed
    v4: ipaddress.IPv4Network


class V6ServiceNetwork(BaseSettings):
Erik Reid's avatar
Erik Reid committed
    v6: ipaddress.IPv6Network


class ServiceNetworks(BaseSettings):
    v4: ipaddress.IPv4Network
    v6: ipaddress.IPv6Network


class V4HostAddress(BaseSettings):
Erik Reid's avatar
Erik Reid committed
    v4: ipaddress.IPv4Address


class V6HostAddress(BaseSettings):


class HostAddresses(BaseSettings):
    v4: ipaddress.IPv4Address
    v6: ipaddress.IPv6Address
def new_service_networks(service_type,
                         comment="",
                         extattrs={}) -> ServiceNetworks:
    v4_service_network = _ipam.allocate_service_ipv4_network(
        service_type=service_type, comment=comment, extattrs=extattrs)
    v6_service_network = _ipam.allocate_service_ipv6_network(
        service_type=service_type, comment=comment, extattrs=extattrs)
Erik Reid's avatar
Erik Reid committed
    return ServiceNetworks(
        v4=v4_service_network.v4,
        v6=v6_service_network.v6)
def new_service_host(hostname,
                     service_type,
                     service_networks: ServiceNetworks = None,
                     host_addresses: HostAddresses = None,
                     cname_aliases=None,
                     extattrs={}) -> HostAddresses:
    return _ipam.allocate_service_host(
        hostname=hostname,
        service_type=service_type,
        service_networks=service_networks,
        host_addresses=host_addresses,
        cname_aliases=cname_aliases,
        extattrs=extattrs)


if __name__ == '__main__':
    # sample call flow to allocate two loopback interfaces and a trunk service
    # new_service_host can be called passing networks, addresses, or nothing.
    # - host h1 for service TRUNK uses a specific ipv4/ipv6 address pair
    # - host h2 for service TRUNK uses the ipv4/ipv6 network pair
    # - service LO uses nothing
    # networks and hosts can be allocated with extensible attributes
    # networks can be created with a comment
    # CNAME records can be optionally created
    hostname_A = 'hA'
    hostname_B = 'hB'

    # h1 LO (loopback)
    new_service_host(hostname=hostname_A+"_LO",
                     cname_aliases=["alias1.hA", "alias2.hA"],
                     service_type='LO')

    # h2 LO (loopback)
    new_service_host(hostname=hostname_B+"_LO",
                     cname_aliases=["alias1.hB"],
                     service_type='LO')

    # h1-h2 TRUNK
    trunk12_network_extattrs = {
        "vrf_name": {"value": "dummy_vrf"},
    }
    trunk12_host_extattrs = {
        "Site": {"value": "dummy_site"},
    }
    trunk12_service_networks = new_service_networks(
        service_type='TRUNK',
        extattrs=trunk12_network_extattrs,
        comment="Network for h1-h2 TRUNK"
    )

    trunk12_v4_host_address = trunk12_service_networks.v4.network_address
    trunk12_v6_host_address = trunk12_service_networks.v6.network_address
    trunk12_host_addresses = HostAddresses(v4=trunk12_v4_host_address,
                                           v6=trunk12_v6_host_address)

    new_service_host(hostname=hostname_A+"_TRUNK",
                     service_type='TRUNK',
                     host_addresses=trunk12_host_addresses,
                     extattrs=trunk12_host_extattrs)

    new_service_host(hostname=hostname_B+"_TRUNK",
                     service_type='TRUNK',
                     service_networks=trunk12_service_networks,
                     extattrs=trunk12_host_extattrs)