Skip to content

Commit

Permalink
ipdb: introduce ipaddr views
Browse files Browse the repository at this point in the history
Views::
    ipdb.interfaces.eth0.ipaddr
    ipdb.interfaces.eth0.ipaddr.ipv4
    ipdb.interfaces.eth0.ipaddr.ipv6

Bug-Url: #302
  • Loading branch information
svinota committed May 31, 2017
1 parent fec71c3 commit 11dbfe8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pyroute2/ipdb/linkedset.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def disconnect(self, link):
self.links.remove(link)

def __repr__(self):
return repr(list(self))
return repr(tuple(self))


class IPaddrSet(LinkedSet):
Expand All @@ -166,6 +166,22 @@ class IPaddrSet(LinkedSet):
IPv6 addresses, but it may be changed with the `ignore_link_local`
argument.
'''
@property
def ipv4(self):
ret = IPaddrSet()
for x in self:
if self[x]['family'] == AF_INET:
ret.add(x, self[x])
return ret

@property
def ipv6(self):
ret = IPaddrSet()
for x in self:
if self[x]['family'] == AF_INET6:
ret.add(x, self[x])
return ret

def wait_ip(self, net, mask=None, timeout=None, ignore_link_local=False):
family = AF_INET6 if net.find(':') >= 0 else AF_INET
alen = 32 if family == AF_INET else 128
Expand Down Expand Up @@ -204,9 +220,6 @@ def match_ip(ipset):
self.clear_target(target)
return ret

def __repr__(self):
return repr(['%s/%s' % x for x in self])

def __getitem__(self, key):
if isinstance(key, (tuple, list)):
return self.raw[key]
Expand Down
13 changes: 13 additions & 0 deletions tests/general/test_ipdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,19 @@ def test_snapshots(self):
assert ('172.16.0.1', 24) in self.ip.interfaces[self.ifd].ipaddr
assert self.ip.interfaces[self.ifd].flags & 1

def test_ipaddr_views(self):
require_user('root')

ifA = self.get_ifname()
i = (self.ip
.create(kind='dummy', ifname=ifA)
.add_ip('172.16.0.1/24')
.add_ip('fdb3:84e5:4ff4:55e4::1/64')
.commit())

assert len(i.ipaddr.ipv4) + len(i.ipaddr.ipv6) == len(i.ipaddr)
assert len(i.ipaddr) >= 2

@skip_if_not_supported
def _test_ipv(self, ipv, kind):
require_user('root')
Expand Down

0 comments on commit 11dbfe8

Please sign in to comment.