Merge lp:~hopem/charm-helpers/lp1525845 into lp:charm-helpers

Proposed by Edward Hope-Morley
Status: Merged
Merged at revision: 509
Proposed branch: lp:~hopem/charm-helpers/lp1525845
Merge into: lp:charm-helpers
Diff against target: 87 lines (+32/-19)
2 files modified
charmhelpers/contrib/network/ip.py (+21/-19)
tests/contrib/network/test_ip.py (+11/-0)
To merge this branch: bzr merge lp:~hopem/charm-helpers/lp1525845
Reviewer Review Type Date Requested Status
OpenStack Charmers Pending
James Page Pending
Review via email: mp+280453@code.launchpad.net

This proposal supersedes a proposal from 2015-12-14.

To post a comment you must log in.
Revision history for this message
James Page (james-page) wrote : Posted in a previous version of this proposal

Some units tests would be nice :-)

review: Needs Fixing

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/network/ip.py'
2--- charmhelpers/contrib/network/ip.py 2015-09-18 07:21:17 +0000
3+++ charmhelpers/contrib/network/ip.py 2015-12-14 15:18:36 +0000
4@@ -53,7 +53,7 @@
5
6
7 def no_ip_found_error_out(network):
8- errmsg = ("No IP address found in network: %s" % network)
9+ errmsg = ("No IP address found in network(s): %s" % network)
10 raise ValueError(errmsg)
11
12
13@@ -61,7 +61,7 @@
14 """Get an IPv4 or IPv6 address within the network from the host.
15
16 :param network (str): CIDR presentation format. For example,
17- '192.168.1.0/24'.
18+ '192.168.1.0/24'. Supports multiple networks as a space-delimited list.
19 :param fallback (str): If no address is found, return fallback.
20 :param fatal (boolean): If no address is found, fallback is not
21 set and fatal is True then exit(1).
22@@ -75,24 +75,26 @@
23 else:
24 return None
25
26- _validate_cidr(network)
27- network = netaddr.IPNetwork(network)
28- for iface in netifaces.interfaces():
29- addresses = netifaces.ifaddresses(iface)
30- if network.version == 4 and netifaces.AF_INET in addresses:
31- addr = addresses[netifaces.AF_INET][0]['addr']
32- netmask = addresses[netifaces.AF_INET][0]['netmask']
33- cidr = netaddr.IPNetwork("%s/%s" % (addr, netmask))
34- if cidr in network:
35- return str(cidr.ip)
36+ networks = network.split() or [network]
37+ for network in networks:
38+ _validate_cidr(network)
39+ network = netaddr.IPNetwork(network)
40+ for iface in netifaces.interfaces():
41+ addresses = netifaces.ifaddresses(iface)
42+ if network.version == 4 and netifaces.AF_INET in addresses:
43+ addr = addresses[netifaces.AF_INET][0]['addr']
44+ netmask = addresses[netifaces.AF_INET][0]['netmask']
45+ cidr = netaddr.IPNetwork("%s/%s" % (addr, netmask))
46+ if cidr in network:
47+ return str(cidr.ip)
48
49- if network.version == 6 and netifaces.AF_INET6 in addresses:
50- for addr in addresses[netifaces.AF_INET6]:
51- if not addr['addr'].startswith('fe80'):
52- cidr = netaddr.IPNetwork("%s/%s" % (addr['addr'],
53- addr['netmask']))
54- if cidr in network:
55- return str(cidr.ip)
56+ if network.version == 6 and netifaces.AF_INET6 in addresses:
57+ for addr in addresses[netifaces.AF_INET6]:
58+ if not addr['addr'].startswith('fe80'):
59+ cidr = netaddr.IPNetwork("%s/%s" % (addr['addr'],
60+ addr['netmask']))
61+ if cidr in network:
62+ return str(cidr.ip)
63
64 if fallback is not None:
65 return fallback
66
67=== modified file 'tests/contrib/network/test_ip.py'
68--- tests/contrib/network/test_ip.py 2015-08-26 18:27:32 +0000
69+++ tests/contrib/network/test_ip.py 2015-12-14 15:18:36 +0000
70@@ -171,6 +171,17 @@
71 def test_get_address_in_network_ipv4(self):
72 self._test_get_address_in_network('192.168.1.55', '192.168.1.0/24')
73
74+ def test_get_address_in_network_ipv4_multi(self):
75+ # Assumes that there is an address configured on both but the first
76+ # one is picked#
77+ self._test_get_address_in_network('192.168.1.55',
78+ '192.168.1.0/24 192.168.10.0/24')
79+
80+ def test_get_address_in_network_ipv4_multi2(self):
81+ # Assumes that there is nothing configured on 192.168.11.0/24
82+ self._test_get_address_in_network('192.168.10.58',
83+ '192.168.11.0/24 192.168.10.0/24')
84+
85 def test_get_address_in_network_ipv6(self):
86 self._test_get_address_in_network('2a01:348:2f4:0:685e:5748:ae62:209f',
87 '2a01:348:2f4::/64')

Subscribers

People subscribed via source and target branches