Newer
Older
import ipaddress
from pydantic import BaseSettings
from gso.services import _ipam
class V6ServiceNetwork(BaseSettings):
v4: ipaddress.IPv4Network
v6: ipaddress.IPv6Network
class V4HostAddress(BaseSettings):
v6: ipaddress.IPv6Address
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)
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,
extattrs={}) -> HostAddresses:
return _ipam.allocate_service_host(
hostname=hostname,
service_type=service_type,
service_networks=service_networks,
host_addresses=host_addresses,
extattrs=extattrs)
JORGE SASIAIN
committed
# sample call flow to allocate two loopback interfaces and a trunk service
# new_service_host can be called passing networks or addresses
# - host h1 for service LO uses a specific ipv4/ipv6 address pair
# - the rest use the ipv4/ipv6 network pair
# networks and hosts can be allocated with extensible attributes
# - host h2 for service LO uses extattrs for both network and address/DNS
# - the rest don't use extattrs
JORGE SASIAIN
committed
hostname_A = 'h1'
hostname_B = 'h2'
# h1 LO (loopback)
lo1_service_networks = new_service_networks(
service_type='LO',
comment="Network for h1 LO"
)
JORGE SASIAIN
committed
lo1_v4_host_address = lo1_service_networks.v4.network_address
lo1_v6_host_address = lo1_service_networks.v6.network_address
lo1_host_addresses = HostAddresses(v4=lo1_v4_host_address,
v6=lo1_v6_host_address)
new_service_host(hostname=hostname_A,
service_type='LO',
host_addresses=lo1_host_addresses)
# h2 LO (loopback)
lo2_network_extattrs = {
"vrf_name": {"value": "dummy_vrf"},
}
lo2_host_extattrs = {
"Site": {"value": "dummy_site"},
}
lo2_service_networks = \
new_service_networks(service_type='LO',
comment="Network for h2 LO",
extattrs=lo2_network_extattrs)
new_service_host(hostname=hostname_B,
service_type='LO',
service_networks=lo2_service_networks,
extattrs=lo2_host_extattrs)
# h1-h2 TRUNK
trunk12_service_networks = new_service_networks(
service_type='TRUNK',
comment="Network for h1-h2 TRUNK"
)
new_service_host(hostname=hostname_A,
service_type='TRUNK',
service_networks=trunk12_service_networks)
new_service_host(hostname=hostname_B,
service_type='TRUNK',
service_networks=trunk12_service_networks)