From 2677eb3a2ff2505d6dd5f4b75a3bf0a1bf16b10f Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Wed, 6 Mar 2019 11:09:40 +0100 Subject: [PATCH] Add ``public_network`` and ``cluster_network`` properties Change-Id: Ia49f4921e772376763be178e11d7777676ccc8da --- requires.py | 27 +++++++++++++++++++++++++-- unit_tests/test_requires.py | 20 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/requires.py b/requires.py index a51424b..62b4424 100644 --- a/requires.py +++ b/requires.py @@ -29,6 +29,7 @@ from charms.reactive import ( ) import charmhelpers.contrib.storage.linux.ceph as ch_ceph +import charmhelpers.contrib.network.ip as ch_ip class CephRBDMirrorRequires(Endpoint): @@ -182,11 +183,33 @@ class CephRBDMirrorRequires(Endpoint): @property def public_network(self): - pass + """Get CIDR for the Ceph public network. + + The public network address advertiesed on the relation is mapped to the + corrensponding local interface from which we get the netmask/cidr of + the network. + + :returns: CIDR or None + :rtype: Option[str, None] + """ + public_addr = self.all_joined_units.received['ceph-public-address'] + if public_addr: + return ch_ip.resolve_network_cidr(public_addr) @property def cluster_network(self): - pass + """Get CIDR for the Ceph cluster network. + + The cluster network address advertiesed on the relation is mapped to + the corrensponding local interface from which we get the netmask/cidr + of the network. + + :returns: CIDR or None + :rtype: Option[str, None] + """ + cluster_addr = self.all_joined_units.received['ceph-cluster-address'] + if cluster_addr: + return ch_ip.resolve_network_cidr(cluster_addr) @property def pools(self): diff --git a/unit_tests/test_requires.py b/unit_tests/test_requires.py index 1979acf..f48de2d 100644 --- a/unit_tests/test_requires.py +++ b/unit_tests/test_requires.py @@ -189,3 +189,23 @@ class TestCephRBDMirrorRequires(test_utils.PatchHelper): self._relations.__iter__.return_value = [relation] self.assertEqual(list(self.requires_class.mon_hosts()), ['[2001:db8:42::1]:6789', '192.0.2.1:6789']) + + def test_public_network(self): + self.patch_requires_class('_all_joined_units') + self._all_joined_units.received.__getitem__.return_value = '192.0.2.1' + self.patch_object(requires.ch_ip, 'resolve_network_cidr') + self.resolve_network_cidr.return_value = '192.0.2.0/24' + self.assertEqual(self.requires_class.public_network, '192.0.2.0/24') + self._all_joined_units.received.__getitem__.assert_called_once_with( + 'ceph-public-address') + self.resolve_network_cidr.assert_called_once_with('192.0.2.1') + + def test_cluster_network(self): + self.patch_requires_class('_all_joined_units') + self._all_joined_units.received.__getitem__.return_value = '192.0.2.1' + self.patch_object(requires.ch_ip, 'resolve_network_cidr') + self.resolve_network_cidr.return_value = '192.0.2.0/24' + self.assertEqual(self.requires_class.cluster_network, '192.0.2.0/24') + self._all_joined_units.received.__getitem__.assert_called_once_with( + 'ceph-cluster-address') + self.resolve_network_cidr.assert_called_once_with('192.0.2.1')