author | Tero Marttila <terom@paivola.fi> |
Mon, 09 Mar 2015 21:17:06 +0200 | |
changeset 736 | 75938aa0390b |
parent 735 | 008cfe47b194 |
child 738 | 3104fdf7ea26 |
permissions | -rw-r--r-- |
735
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
import ipaddress, ipaddr # XXX: conversion |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
2 |
import pvl.hosts |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
3 |
|
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
4 |
def parse_interfaces(interfaces): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
5 |
for interface, value in interfaces.iteritems(): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
6 |
if not isinstance(value, dict): |
736
75938aa0390b
pvl.hosts.interfaces: remove junos-specifics
Tero Marttila <terom@paivola.fi>
parents:
735
diff
changeset
|
7 |
yield (interface, None), ipaddress.ip_interface(value) |
735
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
8 |
else: |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
9 |
for unit, ip in value.iteritems(): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
10 |
yield (interface, int(unit)), ipaddress.ip_interface(value) |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
11 |
|
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
12 |
@pvl.hosts.extension |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
13 |
class HostInterface(object): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
14 |
EXTENSION = 'interface' |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
15 |
|
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
16 |
@classmethod |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
17 |
def build (cls, **interfaces): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
18 |
return cls(dict(parse_interfaces(interfaces))) |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
19 |
|
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
20 |
def __init__ (self, interfaces): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
21 |
self.interfaces = interfaces |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
22 |
|
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
23 |
def addresses (self): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
24 |
for (iface, unit), ip in self.interfaces.iteritems(): |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
25 |
# XXX: ipaddr |
008cfe47b194
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
26 |
yield iface, ipaddr.IPAddress(str(ip.ip)) |