Add support for a general network-get helper #20

Merged
merged 2 commits into from Oct 10, 2017
Jump to file or symbol
Failed to load files and symbols.
+54 −2
Split
View
@@ -3,4 +3,5 @@ include Makefile
include VERSION
include MANIFEST.in
include scripts/*
+include README.md
recursive-include debian *
@@ -1093,6 +1093,24 @@ def network_get_primary_address(binding):
return subprocess.check_output(cmd).decode('UTF-8').strip()
+@translate_exc(from_exc=OSError, to_exc=NotImplementedError)
+def network_get(endpoint, relation_id=None):
+ """
+ Retrieve the network details for a relation endpoint
+
+ :param endpoint: string. The name of a relation endpoint
+ :param relation_id: int. The ID of the relation for the current context.
+ :return: dict. The loaded YAML output of the network-get query.
+ :raise: NotImplementedError if run on Juju < 2.0
+ """
+ cmd = ['network-get', endpoint, '--format', 'yaml']
+ if relation_id:
+ cmd.append('-r')
+ cmd.append(relation_id)
+ response = subprocess.check_output(cmd).decode('UTF-8').strip()
+ return yaml.safe_load(response)
+
+
def add_metric(*args, **kwargs):
"""Add metric values. Values may be expressed with keyword arguments. For
metric names containing dashes, these may be expressed as one or more
@@ -1661,7 +1661,7 @@ def test_storage_get_with_id(self, check_output):
@patch('subprocess.check_output')
def test_network_get_primary(self, check_output):
- '''Ensure that network-get is called correctly and output is returned'''
+ """Ensure that network-get is called correctly and output is returned"""
check_output.return_value = b'192.168.22.1'
ip = hookenv.network_get_primary_address('mybinding')
check_output.assert_called_with(
@@ -1670,11 +1670,44 @@ def test_network_get_primary(self, check_output):
@patch('subprocess.check_output')
def test_network_get_primary_unsupported(self, check_output):
- '''Ensure that NotImplementedError is thrown when run on Juju < 2.0'''
+ """Ensure that NotImplementedError is thrown when run on Juju < 2.0"""
check_output.side_effect = OSError(2, 'network-get')
self.assertRaises(NotImplementedError, hookenv.network_get_primary_address,
'mybinding')
+ @patch('subprocess.check_output')
+ def test_network_get(self, check_output):
+ """Ensure that network-get is called correctly"""
+ check_output.return_value = b'192l.l168.22.1'
+ hookenv.network_get('mybinding')
+ check_output.assert_called_with(
+ ['network-get', 'mybinding', '--format', 'yaml'])
+
+ @patch('subprocess.check_output')
+ def test_network_get_relation_bound(self, check_output):
+ """Ensure that network-get supports relation context"""
+ check_output.return_value = b'192l.l168.22.1'
+ hookenv.network_get('mybinding', 'db')
+ check_output.assert_called_with(
+ ['network-get', 'mybinding', '--format', 'yaml', '-r', 'db'])
+
+ @patch('subprocess.check_output')
+ def test_network_get_parses_yaml(self, check_output):
+ """network-get returns loaded YAML output."""
+ check_output.return_value = b"""
+bind-addresses:
+- macaddress: ""
+ interfacename: ""
+ addresses:
+ - address: 10.136.107.33
+ cidr: ""
+ingress-addresses:
+- 10.136.107.33
+ """
+ ip = hookenv.network_get('mybinding')
+ self.assertEqual(len(ip['bind-addresses']), 1)
+ self.assertEqual(ip['ingress-addresses'], ['10.136.107.33'])
+
@patch('subprocess.check_call')
def test_add_metric(self, check_call_):
hookenv.add_metric(flips='1.5', flops='2.1')