1- from ipaddress import IPv4Interface
1+ from ipaddress import IPv4Interface , ip_network
22
33from ....utils import get_copy
44from .base import RaspbianConverter
@@ -10,6 +10,7 @@ class Interfaces(RaspbianConverter):
1010 def to_intermediate (self ):
1111 result = []
1212 interfaces = get_copy (self .netjson , self .netjson_key )
13+ routes = get_copy (self .netjson , 'routes' )
1314 for interface in interfaces :
1415 new_interface = {}
1516 ifname = interface .get ('name' )
@@ -19,7 +20,7 @@ def to_intermediate(self):
1920 'iftype' : iftype
2021 })
2122 if iftype in ['ethernet' , 'bridge' , 'loopback' , 'wireless' ]:
22- addresses = self ._get_address (interface )
23+ addresses = self ._get_address (interface , routes )
2324 new_interface .update ({
2425 'address' : addresses
2526 })
@@ -48,15 +49,25 @@ def to_intermediate(self):
4849 result .append (new_interface )
4950 return (('interfaces' , result ),)
5051
51- def _get_address (self , interface ):
52+ def _get_address (self , interface , routes ):
5253 addresses = interface .get ('addresses' , False )
5354 if addresses :
5455 for address in addresses :
5556 if address .get ('proto' ) == 'static' :
5657 if address .get ('family' ) == 'ipv4' :
58+
5759 address_mask = str (address .get ('address' )) + '/' + str (address .get ('mask' ))
5860 address ['netmask' ] = IPv4Interface (address_mask ).with_netmask .split ('/' )[1 ]
5961 del address ['mask' ]
62+ if routes :
63+ for route in routes :
64+ if ip_network (route .get ('next' )).version == 4 :
65+ destination = IPv4Interface (route ['destination' ]).with_netmask
66+ dest , dest_mask = destination .split ('/' )
67+ route ['dest' ] = dest
68+ route ['dest_mask' ] = dest_mask
69+ del route ['destination' ]
70+ address ['route' ] = route
6071 if address .get ('family' ) == 'ipv6' :
6172 address ['netmask' ] = address ['mask' ]
6273 del address ['mask' ]
0 commit comments