Merge lp:~james-page/charm-helpers/network-spaces into lp:charm-helpers

Proposed by James Page on 2016-03-07
Status: Merged
Merged at revision: 537
Proposed branch: lp:~james-page/charm-helpers/network-spaces
Merge into: lp:charm-helpers
Diff against target: 43 lines (+28/-0)
2 files modified
charmhelpers/core/hookenv.py (+13/-0)
tests/core/test_hookenv.py (+15/-0)
To merge this branch: bzr merge lp:~james-page/charm-helpers/network-spaces
Reviewer Review Type Date Requested Status
Charles Butler (community) 2016-03-07 Approve on 2016-03-07
Review via email: mp+288285@code.launchpad.net

Description of the Change

Add support for Juju 2.0 network spaces

Juju 2.0 adds a new hook tool called network-get to query for network
information for named relations and extra-bindings.

Add basic hook tool support and associated unit tests.

To post a comment you must log in.
Charles Butler (lazypower) wrote :

LGTM +1

*caveat I have not validated this in a hookenv.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2015-12-10 22:32:57 +0000
3+++ charmhelpers/core/hookenv.py 2016-03-07 12:57:24 +0000
4@@ -976,3 +976,16 @@
5 for callback, args, kwargs in reversed(_atexit):
6 callback(*args, **kwargs)
7 del _atexit[:]
8+
9+
10+@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
11+def network_get_primary_address(binding):
12+ '''
13+ Retrieve the primary network address for a named binding
14+
15+ :param binding: string. The name of a relation of extra-binding
16+ :return: string. The primary IP address for the named binding
17+ :raise: NotImplementedError if run on Juju < 2.0
18+ '''
19+ cmd = ['network-get', '--primary-address', binding]
20+ return subprocess.check_output(cmd).strip()
21
22=== modified file 'tests/core/test_hookenv.py'
23--- tests/core/test_hookenv.py 2015-12-10 22:44:10 +0000
24+++ tests/core/test_hookenv.py 2016-03-07 12:57:24 +0000
25@@ -1602,3 +1602,18 @@
26 self.assertEqual(result, expect)
27 check_output.assert_called_with(['storage-get', '--format=json',
28 '-s', storage_id])
29+
30+ @patch('subprocess.check_output')
31+ def test_network_get_primary(self, check_output):
32+ '''Ensure that network-get is called correctly and output is returned'''
33+ check_output.return_value = '192.168.22.1'
34+ ip = hookenv.network_get_primary_address('mybinding')
35+ check_output.assert_called_with(['network-get', '--primary-address', 'mybinding'])
36+ self.assertEqual(ip, '192.168.22.1')
37+
38+ @patch('subprocess.check_output')
39+ def test_network_get_primary_unsupported(self, check_output):
40+ '''Ensure that NotImplementedError is thrown when run on Juju < 2.0'''
41+ check_output.side_effect = OSError(2, 'network-get')
42+ self.assertRaises(NotImplementedError, hookenv.network_get_primary_address,
43+ 'mybinding')

Subscribers

People subscribed via source and target branches